Skip to content

Commit

Permalink
Move tests inside src so contexts can be reusable in other projects
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuczen committed Nov 23, 2021
1 parent 022bace commit 42269de
Show file tree
Hide file tree
Showing 28 changed files with 270 additions and 191 deletions.
2 changes: 1 addition & 1 deletion behat.yml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
imports:
- vendor/sylius/sylius/src/Sylius/Behat/Resources/config/suites.yml
- tests/Behat/Resources/suites.yml
- src/Behat/Resources/suites.yml

default:
extensions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

declare(strict_types=1);

namespace Tests\BitBag\SyliusGraphqlPlugin\Behat\Client;
namespace BitBag\SyliusGraphqlPlugin\Behat\Client;

use BitBag\SyliusGraphqlPlugin\Behat\Model\OperationRequest;
use BitBag\SyliusGraphqlPlugin\Behat\Model\OperationRequestInterface;
use Exception;
use const JSON_ERROR_NONE;
use RecursiveArrayIterator;
Expand All @@ -21,8 +23,6 @@
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Tests\BitBag\SyliusGraphqlPlugin\Behat\Model\OperationRequest;
use Tests\BitBag\SyliusGraphqlPlugin\Behat\Model\OperationRequestInterface;
use Webmozart\Assert\Assert;

final class GraphqlClient implements GraphqlClientInterface
Expand Down Expand Up @@ -101,7 +101,10 @@ public function prepareQuery(

public function getLastOperationRequest(): ?OperationRequestInterface
{
return $this->sharedStorage->get(self::GRAPHQL_OPERATION);
/** @var OperationRequestInterface|null $operation */
$operation = $this->sharedStorage->get(self::GRAPHQL_OPERATION);

return $operation;
}

public function addAuthorization(): void
Expand All @@ -125,6 +128,7 @@ public function send(): Response
$this->client->setServerParameter('HTTP_HOST', (string) $this->sharedStorage->get('hostname'));
}

/** @var OperationRequest $operation */
$operation = $this->getLastOperationRequest();

$this->sendJsonRequest(
Expand All @@ -149,12 +153,16 @@ public function saveLastResponse(Response $response): void

public function getLastResponse(): ?JsonResponse
{
return $this->sharedStorage->get(self::LAST_GRAPHQL_RESPONSE);
/** @var JsonResponse|null $jsonResponse */
$jsonResponse = $this->sharedStorage->get(self::LAST_GRAPHQL_RESPONSE);

return $jsonResponse;
}

/** @throws Exception */
/** @throws Exception */
public function getLastResponseArrayContent(): array
{
/** @var JsonResponse $response */
$response = $this->getLastResponse();
$json = $this->getJsonFromResponse($response);
if ($json === null) {
Expand All @@ -169,6 +177,8 @@ public function getJsonFromResponse(Response $response): ?array
$content = $response->getContent();
Assert::string($content);

$jsonData = [];

try {
/** @var array $jsonData */
$jsonData = json_decode($content, true);
Expand All @@ -183,23 +193,26 @@ public function getJsonFromResponse(Response $response): ?array
return null;
}

/** @throws Exception */
/** @throws Exception */
public function flattenArray(array $responseArray): array
{
$this->checkIfResponseProperlyFormatted($responseArray);
$array = [];

if ($this->isDataSectionPresentInResponse($responseArray)) {
Assert::isArray($responseArray['data']);
/** @var array $array */
$array = reset($responseArray['data']);
}

if ($this->isErrorSectionPresentInResponse($responseArray)) {
Assert::isArray($responseArray['errors']);
/** @var array $array */
$array = reset($responseArray['errors']);
}
$recursiveIteratorIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array), RecursiveIteratorIterator::CHILD_FIRST);
$result = [];
/** @psalm-suppress MixedAssignment */
foreach ($recursiveIteratorIterator as $value) {
$keys = [];
foreach (range(0, $recursiveIteratorIterator->getDepth()) as $depth) {
Expand All @@ -220,7 +233,9 @@ private function sendJsonRequest(
bool $changeHistory = true
): Crawler {
$content = json_encode($parameters);
Assert::string($content);
$this->setJsonServerHeaders();

return $this->client->request($method, $uri, [], [], $server, $content, $changeHistory);
}

Expand All @@ -235,9 +250,6 @@ public function getValueAtKey(string $key)
$flatResponse = $this->flattenArray($arrayContent);

if (!array_key_exists($key, $flatResponse)) {
$message = array_key_exists('debugMessage', $flatResponse) ? $flatResponse['debugMessage'] : $flatResponse;
$trace = array_key_exists('trace', $flatResponse) ? $flatResponse['trace'] : $flatResponse;

throw new Exception(
sprintf(
"Last response did not have any key named %s \n%s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

declare(strict_types=1);

namespace Tests\BitBag\SyliusGraphqlPlugin\Behat\Client;
namespace BitBag\SyliusGraphqlPlugin\Behat\Client;

use BitBag\SyliusGraphqlPlugin\Behat\Model\OperationRequestInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Tests\BitBag\SyliusGraphqlPlugin\Behat\Model\OperationRequestInterface;

interface GraphqlClientInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

declare(strict_types=1);

namespace Tests\BitBag\SyliusGraphqlPlugin\Behat\Context;
namespace BitBag\SyliusGraphqlPlugin\Behat\Context;

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClientInterface;
use BitBag\SyliusGraphqlPlugin\Behat\Model\OperationRequestInterface;
use Exception;
use Sylius\Behat\Service\SharedStorageInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Tests\BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClient;
use Tests\BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClientInterface;
use Tests\BitBag\SyliusGraphqlPlugin\Behat\Model\OperationRequestInterface;
use Webmozart\Assert\Assert;

/** Context for GraphQL. */
Expand All @@ -32,7 +30,7 @@ public function __construct(GraphqlClientInterface $client, SharedStorageInterfa
*/
public function getBooleanFromString(string $boolean): bool
{
return (bool)$boolean;
return (bool) $boolean;
}

/**
Expand Down Expand Up @@ -68,25 +66,6 @@ public function iShouldReceiveAJsonResponse(): void
}
}

/**
* @When I should see following response:
*
* @throws Exception
*/
public function iShouldSeeFollowingResponse(PyStringNode $json): bool
{
/** @var array $expected */
$expected = json_decode($json->getRaw(), true);
/** @var array $lastResponse */
$lastResponse = $this->sharedStorage->get(GraphqlClient::LAST_GRAPHQL_RESPONSE);
$result_array = $this->differ->diff($expected, $lastResponse);
if (empty($result_array)) {
return true;
}

throw new Exception('Expected response doest match last one');
}

/**
* @param mixed $value
* @When I set :key field to :value
Expand Down Expand Up @@ -122,16 +101,18 @@ public function iSetKeyFieldToPreviouslySavedValue(string $key, string $name, st
}

/**
* @var mixed
* @Then I set :sharedStorageKey object :propertyName property to :value
*
* @param mixed $value
*/
public function iSetObjectPropertyToValue(string $sharedStorageKey, string $propertyName, $value): void
{
try {
$storageValue = (array)$this->sharedStorage->get($sharedStorageKey);
$storageValue = (array) $this->sharedStorage->get($sharedStorageKey);
} catch (\InvalidArgumentException $e) {
$storageValue = [];
}
/** @psalm-suppress MixedAssignment */
$storageValue[$propertyName] = $value;
$this->sharedStorage->set($sharedStorageKey, $storageValue);
}
Expand Down Expand Up @@ -177,29 +158,30 @@ public function thatResponseShouldContainKeyWithValue(string $key, $value): void
/**
* @param mixed $value
*
* @return mixed
* @return bool|float|int|string|array
*/
private function castToType($value, string $type = null)
{
switch ($type) {
case 'bool':
$value = (bool)$value;
$value = (bool) $value;

break;
case 'float':
$value = (float)$value;
$value = (float) $value;

break;
case 'int':
$value = (int)$value;
$value = (int) $value;

break;
case 'string':
$value = (string)$value;
$value = (string) $value;

break;
default:
return $value;
$value = is_array($value) ? (array) $value: (string) $value;
break;
}

return $value;
Expand Down Expand Up @@ -230,13 +212,16 @@ public function iShouldSeeFollowingErrorMessage(string $message): bool
*/
public function iSaveValueAtKeyOfThisModelResponse(string $key, string $name, ?string $type = null): void
{
/** @psalm-suppress MixedAssignment */
$value = $this->client->getValueAtKey($key);
$value = $this->castToType($value, $type);
$this->sharedStorage->set($name, $value);
}

private function getJsonFromResponse(string $response): ?array
{
$jsonData = [];

try {
/** @var array $jsonData */
$jsonData = json_decode($response, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@

declare(strict_types=1);

namespace Tests\BitBag\SyliusGraphqlPlugin\Behat\Context\Shop;
namespace BitBag\SyliusGraphqlPlugin\Behat\Context\Shop;

use ApiPlatform\Core\Api\IriConverterInterface;
use Behat\Behat\Context\Context;
use BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClient;
use BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClientInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\Address;
use Sylius\Component\Core\Model\Customer;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Core\Repository\AddressRepositoryInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Tests\BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClient;
use Tests\BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClientInterface;
use Webmozart\Assert\Assert;

final class AddressContext implements Context
{
Expand Down Expand Up @@ -138,6 +139,8 @@ public function customerHasAnAddress(string $email): void
/** @var ShopUserInterface|null $user */
$user = $this->userRepository->findOneBy(['username' => $email]);

Assert::notNull($user);

/** @var Customer $customer */
$customer = $user->getCustomer();
$address = new Address();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

declare(strict_types=1);

namespace Tests\BitBag\SyliusGraphqlPlugin\Behat\Context\Shop;
namespace BitBag\SyliusGraphqlPlugin\Behat\Context\Shop;

use ApiPlatform\Core\Api\IriConverterInterface;
use Behat\Behat\Context\Context;
use BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClient;
use BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClientInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Tests\BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClient;
use Tests\BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClientInterface;
use Webmozart\Assert\Assert;

final class CartContext implements Context
Expand Down Expand Up @@ -256,7 +256,7 @@ public function iPrepareOperationChoosePredefinedAddressAsBilling(string $addres
/**
* @Then total price for items should equal to :price
*/
public function totalPriceForItemsShouldEqualTo(int $price)
public function totalPriceForItemsShouldEqualTo(int $price): void
{
$orderTotal = (int) $this->client->getValueAtKey('order.total');
$shippingTotal = (int) $this->client->getValueAtKey('order.shippingTotal');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

declare(strict_types=1);

namespace Tests\BitBag\SyliusGraphqlPlugin\Behat\Context\Shop;
namespace BitBag\SyliusGraphqlPlugin\Behat\Context\Shop;

use Behat\Behat\Context\Context;
use BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClient;
use BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClientInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Tests\BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClient;
use Tests\BitBag\SyliusGraphqlPlugin\Behat\Client\GraphqlClientInterface;

final class CheckoutContext implements Context
{
Expand Down
Loading

0 comments on commit 42269de

Please sign in to comment.