-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert deleted files, add deprecation message
- Loading branch information
Showing
5 changed files
with
326 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\Bundle\LayoutsSyliusBundle\EventListener\Shop; | ||
|
||
use Netgen\Layouts\Context\Context; | ||
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; | ||
use Sylius\Component\Locale\Context\LocaleContextInterface; | ||
use Sylius\Component\Taxonomy\Model\TaxonInterface; | ||
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** @deprecated since 1.4.6 together with nglayouts_sylius_taxon attribute, use nglayouts_sylius_resource instead */ | ||
final class ProductIndexListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @param \Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface<\Sylius\Component\Taxonomy\Model\TaxonInterface> $taxonRepository | ||
*/ | ||
public function __construct( | ||
private TaxonRepositoryInterface $taxonRepository, | ||
private LocaleContextInterface $localeContext, | ||
private RequestStack $requestStack, | ||
private Context $context, | ||
) {} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return ['sylius.product.index' => 'onProductIndex']; | ||
} | ||
|
||
/** | ||
* Sets the currently displayed taxon to the request, | ||
* to be able to match with layout resolver. | ||
*/ | ||
public function onProductIndex(ResourceControllerEvent $event): void | ||
{ | ||
$currentRequest = $this->requestStack->getCurrentRequest(); | ||
if (!$currentRequest instanceof Request) { | ||
return; | ||
} | ||
|
||
// Only sane way to extract the reference to the taxon | ||
if (!$currentRequest->attributes->has('slug')) { | ||
return; | ||
} | ||
|
||
$taxon = $this->taxonRepository->findOneBySlug( | ||
$currentRequest->attributes->get('slug'), | ||
$this->localeContext->getLocaleCode(), | ||
); | ||
|
||
if (!$taxon instanceof TaxonInterface) { | ||
return; | ||
} | ||
|
||
$currentRequest->attributes->set('nglayouts_sylius_taxon', $taxon); | ||
// We set context here instead in a ContextProvider, since sylius.taxon.show | ||
// event happens too late, after onKernelRequest event has already been executed | ||
$this->context->set('sylius_taxon_id', (int) $taxon->getId()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\Bundle\LayoutsSyliusBundle\EventListener\Shop; | ||
|
||
use Netgen\Layouts\Context\Context; | ||
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; | ||
use Sylius\Component\Product\Model\ProductInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** @deprecated since 1.4.6 together with nglayouts_sylius_product attribute, use nglayouts_sylius_resource instead */ | ||
final class ProductShowListener implements EventSubscriberInterface | ||
{ | ||
public function __construct(private RequestStack $requestStack, private Context $context) {} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return ['sylius.product.show' => 'onProductShow']; | ||
} | ||
|
||
/** | ||
* Sets the currently displayed product to the request, | ||
* to be able to match with layout resolver. | ||
*/ | ||
public function onProductShow(ResourceControllerEvent $event): void | ||
{ | ||
$product = $event->getSubject(); | ||
if (!$product instanceof ProductInterface) { | ||
return; | ||
} | ||
|
||
$currentRequest = $this->requestStack->getCurrentRequest(); | ||
if ($currentRequest instanceof Request) { | ||
$currentRequest->attributes->set('nglayouts_sylius_product', $product); | ||
// We set context here instead in a ContextProvider, since sylius.product.show | ||
// event happens too late, after onKernelRequest event has already been executed | ||
$this->context->set('sylius_product_id', (int) $product->getId()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
tests/bundle/EventListener/Shop/ProductIndexListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\Bundle\LayoutsSyliusBundle\Tests\EventListener\Shop; | ||
|
||
use Netgen\Bundle\LayoutsSyliusBundle\EventListener\Shop\ProductIndexListener; | ||
use Netgen\Layouts\Context\Context; | ||
use Netgen\Layouts\Sylius\Tests\Stubs\Taxon; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; | ||
use Sylius\Component\Locale\Context\LocaleContextInterface; | ||
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
#[CoversClass(ProductIndexListener::class)] | ||
final class ProductIndexListenerTest extends TestCase | ||
{ | ||
private ProductIndexListener $listener; | ||
|
||
private MockObject $taxonRepositoryMock; | ||
|
||
private MockObject $localeContextMock; | ||
|
||
private RequestStack $requestStack; | ||
|
||
private Context $context; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->taxonRepositoryMock = $this->createMock(TaxonRepositoryInterface::class); | ||
$this->localeContextMock = $this->createMock(LocaleContextInterface::class); | ||
$this->requestStack = new RequestStack(); | ||
$this->context = new Context(); | ||
|
||
$this->localeContextMock | ||
->method('getLocaleCode') | ||
->willReturn('en'); | ||
|
||
$this->listener = new ProductIndexListener( | ||
$this->taxonRepositoryMock, | ||
$this->localeContextMock, | ||
$this->requestStack, | ||
$this->context, | ||
); | ||
} | ||
|
||
public function testGetSubscribedEvents(): void | ||
{ | ||
self::assertSame( | ||
['sylius.product.index' => 'onProductIndex'], | ||
$this->listener::getSubscribedEvents(), | ||
); | ||
} | ||
|
||
public function testOnProductIndex(): void | ||
{ | ||
$request = Request::create('/'); | ||
$request->attributes->set('slug', 'mugs'); | ||
|
||
$this->requestStack->push($request); | ||
|
||
$taxon = new Taxon(42); | ||
|
||
$this->taxonRepositoryMock | ||
->expects(self::once()) | ||
->method('findOneBySlug') | ||
->with(self::identicalTo('mugs'), self::identicalTo('en')) | ||
->willReturn($taxon); | ||
|
||
$event = new ResourceControllerEvent(); | ||
$this->listener->onProductIndex($event); | ||
|
||
self::assertSame($taxon, $request->attributes->get('nglayouts_sylius_taxon')); | ||
|
||
self::assertTrue($this->context->has('sylius_taxon_id')); | ||
self::assertSame(42, $this->context->get('sylius_taxon_id')); | ||
} | ||
|
||
public function testOnProductIndexWithoutRequest(): void | ||
{ | ||
$this->taxonRepositoryMock | ||
->expects(self::never()) | ||
->method('findOneBySlug'); | ||
|
||
$event = new ResourceControllerEvent(); | ||
$this->listener->onProductIndex($event); | ||
|
||
self::assertFalse($this->context->has('sylius_taxon_id')); | ||
} | ||
|
||
public function testOnProductIndexWithoutSlug(): void | ||
{ | ||
$request = Request::create('/'); | ||
$this->requestStack->push($request); | ||
|
||
$this->taxonRepositoryMock | ||
->expects(self::never()) | ||
->method('findOneBySlug'); | ||
|
||
$event = new ResourceControllerEvent(); | ||
$this->listener->onProductIndex($event); | ||
|
||
self::assertFalse($request->attributes->has('nglayouts_sylius_taxon')); | ||
self::assertFalse($request->attributes->has('nglayouts_sylius_resource')); | ||
self::assertFalse($this->context->has('sylius_taxon_id')); | ||
} | ||
|
||
public function testOnProductIndexWithNonExistingTaxon(): void | ||
{ | ||
$request = Request::create('/'); | ||
$request->attributes->set('slug', 'unknown'); | ||
|
||
$this->requestStack->push($request); | ||
|
||
$this->taxonRepositoryMock | ||
->expects(self::once()) | ||
->method('findOneBySlug') | ||
->with(self::identicalTo('unknown'), self::identicalTo('en')) | ||
->willReturn(null); | ||
|
||
$event = new ResourceControllerEvent(); | ||
$this->listener->onProductIndex($event); | ||
|
||
self::assertFalse($request->attributes->has('nglayouts_sylius_taxon')); | ||
self::assertFalse($request->attributes->has('nglayouts_sylius_resource')); | ||
self::assertFalse($this->context->has('sylius_taxon_id')); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
tests/bundle/EventListener/Shop/ProductShowListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\Bundle\LayoutsSyliusBundle\Tests\EventListener\Shop; | ||
|
||
use Netgen\Bundle\LayoutsSyliusBundle\EventListener\Shop\ProductShowListener; | ||
use Netgen\Layouts\Context\Context; | ||
use Netgen\Layouts\Sylius\Tests\Stubs\Product; | ||
use Netgen\Layouts\Sylius\Tests\Stubs\Taxon; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
#[CoversClass(ProductShowListener::class)] | ||
final class ProductShowListenerTest extends TestCase | ||
{ | ||
private ProductShowListener $listener; | ||
|
||
private RequestStack $requestStack; | ||
|
||
private Context $context; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->requestStack = new RequestStack(); | ||
$this->context = new Context(); | ||
|
||
$this->listener = new ProductShowListener($this->requestStack, $this->context); | ||
} | ||
|
||
public function testGetSubscribedEvents(): void | ||
{ | ||
self::assertSame( | ||
['sylius.product.show' => 'onProductShow'], | ||
$this->listener::getSubscribedEvents(), | ||
); | ||
} | ||
|
||
public function testOnProductShow(): void | ||
{ | ||
$request = Request::create('/'); | ||
$this->requestStack->push($request); | ||
|
||
$product = new Product(42); | ||
$event = new ResourceControllerEvent($product); | ||
$this->listener->onProductShow($event); | ||
|
||
self::assertSame($product, $request->attributes->get('nglayouts_sylius_product')); | ||
|
||
self::assertTrue($this->context->has('sylius_product_id')); | ||
self::assertSame(42, $this->context->get('sylius_product_id')); | ||
} | ||
|
||
public function testOnProductShowWithoutProduct(): void | ||
{ | ||
$request = Request::create('/'); | ||
$this->requestStack->push($request); | ||
|
||
$taxon = new Taxon(42); | ||
$event = new ResourceControllerEvent($taxon); | ||
$this->listener->onProductShow($event); | ||
|
||
self::assertFalse($request->attributes->has('nglayouts_sylius_product')); | ||
self::assertFalse($this->context->has('sylius_product_id')); | ||
} | ||
} |