1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Doctrine Bundle |
5
|
|
|
* |
6
|
|
|
* The code was originally distributed inside the Symfony framework. |
7
|
|
|
* |
8
|
|
|
* (c) Fabien Potencier <[email protected]> |
9
|
|
|
* (c) Doctrine Project, Benjamin Eberlei <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Tests; |
16
|
|
|
|
17
|
|
|
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass; |
18
|
|
|
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension; |
19
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
20
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
21
|
|
|
use Doctrine\ORM\EntityRepository; |
22
|
|
|
use Doctrine\ORM\QueryBuilder; |
23
|
|
|
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestCustomClassRepoEntity; |
24
|
|
|
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestCustomServiceRepoEntity; |
25
|
|
|
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestDefaultRepoEntity; |
26
|
|
|
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomClassRepoRepository; |
27
|
|
|
use Fixtures\Bundles\RepositoryServiceBundle\RepositoryServiceBundle; |
28
|
|
|
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomServiceRepoRepository; |
29
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; |
30
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
31
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
32
|
|
|
|
33
|
|
|
class ServiceRepositoryTest extends TestCase |
34
|
|
|
{ |
35
|
|
|
protected function setUp() |
36
|
|
|
{ |
37
|
|
|
parent::setUp(); |
38
|
|
|
|
39
|
|
|
if (!class_exists('Doctrine\\ORM\\Version')) { |
40
|
|
|
$this->markTestSkipped('Doctrine ORM is not available.'); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testRepositoryServiceWiring() |
45
|
|
|
{ |
46
|
|
|
// needed for older versions of Doctrine |
47
|
|
|
AnnotationRegistry::registerFile(__DIR__.'/../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); |
48
|
|
|
|
49
|
|
|
$container = new ContainerBuilder(new ParameterBag(array( |
50
|
|
|
'kernel.name' => 'app', |
51
|
|
|
'kernel.debug' => false, |
52
|
|
|
'kernel.bundles' => array('RepositoryServiceBundle' => RepositoryServiceBundle::class), |
53
|
|
|
'kernel.cache_dir' => sys_get_temp_dir(), |
54
|
|
|
'kernel.environment' => 'test', |
55
|
|
|
'kernel.root_dir' => __DIR__.'/../../../../', // src dir |
56
|
|
|
))); |
57
|
|
|
$container->set('annotation_reader', new AnnotationReader()); |
58
|
|
|
$extension = new DoctrineExtension(); |
59
|
|
|
$container->registerExtension($extension); |
60
|
|
|
$extension->load(array(array( |
61
|
|
|
'dbal' => array( |
62
|
|
|
'driver' => 'pdo_mysql', |
63
|
|
|
'charset' => 'UTF8', |
64
|
|
|
), |
65
|
|
|
'orm' => array( |
66
|
|
|
'mappings' => array('RepositoryServiceBundle' => array( |
67
|
|
|
'type' => 'annotation', |
68
|
|
|
'dir' => __DIR__.'/DependencyInjection/Fixtures/Bundles/RepositoryServiceBundle/Entity', |
69
|
|
|
'prefix' => 'Fixtures\Bundles\RepositoryServiceBundle\Entity', |
70
|
|
|
)), |
71
|
|
|
), |
72
|
|
|
)), $container); |
73
|
|
|
|
74
|
|
|
$def = $container->register(TestCustomServiceRepoRepository::class, TestCustomServiceRepoRepository::class) |
75
|
|
|
->setPublic(false); |
76
|
|
|
|
77
|
|
|
// Symfony 2.7 compat - can be moved above later |
78
|
|
|
if (method_exists($container, 'setAutowired')) { |
79
|
|
|
$def->setAutowired(true); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Symfony 3.3 and higher: autowire definition so it receives the tags |
83
|
|
|
if (class_exists(ServiceLocatorTagPass::class)) { |
84
|
|
|
$def->setAutoconfigured(true); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$container->addCompilerPass(new ServiceRepositoryCompilerPass()); |
88
|
|
|
$container->compile(); |
89
|
|
|
|
90
|
|
|
$em = $container->get('doctrine.orm.default_entity_manager'); |
91
|
|
|
|
92
|
|
|
// traditional custom class repository |
93
|
|
|
$customClassRepo = $em->getRepository(TestCustomClassRepoEntity::class); |
94
|
|
|
$this->assertInstanceOf(TestCustomClassRepoRepository::class, $customClassRepo); |
95
|
|
|
// a smoke test, trying some methods |
96
|
|
|
$this->assertSame(TestCustomClassRepoEntity::class, $customClassRepo->getClassName()); |
97
|
|
|
$this->assertInstanceOf(QueryBuilder::class, $customClassRepo->createQueryBuilder('tc')); |
98
|
|
|
|
99
|
|
|
// generic EntityRepository |
100
|
|
|
$genericRepository = $em->getRepository(TestDefaultRepoEntity::class); |
101
|
|
|
$this->assertInstanceOf(EntityRepository::class, $genericRepository); |
102
|
|
|
$this->assertSame($genericRepository, $genericRepository = $em->getRepository(TestDefaultRepoEntity::class)); |
103
|
|
|
// a smoke test, trying one of the methods |
104
|
|
|
$this->assertSame(TestDefaultRepoEntity::class, $genericRepository->getClassName()); |
105
|
|
|
|
106
|
|
|
// Symfony 3.2 and lower should work normally in traditional cases (tested above) |
107
|
|
|
// the code below should *not* work (by design) |
108
|
|
|
if (!class_exists(ServiceLocatorTagPass::class)) { |
109
|
|
|
$message = '/Support for loading entities from the service container only works for Symfony 3\.3/'; |
110
|
|
|
if (method_exists($this, 'expectException')) { |
111
|
|
|
$this->expectException(\RuntimeException::class); |
112
|
|
|
$this->expectExceptionMessageRegExp($message); |
113
|
|
|
} else { |
114
|
|
|
// PHPUnit 4 compat |
115
|
|
|
$this->setExpectedException(\RuntimeException::class); |
|
|
|
|
116
|
|
|
$this->setExpectedExceptionMessage($message); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// custom service repository |
121
|
|
|
$customServiceRepo = $em->getRepository(TestCustomServiceRepoEntity::class); |
122
|
|
|
$this->assertSame($customServiceRepo, $container->get(TestCustomServiceRepoRepository::class)); |
123
|
|
|
// a smoke test, trying some methods |
124
|
|
|
$this->assertSame(TestCustomServiceRepoEntity::class, $customServiceRepo->getClassName()); |
125
|
|
|
$this->assertInstanceOf(QueryBuilder::class, $customServiceRepo->createQueryBuilder('tc')); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.