|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Functional; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
|
6
|
|
|
use Zenstruck\Foundry\Factory; |
|
7
|
|
|
use Zenstruck\Foundry\Test\Factories; |
|
8
|
|
|
use Zenstruck\Foundry\Test\ResetDatabase; |
|
9
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryServiceFactory; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Kevin Bond <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
final class ModelFactoryServiceTest extends KernelTestCase |
|
15
|
|
|
{ |
|
16
|
|
|
use ResetDatabase, Factories; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @test |
|
20
|
|
|
*/ |
|
21
|
|
|
public function can_create_service_factory(): void |
|
22
|
|
|
{ |
|
23
|
|
|
if (!\getenv('USE_FOUNDRY_BUNDLE')) { |
|
24
|
|
|
$this->markTestSkipped('ZenstruckFoundryBundle not enabled.'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$factory = CategoryServiceFactory::new(); |
|
28
|
|
|
|
|
29
|
|
|
$this->assertSame('From Service', $factory->create()->getName()); |
|
|
|
|
|
|
30
|
|
|
$this->assertSame('From Factory Create', $factory->create(['name' => 'From Factory Create'])->getName()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @test |
|
35
|
|
|
*/ |
|
36
|
|
|
public function service_factories_are_not_the_same_object(): void |
|
37
|
|
|
{ |
|
38
|
|
|
if (!\getenv('USE_FOUNDRY_BUNDLE')) { |
|
39
|
|
|
$this->markTestSkipped('ZenstruckFoundryBundle not enabled.'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->assertNotSame(CategoryServiceFactory::new(), CategoryServiceFactory::new()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @test |
|
47
|
|
|
*/ |
|
48
|
|
|
public function service_factories_cannot_be_used_without_bundle(): void |
|
49
|
|
|
{ |
|
50
|
|
|
if (\getenv('USE_FOUNDRY_BUNDLE')) { |
|
51
|
|
|
$this->markTestSkipped('ZenstruckFoundryBundle enabled.'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->expectException(\RuntimeException::class); |
|
55
|
|
|
$this->expectExceptionMessage('Model Factories with dependencies (Model Factory services) cannot be used without the foundry bundle.'); |
|
56
|
|
|
|
|
57
|
|
|
CategoryServiceFactory::new(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @test |
|
62
|
|
|
*/ |
|
63
|
|
|
public function cannot_create_service_factories_without_foundry_booted(): void |
|
64
|
|
|
{ |
|
65
|
|
|
Factory::shutdown(); |
|
66
|
|
|
|
|
67
|
|
|
$this->expectException(\RuntimeException::class); |
|
68
|
|
|
$this->expectExceptionMessage('Model Factories with dependencies (Model Factory services) cannot be created before foundry is booted.'); |
|
69
|
|
|
|
|
70
|
|
|
CategoryServiceFactory::new(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|