1 | <?php |
||
17 | class ContainerRepositoryFactoryTest extends TestCase |
||
18 | { |
||
19 | public static function setUpBeforeClass() : void |
||
20 | { |
||
21 | if (interface_exists(EntityManagerInterface::class)) { |
||
22 | return; |
||
23 | } |
||
24 | |||
25 | self::markTestSkipped('This test requires ORM'); |
||
26 | } |
||
27 | |||
28 | public function testGetRepositoryReturnsService() : void |
||
29 | { |
||
30 | $em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']); |
||
31 | $repo = new StubRepository(); |
||
32 | $container = $this->createContainer(['my_repo' => $repo]); |
||
33 | |||
34 | $factory = new ContainerRepositoryFactory($container); |
||
|
|||
35 | $this->assertSame($repo, $factory->getRepository($em, 'Foo\CoolEntity')); |
||
36 | } |
||
37 | |||
38 | public function testGetRepositoryReturnsEntityRepository() : void |
||
39 | { |
||
40 | $container = $this->createContainer([]); |
||
41 | $em = $this->createEntityManager(['Foo\BoringEntity' => null]); |
||
42 | |||
43 | $factory = new ContainerRepositoryFactory($container); |
||
44 | $actualRepo = $factory->getRepository($em, 'Foo\BoringEntity'); |
||
45 | $this->assertInstanceOf(EntityRepository::class, $actualRepo); |
||
46 | // test the same instance is returned |
||
47 | $this->assertSame($actualRepo, $factory->getRepository($em, 'Foo\BoringEntity')); |
||
48 | } |
||
49 | |||
50 | public function testCustomRepositoryIsReturned() : void |
||
51 | { |
||
52 | $container = $this->createContainer([]); |
||
53 | $em = $this->createEntityManager([ |
||
54 | 'Foo\CustomNormalRepoEntity' => StubRepository::class, |
||
55 | ]); |
||
56 | |||
57 | $factory = new ContainerRepositoryFactory($container); |
||
58 | $actualRepo = $factory->getRepository($em, 'Foo\CustomNormalRepoEntity'); |
||
59 | $this->assertInstanceOf(StubRepository::class, $actualRepo); |
||
60 | // test the same instance is returned |
||
61 | $this->assertSame($actualRepo, $factory->getRepository($em, 'Foo\CustomNormalRepoEntity')); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @expectedException \RuntimeException |
||
66 | * @expectedExceptionMessage The service "my_repo" must implement ObjectRepository (or extend a base class, like ServiceEntityRepository). |
||
67 | */ |
||
68 | public function testServiceRepositoriesMustExtendObjectRepository() : void |
||
69 | { |
||
70 | $repo = new stdClass(); |
||
71 | |||
72 | $container = $this->createContainer(['my_repo' => $repo]); |
||
73 | |||
74 | $em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']); |
||
75 | |||
76 | $factory = new ContainerRepositoryFactory($container); |
||
77 | $factory->getRepository($em, 'Foo\CoolEntity'); |
||
78 | } |
||
79 | |||
80 | public function testServiceRepositoriesCanNotExtendsEntityRepository() : void |
||
81 | { |
||
82 | $repo = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
||
83 | |||
84 | $container = $this->createContainer(['my_repo' => $repo]); |
||
85 | |||
86 | $em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']); |
||
87 | |||
88 | $factory = new ContainerRepositoryFactory($container); |
||
89 | $factory->getRepository($em, 'Foo\CoolEntity'); |
||
90 | $actualRepo = $factory->getRepository($em, 'Foo\CoolEntity'); |
||
91 | $this->assertSame($repo, $actualRepo); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @expectedException \RuntimeException |
||
96 | * @expectedExceptionMessage The "Doctrine\Bundle\DoctrineBundle\Tests\Repository\StubServiceRepository" entity repository implements "Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface", but its service could not be found. Make sure the service exists and is tagged with "doctrine.repository_service". |
||
97 | */ |
||
98 | public function testRepositoryMatchesServiceInterfaceButServiceNotFound() : void |
||
99 | { |
||
100 | $container = $this->createContainer([]); |
||
101 | |||
102 | $em = $this->createEntityManager([ |
||
103 | 'Foo\CoolEntity' => StubServiceRepository::class, |
||
104 | ]); |
||
105 | |||
106 | $factory = new ContainerRepositoryFactory($container); |
||
107 | $factory->getRepository($em, 'Foo\CoolEntity'); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @expectedException \RuntimeException |
||
112 | * @expectedExceptionMessage The "Foo\CoolEntity" entity has a repositoryClass set to "not_a_real_class", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with "doctrine.repository_service". |
||
113 | */ |
||
114 | public function testCustomRepositoryIsNotAValidClass() : void |
||
115 | { |
||
116 | $container = $this->createContainer([]); |
||
117 | |||
118 | $em = $this->createEntityManager(['Foo\CoolEntity' => 'not_a_real_class']); |
||
119 | |||
120 | $factory = new ContainerRepositoryFactory($container); |
||
121 | $factory->getRepository($em, 'Foo\CoolEntity'); |
||
122 | } |
||
123 | |||
124 | private function createContainer(array $services) : MockObject |
||
125 | { |
||
126 | $container = $this->getMockBuilder(ContainerInterface::class)->getMock(); |
||
127 | $container->expects($this->any()) |
||
128 | ->method('has') |
||
129 | ->willReturnCallback(static function ($id) use ($services) { |
||
130 | return isset($services[$id]); |
||
131 | }); |
||
132 | $container->expects($this->any()) |
||
133 | ->method('get') |
||
134 | ->willReturnCallback(static function ($id) use ($services) { |
||
135 | return $services[$id]; |
||
136 | }); |
||
137 | |||
138 | return $container; |
||
139 | } |
||
140 | |||
141 | private function createEntityManager(array $entityRepositoryClasses) : MockObject |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Repository implementing non-deprecated interface, as current interface implemented in ORM\EntityRepository |
||
168 | * uses deprecated one and Composer autoload triggers deprecations that can't be silenced by @group legacy |
||
169 | */ |
||
170 | class NonDeprecatedRepository implements ObjectRepository |
||
171 | { |
||
172 | /** |
||
173 | * {@inheritDoc} |
||
211 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: