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