Passed
Push — master ( b9c161...d2cd4c )
by Kevin
05:14 queued 02:28
created

MakeFactoryTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 343
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 8
eloc 268
c 5
b 1
f 2
dl 0
loc 343
rs 10

7 Methods

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