From dee013481221f6dcff253fb56e4bfdba8bcea890 Mon Sep 17 00:00:00 2001 From: Stefan Doorn Date: Tue, 11 Jul 2017 21:05:13 +0200 Subject: [PATCH] Add channel support (closes #2) (#14) Add channel aware support for product provider (closes #2) --- .travis.yml | 4 -- .../Provider/ProductUrlProviderSpec.php | 35 +++++++++--- src/Provider/ProductUrlProvider.php | 26 +++++++-- src/Resources/config/services/sitemap.xml | 5 +- tests/Application/app/config/config.yml | 2 +- tests/Controller/AbstractTestController.php | 56 +++++++++++++++++-- tests/Controller/RelativeClientTrait.php | 3 + .../SitemapAllControllerApiRelativeTest.php | 1 + .../SitemapAllControllerApiTest.php | 1 + ...SitemapProductControllerApiLocalesTest.php | 18 ++++++ ...itemapProductControllerApiRelativeTest.php | 3 + .../SitemapProductControllerApiTest.php | 3 + tests/Controller/TearDownTrait.php | 3 + 13 files changed, 135 insertions(+), 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index 145fb004..07ce2f4a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,11 +18,7 @@ install: before_script: - (cd tests/Application && bin/console doctrine:schema:create --env=test --no-interaction) - - (cd tests/Application && bin/console sylius:fixtures:load --env=test --no-interaction) - - (cd tests/Application && bin/console assets:install --env=test --no-interaction) - (cd tests/Application && bin/console doctrine:schema:create --env=test_relative --no-interaction) - - (cd tests/Application && bin/console sylius:fixtures:load --env=test_relative --no-interaction) - - (cd tests/Application && bin/console assets:install --env=test_relative --no-interaction) script: - composer validate --strict --no-check-all diff --git a/spec/SitemapPlugin/Provider/ProductUrlProviderSpec.php b/spec/SitemapPlugin/Provider/ProductUrlProviderSpec.php index 09239325..b12dbd54 100644 --- a/spec/SitemapPlugin/Provider/ProductUrlProviderSpec.php +++ b/spec/SitemapPlugin/Provider/ProductUrlProviderSpec.php @@ -3,16 +3,18 @@ namespace spec\SitemapPlugin\Provider; use Doctrine\Common\Collections\Collection; +use Doctrine\ORM\AbstractQuery; +use Doctrine\ORM\QueryBuilder; use PhpSpec\ObjectBehavior; -use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository; use SitemapPlugin\Factory\SitemapUrlFactoryInterface; use SitemapPlugin\Model\ChangeFrequency; use SitemapPlugin\Model\SitemapUrlInterface; use SitemapPlugin\Provider\ProductUrlProvider; use SitemapPlugin\Provider\UrlProviderInterface; +use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository; +use Sylius\Component\Channel\Context\ChannelContextInterface; use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Core\Model\ProductTranslation; -use Sylius\Component\Core\Model\ProductTranslationInterface; use Sylius\Component\Locale\Context\LocaleContextInterface; use Symfony\Component\Routing\RouterInterface; @@ -22,9 +24,14 @@ */ final class ProductUrlProviderSpec extends ObjectBehavior { - function let(ProductRepository $repository, RouterInterface $router, SitemapUrlFactoryInterface $sitemapUrlFactory, LocaleContextInterface $localeContext) - { - $this->beConstructedWith($repository, $router, $sitemapUrlFactory, $localeContext); + function let( + ProductRepository $repository, + RouterInterface $router, + SitemapUrlFactoryInterface $sitemapUrlFactory, + LocaleContextInterface $localeContext, + ChannelContextInterface $channelContext + ) { + $this->beConstructedWith($repository, $router, $sitemapUrlFactory, $localeContext, $channelContext); } function it_is_initializable() @@ -49,11 +56,22 @@ function it_generates_urls( ProductInterface $product, ProductTranslation $productTranslation, SitemapUrlInterface $sitemapUrl, - \DateTime $now + \DateTime $now, + QueryBuilder $queryBuilder, + AbstractQuery $query ) { $localeContext->getLocaleCode()->willReturn('en_US'); - $repository->findBy(['enabled' => true])->willReturn($products); + $repository->createQueryBuilder('o')->willReturn($queryBuilder); + $queryBuilder->addSelect('translation')->willReturn($queryBuilder); + $queryBuilder->innerJoin('o.translations', 'translation')->willReturn($queryBuilder); + $queryBuilder->andWhere(':channel MEMBER OF o.channels')->willReturn($queryBuilder); + $queryBuilder->andWhere('o.enabled = :enabled')->willReturn($queryBuilder); + $queryBuilder->setParameter('channel', null)->willReturn($queryBuilder); + $queryBuilder->setParameter('enabled', true)->willReturn($queryBuilder); + $queryBuilder->getQuery()->willReturn($query); + $query->getResult()->willReturn($products); + $products->getIterator()->willReturn($iterator); $iterator->valid()->willReturn(true, false); $iterator->next()->shouldBeCalled(); @@ -72,7 +90,8 @@ function it_generates_urls( $productTranslation->getSlug()->willReturn('t-shirt'); $product->getTranslations()->willReturn($translations); - $router->generate('sylius_shop_product_show', ['slug' => 't-shirt', '_locale' => 'en_US'])->willReturn('http://sylius.org/en_US/products/t-shirt'); + $router->generate('sylius_shop_product_show', + ['slug' => 't-shirt', '_locale' => 'en_US'])->willReturn('http://sylius.org/en_US/products/t-shirt'); $router->generate($product, [], true)->willReturn('http://sylius.org/en_US/products/t-shirt'); $sitemapUrlFactory->createNew()->willReturn($sitemapUrl); diff --git a/src/Provider/ProductUrlProvider.php b/src/Provider/ProductUrlProvider.php index 77eb4aeb..48b9418e 100644 --- a/src/Provider/ProductUrlProvider.php +++ b/src/Provider/ProductUrlProvider.php @@ -4,6 +4,8 @@ use SitemapPlugin\Factory\SitemapUrlFactoryInterface; use SitemapPlugin\Model\ChangeFrequency; +use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; +use Sylius\Component\Channel\Context\ChannelContextInterface; use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Core\Model\ProductTranslationInterface; use Sylius\Component\Core\Repository\ProductRepositoryInterface; @@ -18,7 +20,7 @@ final class ProductUrlProvider implements UrlProviderInterface { /** - * @var ProductRepositoryInterface + * @var ProductRepositoryInterface|EntityRepository */ private $productRepository; @@ -37,6 +39,11 @@ final class ProductUrlProvider implements UrlProviderInterface */ private $localeContext; + /** + * @var ChannelContextInterface + */ + private $channelContext; + /** * @var array */ @@ -47,17 +54,20 @@ final class ProductUrlProvider implements UrlProviderInterface * @param RouterInterface $router * @param SitemapUrlFactoryInterface $sitemapUrlFactory * @param LocaleContextInterface $localeContext + * @param ChannelContextInterface $channelContext */ public function __construct( ProductRepositoryInterface $productRepository, RouterInterface $router, SitemapUrlFactoryInterface $sitemapUrlFactory, - LocaleContextInterface $localeContext + LocaleContextInterface $localeContext, + ChannelContextInterface $channelContext ) { $this->productRepository = $productRepository; $this->router = $router; $this->sitemapUrlFactory = $sitemapUrlFactory; $this->localeContext = $localeContext; + $this->channelContext = $channelContext; } /** @@ -104,8 +114,14 @@ public function generate() */ private function getProducts() { - return $this->productRepository->findBy([ - 'enabled' => true, - ]); + return $this->productRepository->createQueryBuilder('o') + ->addSelect('translation') + ->innerJoin('o.translations', 'translation') + ->andWhere(':channel MEMBER OF o.channels') + ->andWhere('o.enabled = :enabled') + ->setParameter('channel', $this->channelContext->getChannel()) + ->setParameter('enabled', true) + ->getQuery() + ->getResult(); } } diff --git a/src/Resources/config/services/sitemap.xml b/src/Resources/config/services/sitemap.xml index fb8ee805..ed66fab8 100644 --- a/src/Resources/config/services/sitemap.xml +++ b/src/Resources/config/services/sitemap.xml @@ -52,7 +52,8 @@ - + + @@ -60,7 +61,7 @@ - + %sylius.sitemap_exclude_taxon_root% diff --git a/tests/Application/app/config/config.yml b/tests/Application/app/config/config.yml index 2f860754..a7866ab5 100644 --- a/tests/Application/app/config/config.yml +++ b/tests/Application/app/config/config.yml @@ -25,7 +25,7 @@ framework: default_locale: "%locale%" trusted_proxies: ~ session: - handler_id: ~ + storage_id: session.storage.mock_file test: ~ swiftmailer: diff --git a/tests/Controller/AbstractTestController.php b/tests/Controller/AbstractTestController.php index c3616dc5..b7f09768 100644 --- a/tests/Controller/AbstractTestController.php +++ b/tests/Controller/AbstractTestController.php @@ -2,20 +2,66 @@ namespace Tests\SitemapPlugin\Controller; -use Lakion\ApiTestCase\ApiTestCase; use Lakion\ApiTestCase\XmlApiTestCase; +use Sylius\Component\Core\Model\Channel; +use Sylius\Component\Core\Model\ChannelInterface; +use Sylius\Component\Currency\Model\Currency; +use Sylius\Component\Currency\Model\CurrencyInterface; +use Sylius\Component\Locale\Model\Locale; +use Sylius\Component\Locale\Model\LocaleInterface; /** - * Class AbstractTestController - * @package Tests\SitemapPlugin\Controller + * @author Stefan Doorn */ abstract class AbstractTestController extends XmlApiTestCase { + /** + * @var ChannelInterface + */ + protected $channel; + + /** + * @var LocaleInterface + */ + protected $locale; + + /** + * @var CurrencyInterface + */ + protected $currency; + /** * @before */ - public function setUpClient() + public function setupDatabase() { - $this->client = static::createClient(array(), array()); + parent::setUpDatabase(); + + $this->locale = new Locale(); + $this->locale->setCode('en_US'); + + $this->getEntityManager()->persist($this->locale); + + $locale = new Locale(); + $locale->setCode('nl_NL'); + + $this->getEntityManager()->persist($locale); + + $this->currency = new Currency(); + $this->currency->setCode('USD'); + + $this->getEntityManager()->persist($this->currency); + + $this->channel = new Channel(); + $this->channel->setCode('US_WEB'); + $this->channel->setName('US Web Store'); + $this->channel->setDefaultLocale($this->locale); + $this->channel->setBaseCurrency($this->currency); + $this->channel->setTaxCalculationStrategy('order_items_based'); + + $this->channel->addLocale($this->locale); + + $this->getEntityManager()->persist($this->channel); + $this->getEntityManager()->flush(); } } diff --git a/tests/Controller/RelativeClientTrait.php b/tests/Controller/RelativeClientTrait.php index 9a63aeba..707bb213 100644 --- a/tests/Controller/RelativeClientTrait.php +++ b/tests/Controller/RelativeClientTrait.php @@ -2,6 +2,9 @@ namespace Tests\SitemapPlugin\Controller; +/** + * @author Stefan Doorn + */ trait RelativeClientTrait { /** diff --git a/tests/Controller/SitemapAllControllerApiRelativeTest.php b/tests/Controller/SitemapAllControllerApiRelativeTest.php index 864d6d87..1662d0e8 100644 --- a/tests/Controller/SitemapAllControllerApiRelativeTest.php +++ b/tests/Controller/SitemapAllControllerApiRelativeTest.php @@ -25,6 +25,7 @@ public function setUpDatabase() $product->setName('Test'); $product->setCode('test-code'); $product->setSlug('test'); + $product->addChannel($this->channel); $this->getEntityManager()->persist($product); $root = new Taxon(); diff --git a/tests/Controller/SitemapAllControllerApiTest.php b/tests/Controller/SitemapAllControllerApiTest.php index e887a426..af102c8e 100644 --- a/tests/Controller/SitemapAllControllerApiTest.php +++ b/tests/Controller/SitemapAllControllerApiTest.php @@ -24,6 +24,7 @@ public function setUpDatabase() $product->setName('Test'); $product->setCode('test-code'); $product->setSlug('test'); + $product->addChannel($this->channel); $this->getEntityManager()->persist($product); $root = new Taxon(); diff --git a/tests/Controller/SitemapProductControllerApiLocalesTest.php b/tests/Controller/SitemapProductControllerApiLocalesTest.php index 40426446..641dbeab 100644 --- a/tests/Controller/SitemapProductControllerApiLocalesTest.php +++ b/tests/Controller/SitemapProductControllerApiLocalesTest.php @@ -2,7 +2,10 @@ namespace Tests\SitemapPlugin\Controller; +use Sylius\Component\Core\Model\Channel; use Sylius\Component\Core\Model\Product; +use Sylius\Component\Core\Model\ProductTranslation; +use Sylius\Component\Locale\Model\Locale; /** * @author Stefan Doorn @@ -27,6 +30,7 @@ public function setUpDatabase() $product->setName('Test'); $product->setCode('test-code'); $product->setSlug('test'); + $product->addChannel($this->channel); $this->getEntityManager()->persist($product); $product = new Product(); @@ -38,6 +42,7 @@ public function setUpDatabase() $product->setName('Mock'); $product->setCode('mock-code'); $product->setSlug('mock'); + $product->addChannel($this->channel); $this->getEntityManager()->persist($product); $product = new Product(); @@ -50,6 +55,19 @@ public function setUpDatabase() $product->setCode('test-code-3'); $product->setSlug('test 2'); $product->setEnabled(false); + $product->addChannel($this->channel); + $this->getEntityManager()->persist($product); + + $product = new Product(); + $product->setCurrentLocale('en_US'); + $product->setName('Test 3'); + $product->setCode('test-code-4'); + $product->setSlug('test 3'); + $product->setCurrentLocale('nl_NL'); + $product->setName('Test 3'); + $product->setCode('test-code-4'); + $product->setSlug('test 3'); + $product->setEnabled(false); $this->getEntityManager()->persist($product); $this->getEntityManager()->flush(); diff --git a/tests/Controller/SitemapProductControllerApiRelativeTest.php b/tests/Controller/SitemapProductControllerApiRelativeTest.php index f9274385..ba687219 100644 --- a/tests/Controller/SitemapProductControllerApiRelativeTest.php +++ b/tests/Controller/SitemapProductControllerApiRelativeTest.php @@ -24,6 +24,7 @@ public function setUpDatabase() $product->setName('Test'); $product->setCode('test-code'); $product->setSlug('test'); + $product->addChannel($this->channel); $this->getEntityManager()->persist($product); $product = new Product(); @@ -31,6 +32,7 @@ public function setUpDatabase() $product->setName('Mock'); $product->setCode('mock-code'); $product->setSlug('mock'); + $product->addChannel($this->channel); $this->getEntityManager()->persist($product); $product = new Product(); @@ -39,6 +41,7 @@ public function setUpDatabase() $product->setCode('test-code-3'); $product->setSlug('test 2'); $product->setEnabled(false); + $product->addChannel($this->channel); $this->getEntityManager()->persist($product); $this->getEntityManager()->flush(); diff --git a/tests/Controller/SitemapProductControllerApiTest.php b/tests/Controller/SitemapProductControllerApiTest.php index 76780f99..14f49275 100644 --- a/tests/Controller/SitemapProductControllerApiTest.php +++ b/tests/Controller/SitemapProductControllerApiTest.php @@ -23,6 +23,7 @@ public function setUpDatabase() $product->setName('Test'); $product->setCode('test-code'); $product->setSlug('test'); + $product->addChannel($this->channel); $this->getEntityManager()->persist($product); $product = new Product(); @@ -30,6 +31,7 @@ public function setUpDatabase() $product->setName('Mock'); $product->setCode('mock-code'); $product->setSlug('mock'); + $product->addChannel($this->channel); $this->getEntityManager()->persist($product); $product = new Product(); @@ -38,6 +40,7 @@ public function setUpDatabase() $product->setCode('test-code-3'); $product->setSlug('test 2'); $product->setEnabled(false); + $product->addChannel($this->channel); $this->getEntityManager()->persist($product); $this->getEntityManager()->flush(); diff --git a/tests/Controller/TearDownTrait.php b/tests/Controller/TearDownTrait.php index 79839062..8e38c25e 100644 --- a/tests/Controller/TearDownTrait.php +++ b/tests/Controller/TearDownTrait.php @@ -2,6 +2,9 @@ namespace Tests\SitemapPlugin\Controller; +/** + * @author Stefan Doorn + */ trait TearDownTrait { public function tearDown()