Skip to content

Commit

Permalink
[Locale] refactor get locale responses
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed Jul 7, 2021
1 parent 4699cf1 commit 996343d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 22 deletions.
6 changes: 3 additions & 3 deletions config/packages/lexik_jwt_authentication.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lexik_jwt_authentication:
private_key_path: '%kernel.project_dir%/config/jwt/private-test.pem'
public_key_path: '%kernel.project_dir%/config/jwt/public-test.pem'
pass_phrase: 'adam'
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
pass_phrase: '%env(JWT_PASSPHRASE)%'
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ Feature: Getting available locales in the current channel
And the "English (United States)" locale with code "en_US" should be available
And the "Polish (Poland)" locale with code "pl_PL" should be available
But the "German (Germany)" locale with code "de_DE" should not be available

@api
Scenario: Get locales details
When I get "English (United States)" locale
Then I should have "English (United States)" with code "en_US"
2 changes: 1 addition & 1 deletion features/locale/managing_locales/adding_locale.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: Adding a new locale
Background:
Given I am logged in as an administrator

@api
@ui @api
Scenario: Adding a new locale
Given I want to create a new locale
When I choose Norwegian
Expand Down
39 changes: 22 additions & 17 deletions src/Sylius/Behat/Context/Api/Shop/LocaleContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Webmozart\Assert\Assert;

final class LocaleContext implements Context
Expand All @@ -40,6 +41,14 @@ public function iGetAvailableLocales(): void
$this->client->index();
}

/**
* @When I get :locale locale
*/
public function iGetLocale(LocaleInterface $locale): void
{
$this->client->show($locale->getCode());
}

/**
* @Then I should have :count locales
*/
Expand All @@ -56,33 +65,29 @@ public function iShouldHaveLocales(int $count): void
*/
public function theLocaleWithCodeShouldBeAvailable(string $name, string $code): void
{
Assert::true($this->isLocaleWithNameAndCode(
$this->responseChecker->getCollection($this->client->getLastResponse()),
$name,
$code
));
Assert::true(
$this->responseChecker->hasItemWithValues($this->client->getLastResponse(), ['name' => $name, 'code' => $code])
);
}

/**
* @Then the :name locale with code :code should not be available
*/
public function theLocaleWithCodeShouldNotBeAvailable(string $name, string $code): void
{
Assert::false($this->isLocaleWithNameAndCode(
$this->responseChecker->getCollection($this->client->getLastResponse()),
$name,
$code
));
Assert::false(
$this->responseChecker->hasItemWithValues($this->client->getLastResponse(), ['name' => $name, 'code' => $code])
);
}

private function isLocaleWithNameAndCode(array $locales, string $name, string $code): bool
/**
* @Then I should have :name with code :code
*/
public function iShouldHaveWithCode(string $name, string $code): void
{
foreach ($locales as $locale) {
if ($locale['name'] === $name && $locale['code'] === $code) {
return true;
}
}
$response = $this->client->getLastResponse();

return false;
Assert::true($this->responseChecker->hasValue($response, 'name', $name));
Assert::true($this->responseChecker->hasValue($response, 'code', $code));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ default:
- sylius.behat.context.setup.locale

- sylius.behat.context.api.shop.locale

filters:
tags: "@locales && @api"
13 changes: 12 additions & 1 deletion tests/Api/Admin/LocalesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
final class LocalesTest extends JsonApiTestCase
{
/** @test */
public function it_gets_locales(): void
public function it_gets_locales_as_logged_in_administrator(): void
{
$this->loadFixturesFromFiles(['cart.yaml', 'authentication/api_administrator.yaml']);

Expand Down Expand Up @@ -49,4 +49,15 @@ public function it_gets_locales(): void

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

/** @test */
public function it_denies_access_to_a_locales_list_for_not_authenticated_user(): void
{
$this->loadFixturesFromFiles(['cart.yaml', 'authentication/api_administrator.yaml']);

$this->client->request('GET', '/api/v2/admin/locales');

$response = $this->client->getResponse();
$this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getStatusCode());
}
}
7 changes: 7 additions & 0 deletions tests/Api/Responses/Expected/shop/get_locale_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"@context":"\/api\/v2\/contexts\/Locale",
"@id":"\/api\/v2\/shop\/locales\/en_US",
"@type":"Locale",
"code":"en_US",
"name":"English (United States)"
}
11 changes: 11 additions & 0 deletions tests/Api/Shop/LocalesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,15 @@ public function it_gets_locales(): void

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

/** @test */
public function it_gets_locale(): void
{
$this->loadFixturesFromFiles(['cart.yaml']);

$this->client->request('GET', '/api/v2/shop/locales/en_US', [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();

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

0 comments on commit 996343d

Please sign in to comment.