Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Commit b14d5e5

Browse files
authored
Merge pull request #13 from eerison/feature/post-put-delete
Added apiClient
2 parents 4f1c450 + 8d37dee commit b14d5e5

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,78 @@ it('can be used with snapshot')
6565
->toMatchJsonSnapshot();
6666
```
6767

68+
##Converting api platform test in pest
69+
70+
Before
71+
72+
```php
73+
<?php
74+
// api/tests/BooksTest.php
75+
76+
namespace App\Tests;
77+
78+
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
79+
use App\Entity\Book;
80+
81+
class BooksTest extends ApiTestCase
82+
{
83+
public function testGetCollection(): void
84+
{
85+
// The client implements Symfony HttpClient's `HttpClientInterface`, and the response `ResponseInterface`
86+
$response = static::createClient()->request('GET', '/books');
87+
88+
$this->assertResponseIsSuccessful();
89+
90+
// Asserts that the returned JSON is a superset of this one
91+
$this->assertJsonContains([
92+
'@context' => '/contexts/Book',
93+
'@id' => '/books',
94+
'@type' => 'hydra:Collection',
95+
'hydra:totalItems' => 100,
96+
'hydra:view' => [
97+
'@id' => '/books?page=1',
98+
'@type' => 'hydra:PartialCollectionView',
99+
'hydra:first' => '/books?page=1',
100+
'hydra:last' => '/books?page=4',
101+
'hydra:next' => '/books?page=2',
102+
],
103+
]);
104+
105+
// Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
106+
// This generated JSON Schema is also used in the OpenAPI spec!
107+
$this->assertMatchesResourceCollectionJsonSchema(Book::class);
108+
}
109+
```
110+
111+
After
112+
113+
```php
114+
use App\Entity\Book;
115+
116+
it('can get a collection')
117+
->get('/books')
118+
->assertResponseIsSuccessful()
119+
->expectResponseContent()
120+
->json()
121+
->toHaveKey('@context', '/contexts/Book')
122+
->toHaveKey('@id', '/books')
123+
->toHaveKey('@type', 'hydra:Collection')
124+
->toHaveKey('hydra:totalItems', 100)
125+
->toHaveKey('hydra:view.@id', '/books?page=1')
126+
->toHaveKey('hydra:view.@type', 'hydra:PartialCollectionView')
127+
->toHaveKey('hydra:first', '/books?page=1')
128+
->toHaveKey('hydra:last', '/books?page=4')
129+
->toHaveKey('hydra:next', '/books?page=2')
130+
->toMatchesResourceCollectionJsonSchema(Book::class)
131+
;
132+
```
133+
68134
Expectations
69135
- `toMatchesResourceCollectionJsonSchema(Your::class)`
70136
- `toMatchesResourceItemJsonSchema(Your::class)`
71137

72138
Functions
139+
- `apiClient()`
73140
- `get()`
74141
- `post()`
75142
- `put()`

src/Autoload.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@
44

55
namespace Eerison\PestPluginApiPlatform;
66

7+
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
78
use Pest\Plugin;
89
use Symfony\Contracts\HttpClient\ResponseInterface;
910

1011
Plugin::uses(Resource::class);
1112
Plugin::uses(ResourceShortcuts::class);
1213

14+
/**
15+
* @param array<string> $kernelOptions
16+
* @param array<string> $defaultOptions
17+
*/
18+
function apiClient(array $kernelOptions = [], array $defaultOptions = []): Client
19+
{
20+
return test()->apiClient($kernelOptions, $defaultOptions);
21+
}
22+
1323
/**
1424
* @param array<string> $options
1525
*/

src/Resource.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Eerison\PestPluginApiPlatform;
66

7+
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
78
use PHPUnit\Framework\TestCase;
89
use Symfony\Component\HttpFoundation\Request;
910
use Symfony\Contracts\HttpClient\ResponseInterface;
@@ -15,6 +16,15 @@ trait Resource
1516
{
1617
private ResponseInterface $response;
1718

19+
/**
20+
* @param array<string> $kernelOptions
21+
* @param array<string> $defaultOptions
22+
*/
23+
public function apiClient(array $kernelOptions = [], array $defaultOptions = []): Client
24+
{
25+
return ApiPlatform::createApiClient($kernelOptions, $defaultOptions);
26+
}
27+
1828
public function get(string $url, array $options = []): TestCase
1929
{
2030
$this->response = ApiPlatform::createApiClient()->request(Request::METHOD_GET, $url, $options);

tests/ResourceResponseTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use function Eerison\PestPluginApiPlatform\apiClient;
34
use function Eerison\PestPluginApiPlatform\get;
45

56
it('is checking the body structure')
@@ -25,3 +26,15 @@
2526
->toHaveKey('company.address')
2627
;
2728
});
29+
30+
it('can use apiClient', function () {
31+
$apiClient = apiClient();
32+
33+
$response = $apiClient->request('GET', '/foo/response/200');
34+
$content = $response->getContent();
35+
36+
expect($content)
37+
->json()
38+
->toHaveKey('company.address')
39+
;
40+
});

0 commit comments

Comments
 (0)