1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Functional\Bundle\Maker; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
6
|
|
|
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; |
7
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
8
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Kevin Bond <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
final class MakeFactoryTest extends MakerTestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @test |
17
|
|
|
*/ |
18
|
|
|
public function can_create_factory(): void |
19
|
|
|
{ |
20
|
|
|
$tester = new CommandTester((new Application(self::bootKernel()))->find('make:factory')); |
21
|
|
|
|
22
|
|
|
$this->assertFileNotExists(self::tempFile('src/Factory/CategoryFactory.php')); |
23
|
|
|
|
24
|
|
|
$tester->execute(['entity' => Category::class]); |
25
|
|
|
|
26
|
|
|
$this->assertFileExists(self::tempFile('src/Factory/CategoryFactory.php')); |
27
|
|
|
$this->assertSame(<<<EOF |
28
|
|
|
<?php |
29
|
|
|
|
30
|
|
|
namespace App\\Factory; |
31
|
|
|
|
32
|
|
|
use Zenstruck\\Foundry\\Tests\\Fixtures\\Entity\\Category; |
33
|
|
|
use Zenstruck\\Foundry\\ModelFactory; |
34
|
|
|
use Zenstruck\\Foundry\\Proxy; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @method static Category|Proxy findOrCreate(array \$attributes) |
38
|
|
|
* @method static Category|Proxy random() |
39
|
|
|
* @method static Category[]|Proxy[] randomSet(int \$number) |
40
|
|
|
* @method static Category[]|Proxy[] randomRange(int \$min, int \$max) |
41
|
|
|
* @method Category|Proxy create(\$attributes = []) |
42
|
|
|
* @method Category[]|Proxy[] createMany(int \$number, \$attributes = []) |
43
|
|
|
*/ |
44
|
|
|
final class CategoryFactory extends ModelFactory |
45
|
|
|
{ |
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
parent::__construct(); |
49
|
|
|
|
50
|
|
|
// TODO inject services if required (https://github.com/zenstruck/foundry#factories-as-services) |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function getDefaults(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
// TODO add your default values here (https://github.com/zenstruck/foundry#model-factories) |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function initialize(): self |
61
|
|
|
{ |
62
|
|
|
// see https://github.com/zenstruck/foundry#initialization |
63
|
|
|
return \$this |
64
|
|
|
// ->afterInstantiate(function(Category \$category) {}) |
65
|
|
|
; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected static function getClass(): string |
69
|
|
|
{ |
70
|
|
|
return Category::class; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
EOF |
75
|
|
|
, \file_get_contents(self::tempFile('src/Factory/CategoryFactory.php')) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @test |
81
|
|
|
*/ |
82
|
|
|
public function can_create_factory_interactively(): void |
83
|
|
|
{ |
84
|
|
|
$tester = new CommandTester((new Application(self::bootKernel()))->find('make:factory')); |
85
|
|
|
|
86
|
|
|
$this->assertFileNotExists(self::tempFile('src/Factory/CategoryFactory.php')); |
87
|
|
|
|
88
|
|
|
$tester->setInputs([Category::class]); |
89
|
|
|
$tester->execute([]); |
90
|
|
|
$output = $tester->getDisplay(); |
91
|
|
|
|
92
|
|
|
$this->assertFileExists(self::tempFile('src/Factory/CategoryFactory.php')); |
93
|
|
|
$this->assertStringContainsString('Note: pass --test if you want to generate factories in your tests/ directory', $output); |
94
|
|
|
$this->assertSame(<<<EOF |
95
|
|
|
<?php |
96
|
|
|
|
97
|
|
|
namespace App\\Factory; |
98
|
|
|
|
99
|
|
|
use Zenstruck\\Foundry\\Tests\\Fixtures\\Entity\\Category; |
100
|
|
|
use Zenstruck\\Foundry\\ModelFactory; |
101
|
|
|
use Zenstruck\\Foundry\\Proxy; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @method static Category|Proxy findOrCreate(array \$attributes) |
105
|
|
|
* @method static Category|Proxy random() |
106
|
|
|
* @method static Category[]|Proxy[] randomSet(int \$number) |
107
|
|
|
* @method static Category[]|Proxy[] randomRange(int \$min, int \$max) |
108
|
|
|
* @method Category|Proxy create(\$attributes = []) |
109
|
|
|
* @method Category[]|Proxy[] createMany(int \$number, \$attributes = []) |
110
|
|
|
*/ |
111
|
|
|
final class CategoryFactory extends ModelFactory |
112
|
|
|
{ |
113
|
|
|
public function __construct() |
114
|
|
|
{ |
115
|
|
|
parent::__construct(); |
116
|
|
|
|
117
|
|
|
// TODO inject services if required (https://github.com/zenstruck/foundry#factories-as-services) |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function getDefaults(): array |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
// TODO add your default values here (https://github.com/zenstruck/foundry#model-factories) |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function initialize(): self |
128
|
|
|
{ |
129
|
|
|
// see https://github.com/zenstruck/foundry#initialization |
130
|
|
|
return \$this |
131
|
|
|
// ->afterInstantiate(function(Category \$category) {}) |
132
|
|
|
; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected static function getClass(): string |
136
|
|
|
{ |
137
|
|
|
return Category::class; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
EOF |
142
|
|
|
, \file_get_contents(self::tempFile('src/Factory/CategoryFactory.php')) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @test |
148
|
|
|
*/ |
149
|
|
|
public function can_create_factory_in_test_dir(): void |
150
|
|
|
{ |
151
|
|
|
$tester = new CommandTester((new Application(self::bootKernel()))->find('make:factory')); |
152
|
|
|
|
153
|
|
|
$this->assertFileNotExists(self::tempFile('tests/Factory/CategoryFactory.php')); |
154
|
|
|
|
155
|
|
|
$tester->execute(['entity' => Category::class, '--test' => true]); |
156
|
|
|
|
157
|
|
|
$this->assertFileExists(self::tempFile('tests/Factory/CategoryFactory.php')); |
158
|
|
|
$this->assertSame(<<<EOF |
159
|
|
|
<?php |
160
|
|
|
|
161
|
|
|
namespace App\\Tests\\Factory; |
162
|
|
|
|
163
|
|
|
use Zenstruck\\Foundry\\Tests\\Fixtures\\Entity\\Category; |
164
|
|
|
use Zenstruck\\Foundry\\ModelFactory; |
165
|
|
|
use Zenstruck\\Foundry\\Proxy; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @method static Category|Proxy findOrCreate(array \$attributes) |
169
|
|
|
* @method static Category|Proxy random() |
170
|
|
|
* @method static Category[]|Proxy[] randomSet(int \$number) |
171
|
|
|
* @method static Category[]|Proxy[] randomRange(int \$min, int \$max) |
172
|
|
|
* @method Category|Proxy create(\$attributes = []) |
173
|
|
|
* @method Category[]|Proxy[] createMany(int \$number, \$attributes = []) |
174
|
|
|
*/ |
175
|
|
|
final class CategoryFactory extends ModelFactory |
176
|
|
|
{ |
177
|
|
|
public function __construct() |
178
|
|
|
{ |
179
|
|
|
parent::__construct(); |
180
|
|
|
|
181
|
|
|
// TODO inject services if required (https://github.com/zenstruck/foundry#factories-as-services) |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected function getDefaults(): array |
185
|
|
|
{ |
186
|
|
|
return [ |
187
|
|
|
// TODO add your default values here (https://github.com/zenstruck/foundry#model-factories) |
188
|
|
|
]; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
protected function initialize(): self |
192
|
|
|
{ |
193
|
|
|
// see https://github.com/zenstruck/foundry#initialization |
194
|
|
|
return \$this |
195
|
|
|
// ->afterInstantiate(function(Category \$category) {}) |
196
|
|
|
; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
protected static function getClass(): string |
200
|
|
|
{ |
201
|
|
|
return Category::class; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
EOF |
206
|
|
|
, \file_get_contents(self::tempFile('tests/Factory/CategoryFactory.php')) |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @test |
212
|
|
|
*/ |
213
|
|
|
public function can_create_factory_in_test_dir_interactively(): void |
214
|
|
|
{ |
215
|
|
|
$tester = new CommandTester((new Application(self::bootKernel()))->find('make:factory')); |
216
|
|
|
|
217
|
|
|
$this->assertFileNotExists(self::tempFile('tests/Factory/CategoryFactory.php')); |
218
|
|
|
|
219
|
|
|
$tester->setInputs([Category::class]); |
220
|
|
|
$tester->execute(['--test' => true]); |
221
|
|
|
$output = $tester->getDisplay(); |
222
|
|
|
|
223
|
|
|
$this->assertFileExists(self::tempFile('tests/Factory/CategoryFactory.php')); |
224
|
|
|
$this->assertStringNotContainsString('Note: pass --test if you want to generate factories in your tests/ directory', $output); |
225
|
|
|
$this->assertSame(<<<EOF |
226
|
|
|
<?php |
227
|
|
|
|
228
|
|
|
namespace App\\Tests\\Factory; |
229
|
|
|
|
230
|
|
|
use Zenstruck\\Foundry\\Tests\\Fixtures\\Entity\\Category; |
231
|
|
|
use Zenstruck\\Foundry\\ModelFactory; |
232
|
|
|
use Zenstruck\\Foundry\\Proxy; |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @method static Category|Proxy findOrCreate(array \$attributes) |
236
|
|
|
* @method static Category|Proxy random() |
237
|
|
|
* @method static Category[]|Proxy[] randomSet(int \$number) |
238
|
|
|
* @method static Category[]|Proxy[] randomRange(int \$min, int \$max) |
239
|
|
|
* @method Category|Proxy create(\$attributes = []) |
240
|
|
|
* @method Category[]|Proxy[] createMany(int \$number, \$attributes = []) |
241
|
|
|
*/ |
242
|
|
|
final class CategoryFactory extends ModelFactory |
243
|
|
|
{ |
244
|
|
|
public function __construct() |
245
|
|
|
{ |
246
|
|
|
parent::__construct(); |
247
|
|
|
|
248
|
|
|
// TODO inject services if required (https://github.com/zenstruck/foundry#factories-as-services) |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
protected function getDefaults(): array |
252
|
|
|
{ |
253
|
|
|
return [ |
254
|
|
|
// TODO add your default values here (https://github.com/zenstruck/foundry#model-factories) |
255
|
|
|
]; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
protected function initialize(): self |
259
|
|
|
{ |
260
|
|
|
// see https://github.com/zenstruck/foundry#initialization |
261
|
|
|
return \$this |
262
|
|
|
// ->afterInstantiate(function(Category \$category) {}) |
263
|
|
|
; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
protected static function getClass(): string |
267
|
|
|
{ |
268
|
|
|
return Category::class; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
EOF |
273
|
|
|
, \file_get_contents(self::tempFile('tests/Factory/CategoryFactory.php')) |
274
|
|
|
); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @test |
279
|
|
|
*/ |
280
|
|
|
public function invalid_entity_throws_exception(): void |
281
|
|
|
{ |
282
|
|
|
$tester = new CommandTester((new Application(self::bootKernel()))->find('make:factory')); |
283
|
|
|
|
284
|
|
|
$this->assertFileNotExists(self::tempFile('src/Factory/InvalidFactory.php')); |
285
|
|
|
|
286
|
|
|
try { |
287
|
|
|
$tester->execute(['entity' => 'Invalid']); |
288
|
|
|
} catch (RuntimeCommandException $e) { |
289
|
|
|
$this->assertSame('Entity "Invalid" not found.', $e->getMessage()); |
290
|
|
|
$this->assertFileNotExists(self::tempFile('src/Factory/InvalidFactory.php')); |
291
|
|
|
|
292
|
|
|
return; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$this->fail('Exception not thrown.'); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|