ChainManagerRegistryTest::can_get_managers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 21
rs 9.9
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Unit;
4
5
use Doctrine\Persistence\ManagerRegistry;
6
use Doctrine\Persistence\ObjectManager;
7
use Doctrine\Persistence\ObjectRepository;
8
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Zenstruck\Foundry\ChainManagerRegistry;
10
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post;
11
12
final class ChainManagerRegistryTest extends TestCase
13
{
14
    /**
15
     * @test
16
     */
17
    public function can_get_repository(): void
18
    {
19
        $managerRegistry1 = $this->createMock(ManagerRegistry::class);
20
        $managerRegistry2 = $this->createMock(ManagerRegistry::class);
21
22
        $managerRegistry2->expects($this->once())->method('getRepository')->willReturn(
23
            $repository = $this->createMock(ObjectRepository::class)
24
        );
25
26
        $chainManagerRegistry = new ChainManagerRegistry([$managerRegistry1, $managerRegistry2]);
27
28
        $this->assertSame($repository, $chainManagerRegistry->getRepository(Post::class));
29
    }
30
31
    /**
32
     * @test
33
     */
34
    public function throws_exception_if_repository_not_found(): void
35
    {
36
        $class = Post::class;
37
        $this->expectException(\LogicException::class);
38
        $this->expectExceptionMessage("Cannot find repository for class {$class}");
39
40
        $managerRegistry1 = $this->createMock(ManagerRegistry::class);
41
        $managerRegistry2 = $this->createMock(ManagerRegistry::class);
42
43
        $chainManagerRegistry = new ChainManagerRegistry([$managerRegistry1, $managerRegistry2]);
44
45
        $chainManagerRegistry->getRepository($class);
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function can_get_manager_from_class(): void
52
    {
53
        $managerRegistry1 = $this->createMock(ManagerRegistry::class);
54
        $managerRegistry2 = $this->createMock(ManagerRegistry::class);
55
56
        $managerRegistry2->expects($this->once())->method('getManagerForClass')->willReturn(
57
            $objectManager = $this->createMock(ObjectManager::class)
58
        );
59
60
        $chainManagerRegistry = new ChainManagerRegistry([$managerRegistry1, $managerRegistry2]);
61
62
        $this->assertSame($objectManager, $chainManagerRegistry->getManagerForClass(Post::class));
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function returns_null_if_no_manager_found(): void
69
    {
70
        $managerRegistry1 = $this->createMock(ManagerRegistry::class);
71
        $managerRegistry2 = $this->createMock(ManagerRegistry::class);
72
73
        $chainManagerRegistry = new ChainManagerRegistry([$managerRegistry1, $managerRegistry2]);
74
75
        $this->assertNull($chainManagerRegistry->getManagerForClass(Post::class));
76
    }
77
78
    /**
79
     * @test
80
     */
81
    public function can_get_managers(): void
82
    {
83
        $managerRegistry1 = $this->createMock(ManagerRegistry::class);
84
        $managerRegistry2 = $this->createMock(ManagerRegistry::class);
85
86
        $managerRegistry1->expects($this->once())->method('getManagers')->willReturn(
87
            [$objectManager1 = $this->createMock(ObjectManager::class)]
88
        );
89
90
        $managerRegistry2->expects($this->once())->method('getManagers')->willReturn(
91
            [
92
                $objectManager2 = $this->createMock(ObjectManager::class),
93
                $objectManager3 = $this->createMock(ObjectManager::class),
94
            ]
95
        );
96
97
        $chainManagerRegistry = new ChainManagerRegistry([$managerRegistry1, $managerRegistry2]);
98
99
        $this->assertSame(
100
            [$objectManager1, $objectManager2, $objectManager3],
101
            $chainManagerRegistry->getManagers()
102
        );
103
    }
104
}
105