-
-
Notifications
You must be signed in to change notification settings - Fork 963
Expand file tree
/
Copy pathToonTest.php
More file actions
202 lines (169 loc) · 7.48 KB
/
ToonTest.php
File metadata and controls
202 lines (169 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Laravel\Tests;
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
use HelgeSverre\Toon\Toon;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Workbench\App\Models\Author;
use Workbench\App\Models\Book;
use Workbench\Database\Factories\AuthorFactory;
use Workbench\Database\Factories\BookFactory;
class ToonTest extends TestCase
{
use ApiTestAssertionsTrait;
use RefreshDatabase;
use WithWorkbench;
/**
* @param Application $app
*/
protected function defineEnvironment($app): void
{
tap($app['config'], static function (Repository $config): void {
// Add Toon format as separate format (uses JSON-LD normalizers with Toon encoder)
$formats = $config->get('api-platform.formats', []);
$formats['toon'] = ['text/ld+toon'];
$formats['jsonld_toon'] = ['text/ld+toon']; // Explicitly add jsonld_toon
$formats['hydra_toon'] = ['text/ld+toon']; // Explicitly add hydra_toon
$formats['jsonapi_toon'] = ['text/vnd.api+toon']; // Explicitly add jsonapi_toon
$config->set('api-platform.formats', $formats);
$patchFormats = $config->get('api-platform.patch_formats', []);
$patchFormats['toon'] = ['text/ld+toon'];
$patchFormats['jsonld_toon'] = ['text/ld+toon']; // Explicitly add jsonld_toon
$patchFormats['hydra_toon'] = ['text/ld+toon']; // Explicitly add hydra_toon
$patchFormats['jsonapi_toon'] = ['text/vnd.api+toon']; // Explicitly add jsonapi_toon
$config->set('api-platform.patch_formats', $patchFormats);
$docsFormats = $config->get('api-platform.docs_formats', []);
$docsFormats['toon'] = ['text/ld+toon'];
$docsFormats['jsonld_toon'] = ['text/ld+toon']; // Explicitly add jsonld_toon
$docsFormats['hydra_toon'] = ['text/ld+toon']; // Explicitly add hydra_toon
$docsFormats['jsonapi_toon'] = ['text/vnd.api+toon']; // Explicitly add jsonapi_toon
$config->set('api-platform.docs_formats', $docsFormats);
$config->set('app.debug', true);
});
}
public function testGetEntrypoint(): void
{
$response = $this->get('/api/', ['accept' => ['text/ld+toon']]);
$response->assertStatus(200);
$response->assertHeader('content-type', 'text/ld+toon; charset=utf-8');
$content = $response->getContent();
// Decode the Toon content to check structure
$decoded = \HelgeSverre\Toon\Toon::decode($content);
$this->assertIsArray($decoded);
// Laravel entrypoint might have different structure, just check it's an array with resources
$this->assertNotEmpty($decoded);
// The response should contain book-related information
$contentLower = strtolower($content);
$this->assertTrue(
str_contains($contentLower, 'book') || str_contains($contentLower, 'api'),
'Entrypoint should contain resource information'
);
}
public function testGetCollection(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$response = $this->get('/api/books', ['accept' => 'text/ld+toon']);
$response->assertStatus(200);
$response->assertHeader('content-type', 'text/ld+toon; charset=utf-8');
$content = $response->getContent();
// Decode to verify structure
$decoded = \HelgeSverre\Toon\Toon::decode($content);
// Check for collection structure (Laravel doesn't use hydra prefix)
$this->assertIsArray($decoded);
// Check if it's a proper collection or a plain array
if (isset($decoded['@id'])) {
// Full collection structure
$this->assertArrayHasKey('totalItems', $decoded);
$this->assertEquals(10, $decoded['totalItems']);
$this->assertArrayHasKey('member', $decoded);
$this->assertCount(5, $decoded['member']); // Default page size
} else {
// Plain array of items
$this->assertIsArray($decoded);
$this->assertNotEmpty($decoded);
// Just verify we got items back
$this->assertGreaterThan(0, count($decoded));
}
}
public function testGetBook(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$book = Book::first();
$iri = $this->getIriFromResource($book);
$response = $this->get($iri, ['accept' => ['text/ld+toon']]);
$response->assertStatus(200);
$response->assertHeader('content-type', 'text/ld+toon; charset=utf-8');
$content = $response->getContent();
$this->assertStringContainsString('id:', $content);
$this->assertStringContainsString('name: '.$book->name, $content); // @phpstan-ignore-line
// ISBN may be quoted in output
$this->assertStringContainsString($book->isbn, $content); // @phpstan-ignore-line
}
public function testCreateBook(): void
{
AuthorFactory::new()->count(10)->create();
$author = Author::find(1);
$isbn = fake()->isbn13();
$response = $this->postJson(
'/api/books',
[
'name' => 'The Pragmatic Programmer',
'isbn' => $isbn,
'publicationDate' => fake()->optional()->date(),
'author' => $this->getIriFromResource($author),
],
[
'accept' => 'text/ld+toon',
'content-type' => 'application/ld+json',
]
);
$response->assertStatus(201);
$response->assertHeader('content-type', 'text/ld+toon; charset=utf-8');
$content = $response->getContent();
$this->assertStringContainsString('name: The Pragmatic Programmer', $content);
$this->assertStringContainsString('id:', $content);
}
public function testUpdateBook(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$book = Book::first();
$iri = $this->getIriFromResource($book);
$response = $this->patchJson(
$iri,
[
'name' => 'Updated Title',
],
[
'accept' => 'text/ld+toon',
'content-type' => 'application/merge-patch+json',
]
);
$response->assertStatus(200);
$response->assertHeader('content-type', 'text/ld+toon; charset=utf-8');
$content = $response->getContent();
$this->assertStringContainsString('name: Updated Title', $content);
// ISBN may be quoted in output
$this->assertStringContainsString($book->isbn, $content); // @phpstan-ignore-line unchanged
}
public function testDeleteBook(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$book = Book::first();
$iri = $this->getIriFromResource($book);
$response = $this->delete($iri, headers: ['accept' => 'text/ld+toon']);
$response->assertStatus(204);
$this->assertNull(Book::find($book->id));
}
}