Skip to content

Commit

Permalink
Add channel support (closes #2) (#14)
Browse files Browse the repository at this point in the history
Add channel aware support for product provider (closes #2)
  • Loading branch information
stefandoorn authored Jul 11, 2017
1 parent 1fe8e2d commit dee0134
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 25 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 27 additions & 8 deletions spec/SitemapPlugin/Provider/ProductUrlProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
Expand All @@ -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();
Expand All @@ -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);

Expand Down
26 changes: 21 additions & 5 deletions src/Provider/ProductUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +20,7 @@
final class ProductUrlProvider implements UrlProviderInterface
{
/**
* @var ProductRepositoryInterface
* @var ProductRepositoryInterface|EntityRepository
*/
private $productRepository;

Expand All @@ -37,6 +39,11 @@ final class ProductUrlProvider implements UrlProviderInterface
*/
private $localeContext;

/**
* @var ChannelContextInterface
*/
private $channelContext;

/**
* @var array
*/
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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();
}
}
5 changes: 3 additions & 2 deletions src/Resources/config/services/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@
<argument type="service" id="sylius.repository.product" />
<argument type="service" id="router" />
<argument type="service" id="sylius.sitemap_url_factory" />
<argument type="service" id="sylius.context.locale"></argument>
<argument type="service" id="sylius.context.locale" />
<argument type="service" id="sylius.context.channel" />
<tag name="sylius.sitemap_provider" />
</service>

<service id="sylius.sitemap_provider.taxon" class="SitemapPlugin\Provider\TaxonUrlProvider" >
<argument type="service" id="sylius.repository.taxon" />
<argument type="service" id="router" />
<argument type="service" id="sylius.sitemap_url_factory" />
<argument type="service" id="sylius.context.locale"></argument>
<argument type="service" id="sylius.context.locale" />
<argument>%sylius.sitemap_exclude_taxon_root%</argument>
<tag name="sylius.sitemap_provider" />
</service>
Expand Down
2 changes: 1 addition & 1 deletion tests/Application/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ framework:
default_locale: "%locale%"
trusted_proxies: ~
session:
handler_id: ~
storage_id: session.storage.mock_file
test: ~

swiftmailer:
Expand Down
56 changes: 51 additions & 5 deletions tests/Controller/AbstractTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
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();
}
}
3 changes: 3 additions & 0 deletions tests/Controller/RelativeClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Tests\SitemapPlugin\Controller;

/**
* @author Stefan Doorn <[email protected]>
*/
trait RelativeClientTrait
{
/**
Expand Down
1 change: 1 addition & 0 deletions tests/Controller/SitemapAllControllerApiRelativeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions tests/Controller/SitemapAllControllerApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions tests/Controller/SitemapProductControllerApiLocalesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions tests/Controller/SitemapProductControllerApiRelativeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ public function setUpDatabase()
$product->setName('Test');
$product->setCode('test-code');
$product->setSlug('test');
$product->addChannel($this->channel);
$this->getEntityManager()->persist($product);

$product = new Product();
$product->setCurrentLocale('en_US');
$product->setName('Mock');
$product->setCode('mock-code');
$product->setSlug('mock');
$product->addChannel($this->channel);
$this->getEntityManager()->persist($product);

$product = new Product();
Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions tests/Controller/SitemapProductControllerApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public function setUpDatabase()
$product->setName('Test');
$product->setCode('test-code');
$product->setSlug('test');
$product->addChannel($this->channel);
$this->getEntityManager()->persist($product);

$product = new Product();
$product->setCurrentLocale('en_US');
$product->setName('Mock');
$product->setCode('mock-code');
$product->setSlug('mock');
$product->addChannel($this->channel);
$this->getEntityManager()->persist($product);

$product = new Product();
Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions tests/Controller/TearDownTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Tests\SitemapPlugin\Controller;

/**
* @author Stefan Doorn <[email protected]>
*/
trait TearDownTrait
{
public function tearDown()
Expand Down

0 comments on commit dee0134

Please sign in to comment.