Passed
Push — master ( b80d77...ea8430 )
by Kevin
14:18 queued 12:14
created

can_create_service_factory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 0
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());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        $this->assertSame('From Service', $factory->create()->/** @scrutinizer ignore-call */ getName());
Loading history...
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