|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Tools; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
|
7
|
|
|
use Doctrine\ORM\Tools\EntityGenerator; |
|
8
|
|
|
use Doctrine\ORM\Tools\EntityRepositoryGenerator; |
|
9
|
|
|
use Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository; |
|
10
|
|
|
use Doctrine\Tests\Models\DDC3231\DDC3231User1; |
|
11
|
|
|
use Doctrine\Tests\Models\DDC3231\DDC3231User2; |
|
12
|
|
|
use Doctrine\Tests\OrmTestCase; |
|
13
|
|
|
use Doctrine\Tests\VerifyDeprecations; |
|
14
|
|
|
|
|
15
|
|
|
class EntityRepositoryGeneratorTest extends OrmTestCase |
|
16
|
|
|
{ |
|
17
|
|
|
use VerifyDeprecations; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var EntityGenerator |
|
21
|
|
|
*/ |
|
22
|
|
|
private $_generator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var EntityRepositoryGenerator |
|
26
|
|
|
*/ |
|
27
|
|
|
private $_repositoryGenerator; |
|
28
|
|
|
|
|
29
|
|
|
private $_tmpDir; |
|
30
|
|
|
private $_namespace; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @inheritdoc |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setUp() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->_namespace = uniqid('doctrine_'); |
|
38
|
|
|
$this->_tmpDir = \sys_get_temp_dir() . DIRECTORY_SEPARATOR . $this->_namespace; |
|
39
|
|
|
\mkdir($this->_tmpDir); |
|
40
|
|
|
|
|
41
|
|
|
$this->_generator = new EntityGenerator(); |
|
|
|
|
|
|
42
|
|
|
$this->_generator->setAnnotationPrefix(""); |
|
43
|
|
|
$this->_generator->setGenerateAnnotations(true); |
|
44
|
|
|
$this->_generator->setGenerateStubMethods(true); |
|
45
|
|
|
$this->_generator->setRegenerateEntityIfExists(false); |
|
46
|
|
|
$this->_generator->setUpdateEntityIfExists(true); |
|
47
|
|
|
$this->_generator->setFieldVisibility(EntityGenerator::FIELD_VISIBLE_PROTECTED); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
$this->_repositoryGenerator = new EntityRepositoryGenerator(); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function tearDown() |
|
56
|
|
|
{ |
|
57
|
|
|
$dirs = []; |
|
58
|
|
|
|
|
59
|
|
|
$ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_tmpDir)); |
|
60
|
|
|
foreach ($ri AS $file) { |
|
61
|
|
|
/* @var $file \SplFileInfo */ |
|
62
|
|
|
if ($file->isFile()) { |
|
63
|
|
|
\unlink($file->getPathname()); |
|
64
|
|
|
} elseif ($file->getBasename() === '.') { |
|
65
|
|
|
$dirs[] = $file->getRealPath(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
arsort($dirs); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($dirs as $dir) { |
|
72
|
|
|
\rmdir($dir); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** @after */ |
|
77
|
|
|
public function ensureTestGeneratedDeprecationMessages() : void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->assertHasDeprecationMessages(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @group DDC-3231 |
|
84
|
|
|
*/ |
|
85
|
|
|
public function testGeneratedEntityRepositoryClass() |
|
86
|
|
|
{ |
|
87
|
|
|
$em = $this->_getTestEntityManager(); |
|
88
|
|
|
$ns = $this->_namespace; |
|
89
|
|
|
|
|
90
|
|
|
$className = $ns . '\DDC3231User1Tmp'; |
|
91
|
|
|
$this->writeEntityClass(DDC3231User1::class, $className); |
|
92
|
|
|
|
|
93
|
|
|
$rpath = $this->writeRepositoryClass($className); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertFileExists($rpath); |
|
96
|
|
|
|
|
97
|
|
|
require $rpath; |
|
98
|
|
|
|
|
99
|
|
|
$repo = new \ReflectionClass($em->getRepository($className)); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertTrue($repo->inNamespace()); |
|
102
|
|
|
$this->assertSame($className . 'Repository', $repo->getName()); |
|
103
|
|
|
$this->assertSame(EntityRepository::class, $repo->getParentClass()->getName()); |
|
104
|
|
|
|
|
105
|
|
|
require_once __DIR__ . '/../../Models/DDC3231/DDC3231User1NoNamespace.php'; |
|
106
|
|
|
|
|
107
|
|
|
$className2 = 'DDC3231User1NoNamespaceTmp'; |
|
108
|
|
|
$this->writeEntityClass(\DDC3231User1NoNamespace::class, $className2); |
|
109
|
|
|
|
|
110
|
|
|
$rpath2 = $this->writeRepositoryClass($className2); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertFileExists($rpath2); |
|
113
|
|
|
|
|
114
|
|
|
require $rpath2; |
|
115
|
|
|
|
|
116
|
|
|
$repo2 = new \ReflectionClass($em->getRepository($className2)); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertFalse($repo2->inNamespace()); |
|
119
|
|
|
$this->assertSame($className2 . 'Repository', $repo2->getName()); |
|
120
|
|
|
$this->assertSame(EntityRepository::class, $repo2->getParentClass()->getName()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @group DDC-3231 |
|
125
|
|
|
*/ |
|
126
|
|
|
public function testGeneratedEntityRepositoryClassCustomDefaultRepository() |
|
127
|
|
|
{ |
|
128
|
|
|
$em = $this->_getTestEntityManager(); |
|
129
|
|
|
$ns = $this->_namespace; |
|
130
|
|
|
|
|
131
|
|
|
$className = $ns . '\DDC3231User2Tmp'; |
|
132
|
|
|
$this->writeEntityClass(DDC3231User2::class, $className); |
|
133
|
|
|
|
|
134
|
|
|
$rpath = $this->writeRepositoryClass($className, DDC3231EntityRepository::class); |
|
135
|
|
|
|
|
136
|
|
|
$this->assertNotNull($rpath); |
|
137
|
|
|
$this->assertFileExists($rpath); |
|
138
|
|
|
|
|
139
|
|
|
require $rpath; |
|
140
|
|
|
|
|
141
|
|
|
$repo = new \ReflectionClass($em->getRepository($className)); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertTrue($repo->inNamespace()); |
|
144
|
|
|
$this->assertSame($className . 'Repository', $repo->getName()); |
|
145
|
|
|
$this->assertSame(DDC3231EntityRepository::class, $repo->getParentClass()->getName()); |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
require_once __DIR__ . '/../../Models/DDC3231/DDC3231User2NoNamespace.php'; |
|
149
|
|
|
|
|
150
|
|
|
$className2 = 'DDC3231User2NoNamespaceTmp'; |
|
151
|
|
|
$this->writeEntityClass('DDC3231User2NoNamespace', $className2); |
|
152
|
|
|
|
|
153
|
|
|
$rpath2 = $this->writeRepositoryClass($className2, DDC3231EntityRepository::class); |
|
154
|
|
|
|
|
155
|
|
|
$this->assertNotNull($rpath2); |
|
156
|
|
|
$this->assertFileExists($rpath2); |
|
157
|
|
|
|
|
158
|
|
|
require $rpath2; |
|
159
|
|
|
|
|
160
|
|
|
$repo2 = new \ReflectionClass($em->getRepository($className2)); |
|
161
|
|
|
|
|
162
|
|
|
$this->assertFalse($repo2->inNamespace()); |
|
163
|
|
|
$this->assertSame($className2 . 'Repository', $repo2->getName()); |
|
164
|
|
|
$this->assertSame(DDC3231EntityRepository::class, $repo2->getParentClass()->getName()); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $className |
|
169
|
|
|
* @param string $newClassName |
|
170
|
|
|
*/ |
|
171
|
|
|
private function writeEntityClass($className, $newClassName) |
|
172
|
|
|
{ |
|
173
|
|
|
$cmf = new ClassMetadataFactory(); |
|
174
|
|
|
$em = $this->_getTestEntityManager(); |
|
175
|
|
|
|
|
176
|
|
|
$cmf->setEntityManager($em); |
|
177
|
|
|
|
|
178
|
|
|
$metadata = $cmf->getMetadataFor($className); |
|
179
|
|
|
$metadata->namespace = $this->_namespace; |
|
|
|
|
|
|
180
|
|
|
$metadata->name = $newClassName; |
|
|
|
|
|
|
181
|
|
|
$metadata->customRepositoryClassName = $newClassName . "Repository"; |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
$this->_generator->writeEntityClass($metadata, $this->_tmpDir); |
|
184
|
|
|
|
|
185
|
|
|
require $this->_tmpDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $newClassName) . ".php"; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param string $className |
|
190
|
|
|
* @param string $defaultRepository |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
|
|
private function writeRepositoryClass($className, $defaultRepository = null) |
|
194
|
|
|
{ |
|
195
|
|
|
$this->_repositoryGenerator->setDefaultRepositoryName($defaultRepository); |
|
196
|
|
|
|
|
197
|
|
|
$this->_repositoryGenerator->writeEntityRepositoryClass($className . 'Repository', $this->_tmpDir); |
|
198
|
|
|
|
|
199
|
|
|
return $this->_tmpDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $className) . 'Repository.php'; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|