Skip to content

Commit 9618e87

Browse files
authored
[DX][Internal] use Foundry for comic books (#970)
| Q | A | --------------- | ----- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT Based on #969
2 parents 6035483 + 6c77163 commit 9618e87

File tree

7 files changed

+195
-48
lines changed

7 files changed

+195
-48
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ COPY . /app
1515

1616
WORKDIR /app
1717

18+
ENV PHP_MEMORY_LIMIT=512M
19+
1820
RUN composer global config --no-plugins allow-plugins.symfony/flex true
1921
RUN composer global require --no-progress --no-scripts --no-plugins "symfony/flex:^1.10"
2022
RUN composer update --with-all-dependencies --no-interaction --no-progress
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Foundry\Factory;
15+
16+
use App\Entity\Author;
17+
use Zenstruck\Foundry\ObjectFactory;
18+
19+
/**
20+
* @extends ObjectFactory<Author>
21+
*/
22+
final class AuthorFactory extends ObjectFactory
23+
{
24+
public static function class(): string
25+
{
26+
return Author::class;
27+
}
28+
29+
public function withFirstName(string $firstName): self
30+
{
31+
return $this->with(['firstName' => $firstName]);
32+
}
33+
34+
public function withLastName(string $lastName): self
35+
{
36+
return $this->with(['lastName' => $lastName]);
37+
}
38+
39+
protected function defaults(): array
40+
{
41+
return [
42+
'firstName' => self::faker()->firstName(),
43+
'lastName' => self::faker()->lastName(),
44+
];
45+
}
46+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Foundry\Factory;
15+
16+
use App\Entity\Author;
17+
use App\Entity\ComicBook;
18+
use Doctrine\Persistence\Proxy;
19+
use Zenstruck\Foundry\LazyValue;
20+
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
21+
22+
/**
23+
* @extends PersistentProxyObjectFactory<ComicBook>
24+
*/
25+
final class ComicBookFactory extends PersistentProxyObjectFactory
26+
{
27+
public static function class(): string
28+
{
29+
return ComicBook::class;
30+
}
31+
32+
public function withTitle(string $title): self
33+
{
34+
return $this->with(['title' => $title]);
35+
}
36+
37+
/**
38+
* @param AuthorFactory|Proxy<Author> $author
39+
*/
40+
public function withAuthor(AuthorFactory|Proxy $author): self
41+
{
42+
return $this->with(['author' => $author]);
43+
}
44+
45+
protected function defaults(): array
46+
{
47+
$author = LazyValue::memoize(fn () => AuthorFactory::createOne());
48+
49+
return [
50+
'title' => ucfirst(self::faker()->words(2, true)),
51+
'author' => $author,
52+
];
53+
}
54+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Foundry\Story;
15+
16+
use App\Foundry\Factory\AuthorFactory;
17+
use App\Foundry\Factory\ComicBookFactory;
18+
use Zenstruck\Foundry\Story;
19+
20+
final class DefaultComicBooksStory extends Story
21+
{
22+
public function build(): void
23+
{
24+
ComicBookFactory::new()
25+
->withTitle('Old Man Logan')
26+
->withAuthor(
27+
AuthorFactory::new()
28+
->withFirstName('Andrea')
29+
->withLastName('Sorrentino'),
30+
)
31+
->create()
32+
;
33+
34+
ComicBookFactory::new()
35+
->withTitle('Civil War II')
36+
->withAuthor(
37+
AuthorFactory::new()
38+
->withFirstName('Brian Michael')
39+
->withLastName('Bendis'),
40+
)
41+
->create()
42+
;
43+
}
44+
}

tests/Application/src/Foundry/Story/MoreBooksStory.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,18 @@
1414
namespace App\Foundry\Story;
1515

1616
use App\Foundry\Factory\BookFactory;
17-
use function Zenstruck\Foundry\Persistence\flush_after;
1817
use Zenstruck\Foundry\Story;
1918

2019
final class MoreBooksStory extends Story
2120
{
2221
public function build(): void
2322
{
24-
flush_after(function () {
25-
foreach (range(1, 22) as $number) {
26-
BookFactory::new()
27-
->withTitle('Book ' . $number)
28-
->create()
29-
;
30-
}
31-
});
23+
BookFactory::createSequence(
24+
function () {
25+
foreach (range(1, 22) as $number) {
26+
yield ['title' => 'Book ' . $number];
27+
}
28+
},
29+
);
3230
}
3331
}

tests/Application/src/Tests/Controller/ComicBookApiTest.php

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@
1414
namespace App\Tests\Controller;
1515

1616
use ApiTestCase\JsonApiTestCase;
17+
use App\Foundry\Factory\AuthorFactory;
18+
use App\Foundry\Factory\ComicBookFactory;
19+
use App\Foundry\Story\DefaultComicBooksStory;
1720
use Symfony\Component\HttpFoundation\Response;
21+
use Zenstruck\Foundry\Test\Factories;
22+
use Zenstruck\Foundry\Test\ResetDatabase;
1823

1924
final class ComicBookApiTest extends JsonApiTestCase
2025
{
26+
use Factories;
27+
use ResetDatabase;
28+
2129
/**
2230
* @test
2331
*/
24-
public function it_allows_creating_a_comic_book()
32+
public function it_allows_creating_a_comic_book(): void
2533
{
2634
$data =
2735
<<<EOT
@@ -42,7 +50,7 @@ public function it_allows_creating_a_comic_book()
4250
/**
4351
* @test
4452
*/
45-
public function it_allows_versioned_creating_a_comic_book()
53+
public function it_allows_versioned_creating_a_comic_book(): void
4654
{
4755
$this->markAsSkippedIfNecessary();
4856

@@ -65,9 +73,9 @@ public function it_allows_versioned_creating_a_comic_book()
6573
/**
6674
* @test
6775
*/
68-
public function it_allows_updating_a_comic_book()
76+
public function it_allows_updating_a_comic_book(): void
6977
{
70-
$objects = $this->loadFixturesFromFile('comic_books.yml');
78+
$comicBook = self::someComicBook()->create();
7179

7280
$data =
7381
<<<EOT
@@ -80,17 +88,17 @@ public function it_allows_updating_a_comic_book()
8088
}
8189
EOT;
8290

83-
$this->client->request('PUT', '/v1/comic-books/' . $objects['comic-book1']->getId(), [], [], ['CONTENT_TYPE' => 'application/json'], $data);
91+
$this->client->request('PUT', '/v1/comic-books/' . $comicBook->getId(), [], [], ['CONTENT_TYPE' => 'application/json'], $data);
8492
$response = $this->client->getResponse();
8593
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
8694
}
8795

8896
/**
8997
* @test
9098
*/
91-
public function it_allows_updating_partial_information_about_a_comic_book()
99+
public function it_allows_updating_partial_information_about_a_comic_book(): void
92100
{
93-
$objects = $this->loadFixturesFromFile('comic_books.yml');
101+
$comicBook = self::someComicBook()->create();
94102

95103
$data =
96104
<<<EOT
@@ -102,57 +110,57 @@ public function it_allows_updating_partial_information_about_a_comic_book()
102110
}
103111
EOT;
104112

105-
$this->client->request('PATCH', '/v1/comic-books/' . $objects['comic-book1']->getId(), [], [], ['CONTENT_TYPE' => 'application/json'], $data);
113+
$this->client->request('PATCH', '/v1/comic-books/' . $comicBook->getId(), [], [], ['CONTENT_TYPE' => 'application/json'], $data);
106114
$response = $this->client->getResponse();
107115
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
108116
}
109117

110118
/**
111119
* @test
112120
*/
113-
public function it_allows_removing_a_comic_book()
121+
public function it_allows_removing_a_comic_book(): void
114122
{
115-
$objects = $this->loadFixturesFromFile('comic_books.yml');
123+
$comicBook = self::someComicBook()->create();
116124

117-
$this->client->request('DELETE', '/v1/comic-books/' . $objects['comic-book1']->getId());
125+
$this->client->request('DELETE', '/v1/comic-books/' . $comicBook->getId());
118126
$response = $this->client->getResponse();
119127
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
120128
}
121129

122130
/**
123131
* @test
124132
*/
125-
public function it_allows_showing_a_comic_book()
133+
public function it_allows_showing_a_comic_book(): void
126134
{
127-
$objects = $this->loadFixturesFromFile('comic_books.yml');
135+
$comicBook = self::someComicBook()->create();
128136

129-
$this->client->request('GET', '/v1/comic-books/' . $objects['comic-book1']->getId());
137+
$this->client->request('GET', '/v1/comic-books/' . $comicBook->getId());
130138
$response = $this->client->getResponse();
131139
$this->assertResponse($response, 'comic-books/show_response');
132140
}
133141

134142
/**
135143
* @test
136144
*/
137-
public function it_allows_versioning_of_a_showing_comic_book_serialization()
145+
public function it_allows_versioning_of_a_showing_comic_book_serialization(): void
138146
{
139147
$this->markAsSkippedIfNecessary();
140148

141-
$objects = $this->loadFixturesFromFile('comic_books.yml');
149+
$comicBook = self::someComicBook()->create();
142150

143-
$this->client->request('GET', '/v1.2/comic-books/' . $objects['comic-book1']->getId());
151+
$this->client->request('GET', '/v1.2/comic-books/' . $comicBook->getId());
144152
$response = $this->client->getResponse();
145153
$this->assertResponse($response, 'comic-books/versioned_show_response');
146154
}
147155

148156
/**
149157
* @test
150158
*/
151-
public function it_allows_indexing_of_comic_books()
159+
public function it_allows_indexing_of_comic_books(): void
152160
{
153161
$this->markAsSkippedIfNecessary();
154162

155-
$this->loadFixturesFromFile('comic_books.yml');
163+
DefaultComicBooksStory::load();
156164

157165
$this->client->request('GET', '/v1/comic-books/');
158166
$response = $this->client->getResponse();
@@ -162,11 +170,11 @@ public function it_allows_indexing_of_comic_books()
162170
/**
163171
* @test
164172
*/
165-
public function it_allows_versioned_indexing_of_comic_books()
173+
public function it_allows_versioned_indexing_of_comic_books(): void
166174
{
167175
$this->markAsSkippedIfNecessary();
168176

169-
$this->loadFixturesFromFile('comic_books.yml');
177+
DefaultComicBooksStory::load();
170178

171179
$this->client->request('GET', '/v1.2/comic-books/');
172180
$response = $this->client->getResponse();
@@ -176,10 +184,8 @@ public function it_allows_versioned_indexing_of_comic_books()
176184
/**
177185
* @test
178186
*/
179-
public function it_does_not_allow_showing_resource_if_it_does_not_exist()
187+
public function it_does_not_allow_showing_resource_if_it_does_not_exist(): void
180188
{
181-
$this->loadFixturesFromFile('comic_books.yml');
182-
183189
$this->client->request('GET', '/v1/comic-books/3');
184190
$response = $this->client->getResponse();
185191
$this->assertResponseCode($response, Response::HTTP_NOT_FOUND);
@@ -191,4 +197,16 @@ private function markAsSkippedIfNecessary(): void
191197
$this->markTestSkipped();
192198
}
193199
}
200+
201+
private static function someComicBook(): ComicBookFactory
202+
{
203+
return ComicBookFactory::new()
204+
->withTitle('Old Man Logan')
205+
->withAuthor(
206+
AuthorFactory::new()
207+
->withFirstName('Andrea')
208+
->withLastName('Sorrentino'),
209+
)
210+
;
211+
}
194212
}

tests/Application/src/Tests/DataFixtures/ORM/comic_books.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)