/
BookTest.php
140 lines (121 loc) · 4.98 KB
/
BookTest.php
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
<?php
declare(strict_types=1);
namespace App\Tests\Api;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Symfony\Bundle\Test\Client;
use App\DataFixtures\Factory\BookFactory;
use App\DataFixtures\Factory\ReviewFactory;
use App\Enum\BookCondition;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\FactoryCollection;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
final class BookTest extends ApiTestCase
{
use Factories;
use ResetDatabase;
private Client $client;
protected function setup(): void
{
$this->client = self::createClient();
}
#[Test]
#[DataProvider(methodName: 'getUrls')]
public function asAnonymousICanGetACollectionOfBooks(FactoryCollection $factory, string $url, int $hydraTotalItems): void
{
// Cannot use Factory as data provider because BookFactory has a service dependency
$factory->create();
$response = $this->client->request('GET', $url);
self::assertResponseIsSuccessful();
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
self::assertJsonContains([
'hydra:totalItems' => $hydraTotalItems,
]);
self::assertCount(min($hydraTotalItems, 30), $response->toArray()['hydra:member']);
self::assertMatchesJsonSchema(file_get_contents(__DIR__ . '/schemas/Book/collection.json'));
}
public static function getUrls(): iterable
{
yield 'all books' => [
BookFactory::new()->many(35),
'/books',
35,
];
yield 'books filtered by title' => [
BookFactory::new()->sequence(static function () {
yield ['title' => 'Hyperion'];
foreach (range(1, 10) as $i) {
yield [];
}
}),
'/books?title=yperio',
1,
];
yield 'books filtered by author' => [
BookFactory::new()->sequence(static function () {
yield ['author' => 'Dan Simmons'];
foreach (range(1, 10) as $i) {
yield [];
}
}),
'/books?author=simmons',
1,
];
yield 'books filtered by condition' => [
BookFactory::new()->sequence(static function () {
foreach (range(1, 100) as $i) {
// 33% of books are damaged
yield ['condition' => $i % 3 ? BookCondition::NewCondition : BookCondition::DamagedCondition];
}
}),
'/books?condition=' . BookCondition::DamagedCondition->value,
33,
];
}
#[Test]
public function asAdminUserICanGetACollectionOfBooksOrderedByTitle(): void
{
BookFactory::createOne(['title' => 'Hyperion']);
BookFactory::createOne(['title' => 'The Wandering Earth']);
BookFactory::createOne(['title' => 'Ball Lightning']);
$response = $this->client->request('GET', '/books?order[title]=asc');
self::assertResponseIsSuccessful();
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
self::assertEquals('Ball Lightning', $response->toArray()['hydra:member'][0]['title']);
self::assertEquals('Hyperion', $response->toArray()['hydra:member'][1]['title']);
self::assertEquals('The Wandering Earth', $response->toArray()['hydra:member'][2]['title']);
self::assertMatchesJsonSchema(file_get_contents(__DIR__ . '/schemas/Book/collection.json'));
}
#[Test]
public function asAnonymousICannotGetAnInvalidBook(): void
{
BookFactory::createOne();
$this->client->request('GET', '/books/invalid');
self::assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
}
#[Test]
public function asAnonymousICanGetABook(): void
{
$book = BookFactory::createOne();
ReviewFactory::createOne(['rating' => 1, 'book' => $book]);
ReviewFactory::createOne(['rating' => 2, 'book' => $book]);
ReviewFactory::createOne(['rating' => 3, 'book' => $book]);
ReviewFactory::createOne(['rating' => 4, 'book' => $book]);
ReviewFactory::createOne(['rating' => 5, 'book' => $book]);
$this->client->request('GET', '/books/' . $book->getId());
self::assertResponseIsSuccessful();
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
self::assertJsonContains([
'@id' => '/books/' . $book->getId(),
'book' => $book->book,
'condition' => $book->condition->value,
'title' => $book->title,
'author' => $book->author,
'reviews' => '/books/' . $book->getId() . '/reviews',
'rating' => 3,
]);
self::assertMatchesJsonSchema(file_get_contents(__DIR__ . '/schemas/Book/item.json'));
}
}