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
|
|
|
protected function getDefaults(): array |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
// TODO add your default values here (https://github.com/zenstruck/foundry#model-factories) |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected static function getClass(): string |
54
|
|
|
{ |
55
|
|
|
return Category::class; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
EOF |
60
|
|
|
, \file_get_contents(self::tempFile('src/Factory/CategoryFactory.php')) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @test |
66
|
|
|
*/ |
67
|
|
|
public function can_create_factory_interactively(): void |
68
|
|
|
{ |
69
|
|
|
$tester = new CommandTester((new Application(self::bootKernel()))->find('make:factory')); |
70
|
|
|
|
71
|
|
|
$this->assertFileNotExists(self::tempFile('src/Factory/CategoryFactory.php')); |
72
|
|
|
|
73
|
|
|
$tester->setInputs([Category::class]); |
74
|
|
|
$tester->execute([]); |
75
|
|
|
$output = $tester->getDisplay(); |
76
|
|
|
|
77
|
|
|
$this->assertFileExists(self::tempFile('src/Factory/CategoryFactory.php')); |
78
|
|
|
$this->assertStringContainsString('Note: pass --test if you want to generate factories in your tests/ directory', $output); |
79
|
|
|
$this->assertSame(<<<EOF |
80
|
|
|
<?php |
81
|
|
|
|
82
|
|
|
namespace App\\Factory; |
83
|
|
|
|
84
|
|
|
use Zenstruck\\Foundry\\Tests\\Fixtures\\Entity\\Category; |
85
|
|
|
use Zenstruck\\Foundry\\ModelFactory; |
86
|
|
|
use Zenstruck\\Foundry\\Proxy; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @method static Category|Proxy findOrCreate(array \$attributes) |
90
|
|
|
* @method static Category|Proxy random() |
91
|
|
|
* @method static Category[]|Proxy[] randomSet(int \$number) |
92
|
|
|
* @method static Category[]|Proxy[] randomRange(int \$min, int \$max) |
93
|
|
|
* @method Category|Proxy create(\$attributes = []) |
94
|
|
|
* @method Category[]|Proxy[] createMany(int \$number, \$attributes = []) |
95
|
|
|
*/ |
96
|
|
|
final class CategoryFactory extends ModelFactory |
97
|
|
|
{ |
98
|
|
|
protected function getDefaults(): array |
99
|
|
|
{ |
100
|
|
|
return [ |
101
|
|
|
// TODO add your default values here (https://github.com/zenstruck/foundry#model-factories) |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected static function getClass(): string |
106
|
|
|
{ |
107
|
|
|
return Category::class; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
EOF |
112
|
|
|
, \file_get_contents(self::tempFile('src/Factory/CategoryFactory.php')) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @test |
118
|
|
|
*/ |
119
|
|
|
public function can_create_factory_in_test_dir(): void |
120
|
|
|
{ |
121
|
|
|
$tester = new CommandTester((new Application(self::bootKernel()))->find('make:factory')); |
122
|
|
|
|
123
|
|
|
$this->assertFileNotExists(self::tempFile('tests/Factory/CategoryFactory.php')); |
124
|
|
|
|
125
|
|
|
$tester->execute(['entity' => Category::class, '--test' => true]); |
126
|
|
|
|
127
|
|
|
$this->assertFileExists(self::tempFile('tests/Factory/CategoryFactory.php')); |
128
|
|
|
$this->assertSame(<<<EOF |
129
|
|
|
<?php |
130
|
|
|
|
131
|
|
|
namespace App\\Tests\\Factory; |
132
|
|
|
|
133
|
|
|
use Zenstruck\\Foundry\\Tests\\Fixtures\\Entity\\Category; |
134
|
|
|
use Zenstruck\\Foundry\\ModelFactory; |
135
|
|
|
use Zenstruck\\Foundry\\Proxy; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @method static Category|Proxy findOrCreate(array \$attributes) |
139
|
|
|
* @method static Category|Proxy random() |
140
|
|
|
* @method static Category[]|Proxy[] randomSet(int \$number) |
141
|
|
|
* @method static Category[]|Proxy[] randomRange(int \$min, int \$max) |
142
|
|
|
* @method Category|Proxy create(\$attributes = []) |
143
|
|
|
* @method Category[]|Proxy[] createMany(int \$number, \$attributes = []) |
144
|
|
|
*/ |
145
|
|
|
final class CategoryFactory extends ModelFactory |
146
|
|
|
{ |
147
|
|
|
protected function getDefaults(): array |
148
|
|
|
{ |
149
|
|
|
return [ |
150
|
|
|
// TODO add your default values here (https://github.com/zenstruck/foundry#model-factories) |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected static function getClass(): string |
155
|
|
|
{ |
156
|
|
|
return Category::class; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
EOF |
161
|
|
|
, \file_get_contents(self::tempFile('tests/Factory/CategoryFactory.php')) |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @test |
167
|
|
|
*/ |
168
|
|
|
public function can_create_factory_in_test_dir_interactively(): void |
169
|
|
|
{ |
170
|
|
|
$tester = new CommandTester((new Application(self::bootKernel()))->find('make:factory')); |
171
|
|
|
|
172
|
|
|
$this->assertFileNotExists(self::tempFile('tests/Factory/CategoryFactory.php')); |
173
|
|
|
|
174
|
|
|
$tester->setInputs([Category::class]); |
175
|
|
|
$tester->execute(['--test' => true]); |
176
|
|
|
$output = $tester->getDisplay(); |
177
|
|
|
|
178
|
|
|
$this->assertFileExists(self::tempFile('tests/Factory/CategoryFactory.php')); |
179
|
|
|
$this->assertStringNotContainsString('Note: pass --test if you want to generate factories in your tests/ directory', $output); |
180
|
|
|
$this->assertSame(<<<EOF |
181
|
|
|
<?php |
182
|
|
|
|
183
|
|
|
namespace App\\Tests\\Factory; |
184
|
|
|
|
185
|
|
|
use Zenstruck\\Foundry\\Tests\\Fixtures\\Entity\\Category; |
186
|
|
|
use Zenstruck\\Foundry\\ModelFactory; |
187
|
|
|
use Zenstruck\\Foundry\\Proxy; |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @method static Category|Proxy findOrCreate(array \$attributes) |
191
|
|
|
* @method static Category|Proxy random() |
192
|
|
|
* @method static Category[]|Proxy[] randomSet(int \$number) |
193
|
|
|
* @method static Category[]|Proxy[] randomRange(int \$min, int \$max) |
194
|
|
|
* @method Category|Proxy create(\$attributes = []) |
195
|
|
|
* @method Category[]|Proxy[] createMany(int \$number, \$attributes = []) |
196
|
|
|
*/ |
197
|
|
|
final class CategoryFactory extends ModelFactory |
198
|
|
|
{ |
199
|
|
|
protected function getDefaults(): array |
200
|
|
|
{ |
201
|
|
|
return [ |
202
|
|
|
// TODO add your default values here (https://github.com/zenstruck/foundry#model-factories) |
203
|
|
|
]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
protected static function getClass(): string |
207
|
|
|
{ |
208
|
|
|
return Category::class; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
EOF |
213
|
|
|
, \file_get_contents(self::tempFile('tests/Factory/CategoryFactory.php')) |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @test |
219
|
|
|
*/ |
220
|
|
|
public function invalid_entity_throws_exception(): void |
221
|
|
|
{ |
222
|
|
|
$tester = new CommandTester((new Application(self::bootKernel()))->find('make:factory')); |
223
|
|
|
|
224
|
|
|
$this->assertFileNotExists(self::tempFile('src/Factory/InvalidFactory.php')); |
225
|
|
|
|
226
|
|
|
try { |
227
|
|
|
$tester->execute(['entity' => 'Invalid']); |
228
|
|
|
} catch (RuntimeCommandException $e) { |
229
|
|
|
$this->assertSame('Entity "Invalid" not found.', $e->getMessage()); |
230
|
|
|
$this->assertFileNotExists(self::tempFile('src/Factory/InvalidFactory.php')); |
231
|
|
|
|
232
|
|
|
return; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$this->fail('Exception not thrown.'); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|