Skip to content

Commit 48cf2b4

Browse files
committed
Refactored the API endpoints and client
1 parent 65829b4 commit 48cf2b4

File tree

120 files changed

+2698
-763
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+2698
-763
lines changed

composer.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"autoload-dev": {
2222
"psr-4": {
23+
"spec\\Diglin\\Sylius\\ApiClient\\": "spec/",
2324
"Diglin\\Sylius\\ApiClient\\tests\\": "tests/"
2425
}
2526
},
@@ -33,12 +34,13 @@
3334
"php-http/message-factory": "^v1.0",
3435
"php-http/multipart-stream-builder": "^1.0",
3536
"php-http/client-implementation": "^1.0",
36-
"symfony/expression-language": "^3.0|^4.0|^5.0"
37+
"symfony/expression-language": "^3.0|^4.0|^5.0",
38+
"webmozart/assert": "^1.10"
3739
},
3840
"require-dev": {
3941
"friendsofphp/php-cs-fixer": "^2.14",
40-
"phpunit/phpunit": "^5.7",
41-
"phpspec/phpspec": "^5.0",
42+
"phpunit/phpunit": "^9.0",
43+
"phpspec/phpspec": "^7.1",
4244
"symfony/yaml": "^4.2",
4345
"donatj/mock-webserver": "^2.0",
4446
"php-http/guzzle6-adapter": "^2.0"

spec/Api/AuthenticationApiSpec.php renamed to spec/Api/Legacy/AuthenticationApiSpec.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace spec\Diglin\Sylius\ApiClient\Api;
3+
namespace spec\Diglin\Sylius\ApiClient\Api\Legacy;
44

5-
use Diglin\Sylius\ApiClient\Api\AuthenticationApi;
6-
use Diglin\Sylius\ApiClient\Api\AuthenticationApiInterface;
5+
use Diglin\Sylius\ApiClient\Api\Legacy\AuthenticationApi;
6+
use Diglin\Sylius\ApiClient\Api\Legacy\AuthenticationApiInterface;
77
use Diglin\Sylius\ApiClient\Client\HttpClient;
88
use Diglin\Sylius\ApiClient\Routing\UriGeneratorInterface;
99
use PhpSpec\ObjectBehavior;

spec/Client/AuthenticatedHttpClientSpec.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace spec\Diglin\Sylius\ApiClient\Client;
44

5-
use Diglin\Sylius\ApiClient\Api\AuthenticationApiInterface;
5+
use Diglin\Sylius\ApiClient\Api\Authentication\AuthenticationApiInterface;
66
use Diglin\Sylius\ApiClient\Client\AuthenticatedHttpClient;
77
use Diglin\Sylius\ApiClient\Client\HttpClient;
88
use Diglin\Sylius\ApiClient\Client\HttpClientInterface;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace spec\Diglin\Sylius\ApiClient\Client;
4+
5+
use Diglin\Sylius\ApiClient\Api\Legacy\AuthenticationApiInterface;
6+
use Diglin\Sylius\ApiClient\Client\HttpClient;
7+
use Diglin\Sylius\ApiClient\Client\HttpClientInterface;
8+
use Diglin\Sylius\ApiClient\Client\LegacyAuthenticatedHttpClient;
9+
use Diglin\Sylius\ApiClient\Exception\UnauthorizedHttpException;
10+
use Diglin\Sylius\ApiClient\Security\LegacyAuthentication;
11+
use PhpSpec\ObjectBehavior;
12+
use Psr\Http\Message\ResponseInterface;
13+
14+
class LegacyAuthenticatedHttpClientSpec extends ObjectBehavior
15+
{
16+
public function let(
17+
HttpClient $httpClient,
18+
AuthenticationApiInterface $authenticationApi,
19+
LegacyAuthentication $authentication
20+
) {
21+
$this->beConstructedWith($httpClient, $authenticationApi, $authentication);
22+
}
23+
24+
public function it_is_initializable()
25+
{
26+
$this->shouldHaveType(LegacyAuthenticatedHttpClient::class);
27+
$this->shouldImplement(HttpClientInterface::class);
28+
}
29+
30+
public function it_sends_an_authenticated_and_successful_request_when_access_token_is_defined(
31+
$httpClient,
32+
$authentication,
33+
ResponseInterface $response
34+
) {
35+
$authentication->getAccessToken()->willReturn('bar');
36+
37+
$httpClient->sendRequest(
38+
'POST',
39+
'http://diglin.com/api/rest/v1/products/foo',
40+
['Content-Type' => 'application/json', 'Authorization' => 'Bearer bar'],
41+
'{"identifier": "foo"}'
42+
)->willReturn($response);
43+
44+
$this->sendRequest(
45+
'POST',
46+
'http://diglin.com/api/rest/v1/products/foo',
47+
['Content-Type' => 'application/json'],
48+
'{"identifier": "foo"}'
49+
)->shouldReturn($response);
50+
}
51+
52+
public function it_sends_an_authenticated_and_successful_request_at_first_call(
53+
$httpClient,
54+
$authenticationApi,
55+
$authentication,
56+
ResponseInterface $response
57+
) {
58+
$authentication->getClientId()->willReturn('client_id');
59+
$authentication->getSecret()->willReturn('secret');
60+
$authentication->getUsername()->willReturn('julia');
61+
$authentication->getPassword()->willReturn('julia_pwd');
62+
$authentication->getAccessToken()->willReturn(null, 'foo');
63+
64+
$authenticationApi
65+
->authenticateByPassword('client_id', 'secret', 'julia', 'julia_pwd')
66+
->willReturn([
67+
'access_token' => 'foo',
68+
'expires_in' => 3600,
69+
'token_type' => 'bearer',
70+
'scope' => null,
71+
'refresh_token' => 'bar',
72+
])
73+
;
74+
75+
$authentication
76+
->setAccessToken('foo')
77+
->shouldBeCalled()
78+
->willReturn($authentication)
79+
;
80+
81+
$authentication
82+
->setRefreshToken('bar')
83+
->shouldBeCalled()
84+
->willReturn($authentication)
85+
;
86+
87+
$httpClient->sendRequest(
88+
'POST',
89+
'http://diglin.com/api/rest/v1/products/foo',
90+
['Content-Type' => 'application/json', 'Authorization' => 'Bearer foo'],
91+
'{"identifier": "foo"}'
92+
)->willReturn($response);
93+
94+
$this->sendRequest(
95+
'POST',
96+
'http://diglin.com/api/rest/v1/products/foo',
97+
['Content-Type' => 'application/json'],
98+
'{"identifier": "foo"}'
99+
)->shouldReturn($response);
100+
}
101+
102+
public function it_sends_an_authenticated_and_successful_request_when_access_token_expired(
103+
$httpClient,
104+
$authenticationApi,
105+
$authentication,
106+
ResponseInterface $response
107+
) {
108+
$authentication->getClientId()->willReturn('client_id');
109+
$authentication->getSecret()->willReturn('secret');
110+
$authentication->getUsername()->willReturn('julia');
111+
$authentication->getPassword()->willReturn('julia_pwd');
112+
$authentication->getAccessToken()->willReturn('foo', 'foo', 'baz');
113+
$authentication->getRefreshToken()->willReturn('bar');
114+
115+
$httpClient->sendRequest(
116+
'POST',
117+
'http://diglin.com/api/rest/v1/products/foo',
118+
['Content-Type' => 'application/json', 'Authorization' => 'Bearer foo'],
119+
'{"identifier": "foo"}'
120+
)->willThrow(UnauthorizedHttpException::class);
121+
122+
$authenticationApi
123+
->authenticateByRefreshToken('client_id', 'secret', 'bar')
124+
->willReturn([
125+
'access_token' => 'baz',
126+
'expires_in' => 3600,
127+
'token_type' => 'bearer',
128+
'scope' => null,
129+
'refresh_token' => 'foz',
130+
])
131+
;
132+
133+
$authentication
134+
->setAccessToken('baz')
135+
->shouldBeCalled()
136+
->willReturn($authentication)
137+
;
138+
139+
$authentication
140+
->setRefreshToken('foz')
141+
->shouldBeCalled()
142+
->willReturn($authentication)
143+
;
144+
145+
$httpClient->sendRequest(
146+
'POST',
147+
'http://diglin.com/api/rest/v1/products/foo',
148+
['Content-Type' => 'application/json', 'Authorization' => 'Bearer baz'],
149+
'{"identifier": "foo"}'
150+
)->willReturn($response);
151+
152+
$this->sendRequest(
153+
'POST',
154+
'http://diglin.com/api/rest/v1/products/foo',
155+
['Content-Type' => 'application/json'],
156+
'{"identifier": "foo"}'
157+
);
158+
}
159+
}

spec/Routing/UriGeneratorSpec.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function it_generates_uri_having_search_parameter_encoded_in_json()
8585

8686
$this
8787
->generate('/api', [], $queryParameters)
88-
->shouldReturn(static::BASE_URI.'api?search=%7B%22categories%22%3A%5B%7B%22operator%22%3A%22IN%22%2C%22value%22%3A%22master%22%7D%5D%7D')
88+
->shouldReturn(static::BASE_URI.'api?search%5Bcategories%5D%5B0%5D%5Boperator%5D=IN&search%5Bcategories%5D%5B0%5D%5Bvalue%5D=master')
8989
;
9090
}
9191
}

spec/SyliusClientSpec.php renamed to spec/SyliusLegacyClientSpec.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace spec\Diglin\Sylius\ApiClient;
44

5-
use Diglin\Sylius\ApiClient\Api;
5+
use Diglin\Sylius\ApiClient\Api\Legacy as Api;
66
use Diglin\Sylius\ApiClient\Security\Authentication;
7-
use Diglin\Sylius\ApiClient\SyliusClient;
8-
use Diglin\Sylius\ApiClient\SyliusClientInterface;
7+
use Diglin\Sylius\ApiClient\SyliusAdminClient;
8+
use Diglin\Sylius\ApiClient\SyliusAdminClientInterface;
99
use PhpSpec\ObjectBehavior;
1010

11-
class SyliusClientSpec extends ObjectBehavior
11+
class SyliusLegacyClientSpec extends ObjectBehavior
1212
{
1313
public function let(
1414
Authentication $authentication,
@@ -70,8 +70,8 @@ public function let(
7070

7171
public function it_is_initializable()
7272
{
73-
$this->shouldImplement(SyliusClientInterface::class);
74-
$this->shouldHaveType(SyliusClient::class);
73+
$this->shouldImplement(SyliusAdminClientInterface::class);
74+
$this->shouldHaveType(SyliusAdminClient::class);
7575
}
7676

7777
public function it_gets_access_token($authentication)

src/Api/Admin/AddressApi.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Diglin\Sylius\ApiClient\Api\Admin;
4+
5+
use Diglin\Sylius\ApiClient\Client\ResourceClientInterface;
6+
use Webmozart\Assert\Assert;
7+
8+
final class AddressApi implements AddressApiInterface
9+
{
10+
public function __construct(
11+
private ResourceClientInterface $resourceClient,
12+
) {}
13+
14+
public function get($code): array
15+
{
16+
Assert::integer($code);
17+
return $this->resourceClient->getResource('api/v2/admin/addresses/%d', [$code]);
18+
}
19+
}

src/Api/Admin/AddressApiInterface.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Diglin\Sylius\ApiClient\Api\Admin;
4+
5+
use Diglin\Sylius\ApiClient\Api\Operation\GettableResourceInterface;
6+
7+
interface AddressApiInterface extends GettableResourceInterface
8+
{
9+
}

src/Api/Admin/AdjustmentApi.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Diglin\Sylius\ApiClient\Api\Admin;
4+
5+
use Diglin\Sylius\ApiClient\Client\ResourceClientInterface;
6+
use Diglin\Sylius\ApiClient\Filter\FilterBuilderInterface;
7+
use Diglin\Sylius\ApiClient\Pagination\PageFactoryInterface;
8+
use Diglin\Sylius\ApiClient\Pagination\PageInterface;
9+
use Diglin\Sylius\ApiClient\Pagination\ResourceCursorFactoryInterface;
10+
use Diglin\Sylius\ApiClient\Pagination\ResourceCursorInterface;
11+
use Diglin\Sylius\ApiClient\Sort\SortBuilderInterface;
12+
use Webmozart\Assert\Assert;
13+
14+
final class AdjustmentApi implements AdjustmentApiInterface
15+
{
16+
public function __construct(
17+
private ResourceClientInterface $resourceClient,
18+
private PageFactoryInterface $pageFactory,
19+
private ResourceCursorFactoryInterface $cursorFactory,
20+
) {}
21+
22+
public function get($code): array
23+
{
24+
Assert::integer($code);
25+
return $this->resourceClient->getResource('api/v2/admin/adjustments/%d', [$code]);
26+
}
27+
28+
public function listPerPage(
29+
$code,
30+
int $limit = 10,
31+
array $queryParameters = [],
32+
FilterBuilderInterface $filterBuilder = null,
33+
SortBuilderInterface $sortBuilder = null
34+
): PageInterface {
35+
Assert::string($code);
36+
$data = $this->resourceClient->getResources('api/v2/admin/order-items/%s/adjustments', [$code]);
37+
38+
return $this->pageFactory->createPage($data);
39+
}
40+
41+
public function all(
42+
$code,
43+
int $pageSize = 10,
44+
array $queryParameters = [],
45+
FilterBuilderInterface $filterBuilder = null,
46+
SortBuilderInterface $sortBuilder = null
47+
): ResourceCursorInterface {
48+
$data = $this->listPerPage($code, $pageSize, $queryParameters, $filterBuilder, $sortBuilder);
49+
50+
return $this->cursorFactory->createCursor($pageSize, $data);
51+
}
52+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Diglin\Sylius\ApiClient\Api\Admin;
4+
5+
use Diglin\Sylius\ApiClient\Api\Operation\GettableResourceInterface;
6+
use Diglin\Sylius\ApiClient\Api\Operation\ListableDoubleResourceInterface;
7+
8+
interface AdjustmentApiInterface extends GettableResourceInterface, ListableDoubleResourceInterface
9+
{
10+
}

0 commit comments

Comments
 (0)