Skip to content

Commit

Permalink
refactor Sylius#13896 [behat]Extract Request creation to separate ser…
Browse files Browse the repository at this point in the history
…vice (Ferror)

This PR was merged into the 1.12-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | none
| License         | MIT

It is a part of improving Developer Experience in Behat tests. I removed factory methods out of the Request class, as the object has really grown up. Moved these methods to separate Factory required a lot of changes, that's why I decided to postpone here and merge current changes.

Created Request Builder and Factory will provide an additional layer for future developers to build their own Requests

Commits
-------

77b515e Extract building and creating Request object outside of API client
  • Loading branch information
lchrusciel authored Apr 26, 2022
2 parents 6cbcaa8 + 77b515e commit 3d0c17d
Show file tree
Hide file tree
Showing 21 changed files with 612 additions and 443 deletions.
10 changes: 4 additions & 6 deletions src/Sylius/Behat/Client/ApiClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

interface ApiClientInterface
{
public function request(RequestInterface $request): Response;

public function index(string $resource): Response;

public function showByIri(string $iri): Response;
Expand All @@ -42,8 +44,6 @@ public function customItemAction(string $resource, string $id, string $type, str

public function customAction(string $url, string $method): Response;

public function upload(): Response;

public function resend(): Response;

public function executeCustomRequest(RequestInterface $request): Response;
Expand All @@ -52,8 +52,6 @@ public function buildCreateRequest(string $resource): void;

public function buildUpdateRequest(string $resource, string $id): void;

public function buildUploadRequest(string $resource): void;

public function setRequestData(array $data): void;

/** @param string|int $value */
Expand All @@ -66,8 +64,8 @@ public function clearParameters(): void;

public function addFile(string $key, UploadedFile $file): void;

/** @param string|int|array $value */
public function addRequestData(string $key, $value): void;
/** @param string|int|bool|array $value */
public function addRequestData(string $key, mixed $value): void;

public function setSubResourceData(string $key, array $data): void;

Expand Down
80 changes: 38 additions & 42 deletions src/Sylius/Behat/Client/ApiPlatformClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class ApiPlatformClient implements ApiClientInterface

private SharedStorageInterface $sharedStorage;

private RequestFactoryInterface $requestFactory;

private string $authorizationHeader;

private ?string $section;
Expand All @@ -34,47 +36,51 @@ final class ApiPlatformClient implements ApiClientInterface
public function __construct(
AbstractBrowser $client,
SharedStorageInterface $sharedStorage,
RequestFactoryInterface $requestFactory,
string $authorizationHeader,
?string $section = null
) {
$this->client = $client;
$this->sharedStorage = $sharedStorage;
$this->requestFactory = $requestFactory;
$this->authorizationHeader = $authorizationHeader;
$this->section = $section;
}

public function index(string $resource): Response
{
$this->request = Request::index($this->section, $resource, $this->authorizationHeader, $this->getToken());
$this->request = $this->requestFactory->index($this->section, $resource, $this->authorizationHeader, $this->getToken());

return $this->request($this->request);
}

public function showByIri(string $iri): Response
{
$request = Request::custom($iri, HttpRequest::METHOD_GET);
$request = $this->requestFactory->custom($iri, HttpRequest::METHOD_GET);
$request->authorize($this->getToken(), $this->authorizationHeader);

return $this->request($request);
}

public function subResourceIndex(string $resource, string $subResource, string $id): Response
{
$request = Request::subResourceIndex($this->section, $resource, $id, $subResource);
$request = $this->requestFactory->subResourceIndex($this->section, $resource, $id, $subResource);
$request->authorize($this->getToken(), $this->authorizationHeader);

return $this->request($request);
}

public function show(string $resource, string $id): Response
{
return $this->request(Request::show(
$this->section,
$resource,
$id,
$this->authorizationHeader,
$this->getToken()
));
return $this->request(
$this->requestFactory->show(
$this->section,
$resource,
$id,
$this->authorizationHeader,
$this->getToken()
)
);
}

public function create(?RequestInterface $request = null): Response
Expand All @@ -87,15 +93,22 @@ public function update(): Response
return $this->request($this->request);
}

public function resend(): Response
{
return $this->request($this->request);
}

public function delete(string $resource, string $id): Response
{
return $this->request(Request::delete(
$this->section,
$resource,
$id,
$this->authorizationHeader,
$this->getToken()
));
return $this->request(
$this->requestFactory->delete(
$this->section,
$resource,
$id,
$this->authorizationHeader,
$this->getToken()
)
);
}

public function filter(): Response
Expand All @@ -112,7 +125,7 @@ public function sort(array $sorting): Response

public function applyTransition(string $resource, string $id, string $transition, array $content = []): Response
{
$request = Request::transition($this->section, $resource, $id, $transition);
$request = $this->requestFactory->transition($this->section, $resource, $id, $transition);
$request->authorize($this->getToken(), $this->authorizationHeader);
$request->setContent($content);

Expand All @@ -121,31 +134,20 @@ public function applyTransition(string $resource, string $id, string $transition

public function customItemAction(string $resource, string $id, string $type, string $action): Response
{
$request = Request::customItemAction($this->section, $resource, $id, $type, $action);
$request = $this->requestFactory->customItemAction($this->section, $resource, $id, $type, $action);
$request->authorize($this->getToken(), $this->authorizationHeader);

return $this->request($request);
}

public function customAction(string $url, string $method): Response
{
$request = Request::custom($url, $method);

$request = $this->requestFactory->custom($url, $method);
$request->authorize($this->getToken(), $this->authorizationHeader);

return $this->request($request);
}

public function upload(): Response
{
return $this->request($this->request);
}

public function resend(): Response
{
return $this->request($this->request);
}

public function executeCustomRequest(RequestInterface $request): Response
{
$request->authorize($this->getToken(), $this->authorizationHeader);
Expand All @@ -155,15 +157,14 @@ public function executeCustomRequest(RequestInterface $request): Response

public function buildCreateRequest(string $resource): void
{
$this->request = Request::create($this->section, $resource, $this->authorizationHeader);
$this->request->authorize($this->getToken(), $this->authorizationHeader);
$this->request = $this->requestFactory->create($this->section, $resource, $this->authorizationHeader, $this->getToken());
}

public function buildUpdateRequest(string $resource, string $id): void
{
$this->show($resource, $id);

$this->request = Request::update(
$this->request = $this->requestFactory->update(
$this->section,
$resource,
$id,
Expand All @@ -175,7 +176,7 @@ public function buildUpdateRequest(string $resource, string $id): void

public function buildCustomUpdateRequest(string $resource, string $id, string $customSuffix): void
{
$this->request = Request::update(
$this->request = $this->requestFactory->update(
$this->section,
$resource,
sprintf('%s/%s', $id, $customSuffix),
Expand All @@ -184,11 +185,6 @@ public function buildCustomUpdateRequest(string $resource, string $id, string $c
);
}

public function buildUploadRequest(string $resource): void
{
$this->request = Request::upload($this->section, $resource, $this->authorizationHeader, $this->getToken());
}

/** @param string|int $value */
public function addParameter(string $key, $value): void
{
Expand Down Expand Up @@ -216,7 +212,7 @@ public function addFile(string $key, UploadedFile $file): void
$this->request->updateFiles([$key => $file]);
}

/** @param string|int|array $value */
/** @param string|int|bool|array $value */
public function addRequestData(string $key, $value): void
{
$this->request->updateContent([$key => $value]);
Expand Down Expand Up @@ -257,7 +253,7 @@ public function getToken(): ?string
return $this->sharedStorage->has('token') ? $this->sharedStorage->get('token') : null;
}

private function request(RequestInterface $request): Response
public function request(RequestInterface $request): Response
{
$this->setServerParameters();

Expand Down
36 changes: 36 additions & 0 deletions src/Sylius/Behat/Client/ContentTypeGuide.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Behat\Client;

use Symfony\Component\HttpFoundation\Request as HttpRequest;

class ContentTypeGuide implements ContentTypeGuideInterface
{
private const JSON_CONTENT_TYPE = 'application/json';
private const PATCH_CONTENT_TYPE = 'application/merge-patch+json';
private const LINKED_DATA_JSON_CONTENT_TYPE = 'application/ld+json';

public function guide(string $method): string
{
if ($method === HttpRequest::METHOD_PATCH) {
return self::PATCH_CONTENT_TYPE;
}

if ($method === HttpRequest::METHOD_PUT) {
return self::LINKED_DATA_JSON_CONTENT_TYPE;
}

return self::JSON_CONTENT_TYPE;
}
}
19 changes: 19 additions & 0 deletions src/Sylius/Behat/Client/ContentTypeGuideInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Behat\Client;

interface ContentTypeGuideInterface
{
public function guide(string $method): string;
}
Loading

0 comments on commit 3d0c17d

Please sign in to comment.