Skip to content

Commit

Permalink
Revert deleted files, add deprecation message
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkrz committed Jul 15, 2024
1 parent 385d588 commit 641509b
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 0 deletions.
64 changes: 64 additions & 0 deletions bundle/EventListener/Shop/ProductIndexListener.php
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());
}
}
43 changes: 43 additions & 0 deletions bundle/EventListener/Shop/ProductShowListener.php
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());
}
}
}
18 changes: 18 additions & 0 deletions bundle/Resources/config/services/event_listeners.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,21 @@ services:
- "@request_stack"
tags:
- { name: kernel.event_subscriber }

netgen_layouts.sylius.event_listener.shop.product_show:
class: Netgen\Bundle\LayoutsSyliusBundle\EventListener\Shop\ProductShowListener
arguments:
- "@request_stack"
- "@netgen_layouts.context"
tags:
- { name: kernel.event_subscriber }

netgen_layouts.sylius.event_listener.shop.product_index:
class: Netgen\Bundle\LayoutsSyliusBundle\EventListener\Shop\ProductIndexListener
arguments:
- "@sylius.repository.taxon"
- "@sylius.context.locale"
- "@request_stack"
- "@netgen_layouts.context"
tags:
- { name: kernel.event_subscriber }
132 changes: 132 additions & 0 deletions tests/bundle/EventListener/Shop/ProductIndexListenerTest.php
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 tests/bundle/EventListener/Shop/ProductShowListenerTest.php
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'));
}
}

0 comments on commit 641509b

Please sign in to comment.