Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Taxon] Remove channel code from taxon routes #468

Merged
merged 2 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
| `{channelCode}/products/*` | `products/*` |
| `{channelCode}/product-latest` | `product-latest` |
| `{channelCode}/taxon-products/*` | `taxon-products/*` |
| `{channelCode}/taxons/*` | `taxons/*` |

* The channel code has been added as a second argument to `AddProductReviewByCodeRequest`
and `AddProductReviewBySlugRequest` classes.
Expand Down
9 changes: 3 additions & 6 deletions doc/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,7 @@ paths:
items:
$ref: "#/definitions/Product"

/{channelCode}/taxons:
parameters:
- $ref: "#/parameters/ChannelCode"
/taxons:
get:
tags:
- "taxons"
Expand All @@ -680,9 +678,8 @@ paths:
type: "array"
items:
$ref: "#/definitions/Taxon"
/{channelCode}/taxons/{code}:
parameters:
- $ref: "#/parameters/ChannelCode"

/taxons/{code}:
get:
tags:
- "taxons"
Expand Down
50 changes: 14 additions & 36 deletions spec/Http/RequestBasedLocaleProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
namespace spec\Sylius\ShopApiPlugin\Http;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\ShopApiPlugin\Checker\ChannelExistenceCheckerInterface;
use Sylius\ShopApiPlugin\Exception\ChannelNotFoundException;
use Sylius\ShopApiPlugin\Http\RequestBasedLocaleProviderInterface;
use Sylius\ShopApiPlugin\Provider\SupportedLocaleProviderInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
Expand All @@ -17,71 +16,50 @@
final class RequestBasedLocaleProviderSpec extends ObjectBehavior
{
function let(
ChannelRepositoryInterface $channelRepository,
ChannelExistenceCheckerInterface $channelExistenceChecker,
ChannelContextInterface $channelContext,
SupportedLocaleProviderInterface $supportedLocaleProvider
): void {
$this->beConstructedWith($channelRepository, $channelExistenceChecker, $supportedLocaleProvider);
$this->beConstructedWith($channelContext, $supportedLocaleProvider);
}

function it_implements_request_based_locale_provider_interface(): void
{
$this->shouldImplement(RequestBasedLocaleProviderInterface::class);
}

function it_uses_channel_code_and_locale_provider_to_return_request_locale(
ChannelRepositoryInterface $channelRepository,
ChannelExistenceCheckerInterface $channelExistenceChecker,
function it_provides_a_locale_code(
ChannelContextInterface $channelContext,
SupportedLocaleProviderInterface $supportedLocaleProvider,
ChannelInterface $channel,
Request $request
): void {
$request->attributes = new ParameterBag(['channelCode' => 'WEB_US']);
$request->query = new ParameterBag(['locale' => 'fr_FR']);

$channelExistenceChecker->withCode('WEB_US')->shouldBeCalled();
$channelRepository->findOneByCode('WEB_US')->willReturn($channel);

$channelContext->getChannel()->willReturn($channel);
$supportedLocaleProvider->provide('fr_FR', $channel)->willReturn('fr_FR');

$this->getLocaleCode($request)->shouldReturn('fr_FR');
}

function it_provides_locale_code_even_if_it_is_not_defined_explicitly(
ChannelRepositoryInterface $channelRepository,
ChannelExistenceCheckerInterface $channelExistenceChecker,
function it_provides_a_locale_code_even_if_it_is_not_defined_explicitly(
ChannelContextInterface $channelContext,
SupportedLocaleProviderInterface $supportedLocaleProvider,
ChannelInterface $channel,
Request $request
): void {
$request->attributes = new ParameterBag(['channelCode' => 'WEB_US']);
$request->query = new ParameterBag([]);

$channelExistenceChecker->withCode('WEB_US')->shouldBeCalled();
$channelRepository->findOneByCode('WEB_US')->willReturn($channel);

$channelContext->getChannel()->willReturn($channel);
$supportedLocaleProvider->provide(null, $channel)->willReturn('en_US');

$this->getLocaleCode($request)->shouldReturn('en_US');
}

function it_throws_exception_if_channel_code_is_not_defined(Request $request): void
{
$request->attributes = new ParameterBag([]);

$this
->shouldThrow(\InvalidArgumentException::class)
->during('getLocaleCode', [$request])
;
}

function it_throws_exception_if_channel_with_given_code_does_not_exist(
Request $request,
ChannelExistenceCheckerInterface $channelExistenceChecker
function it_throws_an_exception_if_channel_cannot_be_resolved(
ChannelContextInterface $channelContext,
Request $request
): void {
$request->attributes = new ParameterBag(['channelCode' => 'WEB_PL']);

$channelExistenceChecker->withCode('WEB_PL')->willThrow(ChannelNotFoundException::class);
$channelContext->getChannel()->willThrow(ChannelNotFoundException::class);

$this
->shouldThrow(ChannelNotFoundException::class)
Expand Down
9 changes: 5 additions & 4 deletions src/Controller/Taxon/ShowTaxonDetailsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
use Sylius\ShopApiPlugin\Factory\Taxon\TaxonDetailsViewFactoryInterface;
use Sylius\ShopApiPlugin\Http\RequestBasedLocaleProviderInterface;
Expand Down Expand Up @@ -42,14 +43,14 @@ public function __construct(
public function __invoke(Request $request): Response
{
$code = $request->attributes->get('code');
$locale = $this->requestBasedLocaleProvider->getLocaleCode($request);

/** @var TaxonInterface|null $taxon */
$taxon = $this->taxonRepository->findOneBy(['code' => $code]);

if (null === $taxon) {
throw new NotFoundHttpException(sprintf('Taxon with code %s has not been found.', $code));
}

return $this->viewHandler->handle(View::create($this->taxonViewFactory->create($taxon, $locale), Response::HTTP_OK));
$localeCode = $this->requestBasedLocaleProvider->getLocaleCode($request);

return $this->viewHandler->handle(View::create($this->taxonViewFactory->create($taxon, $localeCode), Response::HTTP_OK));
}
}
10 changes: 5 additions & 5 deletions src/Controller/Taxon/ShowTaxonTreeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ public function __construct(

public function __invoke(Request $request): Response
{
$locale = $this->requestBasedLocaleProvider->getLocaleCode($request);
$localeCode = $this->requestBasedLocaleProvider->getLocaleCode($request);

$taxons = $this->taxonRepository->findRootNodes();
$taxonViews = [];

/** @var TaxonInterface $taxon */
foreach ($taxons as $taxon) {
$taxonViews[] = $this->buildTaxonView($taxon, $locale);
$taxonViews[] = $this->buildTaxonView($taxon, $localeCode);
}

return $this->viewHandler->handle(View::create($taxonViews, Response::HTTP_OK));
}

private function buildTaxonView(TaxonInterface $taxon, $locale): TaxonView
private function buildTaxonView(TaxonInterface $taxon, string $localeCode): TaxonView
{
$taxonView = $this->taxonViewFactory->create($taxon, $locale);
$taxonView = $this->taxonViewFactory->create($taxon, $localeCode);

foreach ($taxon->getChildren() as $childTaxon) {
$taxonView->children[] = $this->buildTaxonView($childTaxon, $locale);
$taxonView->children[] = $this->buildTaxonView($childTaxon, $localeCode);
}

return $taxonView;
Expand Down
19 changes: 19 additions & 0 deletions src/DependencyInjection/Compiler/ChannelContextPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Sylius\ShopApiPlugin\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class ChannelContextPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
// TODO: Remove after next Sylius release (1.6.0)
$container->removeDefinition('sylius.listener.user_impersonated');

$container->removeDefinition('sylius.context.channel.cached');
}
}
5 changes: 5 additions & 0 deletions src/Exception/ChannelNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

final class ChannelNotFoundException extends \InvalidArgumentException
{
public static function occur(): self
{
return new self('Channel has not been found.');
}

public static function withCode(string $channelCode): self
{
return new self(sprintf('Channel with code %s has not been found.', $channelCode));
Expand Down
28 changes: 8 additions & 20 deletions src/Http/RequestBasedLocaleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,33 @@

namespace Sylius\ShopApiPlugin\Http;

use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\ShopApiPlugin\Checker\ChannelExistenceCheckerInterface;
use Sylius\ShopApiPlugin\Exception\ChannelNotFoundException;
use Sylius\ShopApiPlugin\Provider\SupportedLocaleProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Webmozart\Assert\Assert;

final class RequestBasedLocaleProvider implements RequestBasedLocaleProviderInterface
{
/** @var ChannelRepositoryInterface */
private $channelRepository;

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

/** @var SupportedLocaleProviderInterface */
private $supportedLocaleProvider;

public function __construct(
ChannelRepositoryInterface $channelRepository,
ChannelExistenceCheckerInterface $channelExistenceChecker,
ChannelContextInterface $channelContext,
SupportedLocaleProviderInterface $supportedLocaleProvider
) {
$this->channelRepository = $channelRepository;
$this->channelExistenceChecker = $channelExistenceChecker;
$this->channelContext = $channelContext;
$this->supportedLocaleProvider = $supportedLocaleProvider;
}

/** @throws ChannelNotFoundException|\InvalidArgumentException */
/** @throws ChannelNotFoundException */
public function getLocaleCode(Request $request): string
{
Assert::true($request->attributes->has('channelCode'));

$channelCode = $request->attributes->get('channelCode');
$this->channelExistenceChecker->withCode($channelCode);

/** @var ChannelInterface $channel */
$channel = $this->channelRepository->findOneByCode($channelCode);
$channel = $this->channelContext->getChannel();

return $this->supportedLocaleProvider->provide($request->query->get('locale'), $channel);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sylius_shop_api_product_by_code:

sylius_shop_api_taxon:
resource: "@ShopApiPlugin/Resources/config/routing/taxon.yml"
prefix: /shop-api/{channelCode}
prefix: /shop-api

sylius_shop_api_register:
resource: "@ShopApiPlugin/Resources/config/routing/register.yml"
Expand Down
3 changes: 1 addition & 2 deletions src/Resources/config/services/http.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
</service>

<service id="sylius.shop_api_plugin.http.request_based_locale_provider" class="Sylius\ShopApiPlugin\Http\RequestBasedLocaleProvider">
<argument type="service" id="sylius.repository.channel" />
<argument type="service" id="sylius.shop_api_plugin.checker.channel_existence" />
<argument type="service" id="sylius.context.channel" />
<argument type="service" id="sylius.shop_api_plugin.provider.supported_locale_provider" />
</service>
</services>
Expand Down
9 changes: 9 additions & 0 deletions src/ShopApiPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
namespace Sylius\ShopApiPlugin;

use Sylius\Bundle\CoreBundle\Application\SyliusPluginTrait;
use Sylius\ShopApiPlugin\DependencyInjection\Compiler\ChannelContextPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

final class ShopApiPlugin extends Bundle
{
use SyliusPluginTrait;

public function build(ContainerBuilder $container): void
{
parent::build($container);

$container->addCompilerPass(new ChannelContextPass());
}
}
19 changes: 3 additions & 16 deletions tests/Controller/Taxon/TaxonShowDetailsApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function it_shows_summary_of_a_chosen_taxon_with_fallback_locale_from_cha
{
$this->loadFixturesFromFiles(['shop.yml']);

$this->client->request('GET', '/shop-api/WEB_GB/taxons/T_SHIRTS', [], [], self::CONTENT_TYPE_HEADER);
$this->client->request('GET', '/shop-api/taxons/T_SHIRTS', [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();

$this->assertResponse($response, 'taxon/one_of_taxons_response', Response::HTTP_OK);
Expand All @@ -29,7 +29,7 @@ public function it_shows_summary_of_a_chosen_taxon_with_strange_code(): void
{
$this->loadFixturesFromFiles(['shop.yml']);

$this->client->request('GET', '/shop-api/WEB_GB/taxons/de%3Flol%3Dxd%23boom?locale=en_GB', [], [], self::CONTENT_TYPE_HEADER);
$this->client->request('GET', '/shop-api/taxons/de%3Flol%3Dxd%23boom?locale=en_GB', [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();

$this->assertResponse($response, 'taxon/strange_taxon_code_response', Response::HTTP_OK);
Expand All @@ -42,22 +42,9 @@ public function it_shows_summary_of_a_chosen_taxon_in_different_language(): void
{
$this->loadFixturesFromFiles(['shop.yml']);

$this->client->request('GET', '/shop-api/WEB_GB/taxons/T_SHIRTS?locale=de_DE', [], [], self::CONTENT_TYPE_HEADER);
$this->client->request('GET', '/shop-api/taxons/T_SHIRTS?locale=de_DE', [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();

$this->assertResponse($response, 'taxon/german_one_of_taxons_response', Response::HTTP_OK);
}

/**
* @test
*/
public function it_does_not_show_taxon_details_in_non_existent_channel(): void
{
$this->loadFixturesFromFiles(['shop.yml']);

$this->client->request('GET', '/shop-api/SPACE_KLINGON/taxons/T_SHIRTS?locale=en_GB', [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();

$this->assertResponse($response, 'channel_has_not_been_found_response', Response::HTTP_NOT_FOUND);
}
}
18 changes: 2 additions & 16 deletions tests/Controller/Taxon/TaxonShowTreeApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function it_shows_tree_of_all_taxons_with_fallback_locale_from_channel():
{
$this->loadFixturesFromFiles(['shop.yml']);

$this->client->request('GET', '/shop-api/WEB_GB/taxons/', [], [], self::CONTENT_TYPE_HEADER);
$this->client->request('GET', '/shop-api/taxons/', [], [], self::CONTENT_TYPE_HEADER);

$response = $this->client->getResponse();

Expand All @@ -30,24 +30,10 @@ public function it_shows_tree_of_all_taxons_in_different_language(): void
{
$this->loadFixturesFromFiles(['shop.yml']);

$this->client->request('GET', '/shop-api/WEB_GB/taxons/?locale=de_DE', [], [], self::CONTENT_TYPE_HEADER);
$this->client->request('GET', '/shop-api/taxons/?locale=de_DE', [], [], self::CONTENT_TYPE_HEADER);

$response = $this->client->getResponse();

$this->assertResponse($response, 'taxon/german_all_taxons_response', Response::HTTP_OK);
}

/**
* @test
*/
public function it_does_not_show_taxons_tree_in_non_existent_channel(): void
{
$this->loadFixturesFromFiles(['shop.yml']);

$this->client->request('GET', '/shop-api/SPACE_KLINGON/taxons/', [], [], self::CONTENT_TYPE_HEADER);

$response = $this->client->getResponse();

$this->assertResponse($response, 'channel_has_not_been_found_response', Response::HTTP_NOT_FOUND);
}
}