Passed
Pull Request — master (#59)
by Kevin
15:23
created

ModelFactoryServiceTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 19
c 3
b 0
f 0
dl 0
loc 57
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A cannot_create_service_factories_without_foundry_booted() 0 8 1
A can_create_service_factory() 0 10 2
A service_factories_are_not_the_same_object() 0 7 2
A service_factories_cannot_be_used_without_bundle() 0 10 2
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