Issues (131)

tests/Functional/ModelFactoryServiceTest.php (1 issue)

Labels
Severity
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 Factories, ResetDatabase;
17
18
    protected function setUp(): void
19
    {
20
        if (false === \getenv('DATABASE_URL')) {
21
            self::markTestSkipped('doctrine/orm not enabled.');
22
        }
23
    }
24
25
    /**
26
     * @test
27
     */
28
    public function can_create_service_factory(): void
29
    {
30
        if (!\getenv('USE_FOUNDRY_BUNDLE')) {
31
            $this->markTestSkipped('ZenstruckFoundryBundle not enabled.');
32
        }
33
34
        $factory = CategoryServiceFactory::new();
35
36
        $this->assertSame('From Service', $factory->create()->getName());
0 ignored issues
show
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

36
        $this->assertSame('From Service', $factory->create()->/** @scrutinizer ignore-call */ getName());
Loading history...
37
        $this->assertSame('From Factory Create', $factory->create(['name' => 'From Factory Create'])->getName());
38
    }
39
40
    /**
41
     * @test
42
     */
43
    public function service_factories_are_not_the_same_object(): void
44
    {
45
        if (!\getenv('USE_FOUNDRY_BUNDLE')) {
46
            $this->markTestSkipped('ZenstruckFoundryBundle not enabled.');
47
        }
48
49
        $this->assertNotSame(CategoryServiceFactory::new(), CategoryServiceFactory::new());
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function service_factories_cannot_be_used_without_bundle(): void
56
    {
57
        if (\getenv('USE_FOUNDRY_BUNDLE')) {
58
            $this->markTestSkipped('ZenstruckFoundryBundle enabled.');
59
        }
60
61
        $this->expectException(\RuntimeException::class);
62
        $this->expectExceptionMessage('Model Factories with dependencies (Model Factory services) cannot be used without the foundry bundle.');
63
64
        CategoryServiceFactory::new();
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function cannot_create_service_factories_without_foundry_booted(): void
71
    {
72
        Factory::shutdown();
73
74
        $this->expectException(\RuntimeException::class);
75
        $this->expectExceptionMessage('Model Factories with dependencies (Model Factory services) cannot be created before foundry is booted.');
76
77
        CategoryServiceFactory::new();
78
    }
79
}
80