/
GenerateRepositoriesCommandTest.php
178 lines (138 loc) · 6.16 KB
/
GenerateRepositoriesCommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Doctrine\Tests\ORM\Tools\Console\Command;
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\Tests\VerifyDeprecations;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Tester\CommandTester;
class GenerateRepositoriesCommandTest extends OrmFunctionalTestCase
{
use VerifyDeprecations;
/**
* @var \Symfony\Component\Console\Application
*/
private $application;
private $path;
/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();
$this->path = \sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('doctrine_');
\mkdir($this->path);
$metadataDriver = $this->_em->getConfiguration()->getMetadataDriverImpl();
$metadataDriver->addPaths([__DIR__ . '/../../../../Models/DDC3231/']);
$this->application = new Application();
$this->application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($this->_em)]));
$this->application->add(new GenerateRepositoriesCommand());
}
/**
* @inheritdoc
*/
public function tearDown()
{
$dirs = [];
$ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path));
foreach ($ri AS $file) {
/* @var $file \SplFileInfo */
if ($file->isFile()) {
\unlink($file->getPathname());
} elseif ($file->getBasename() === '.') {
$dirs[] = $file->getRealPath();
}
}
arsort($dirs);
foreach ($dirs as $dir) {
\rmdir($dir);
}
parent::tearDown();
}
public function testGenerateRepositories()
{
$this->generateRepositories('DDC3231User1');
$cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User1Repository';
$fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php';
self::assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname);
self::assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php');
require $this->path . DIRECTORY_SEPARATOR . $fname;
require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php';
self::assertTrue(class_exists($cname));
self::assertTrue(class_exists('DDC3231User1NoNamespaceRepository'));
$repo1 = new \ReflectionClass($cname);
$repo2 = new \ReflectionClass('DDC3231User1NoNamespaceRepository');
self::assertSame(EntityRepository::class, $repo1->getParentClass()->getName());
self::assertSame(EntityRepository::class, $repo2->getParentClass()->getName());
$this->assertHasDeprecationMessages();
}
public function testGenerateRepositoriesCustomDefaultRepository()
{
$this->generateRepositories('DDC3231User2', DDC3231EntityRepository::class);
$cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User2Repository';
$fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php';
self::assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname);
self::assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php');
require $this->path . DIRECTORY_SEPARATOR . $fname;
require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php';
self::assertTrue(class_exists($cname));
self::assertTrue(class_exists('DDC3231User2NoNamespaceRepository'));
$repo1 = new \ReflectionClass($cname);
$repo2 = new \ReflectionClass('DDC3231User2NoNamespaceRepository');
self::assertSame(DDC3231EntityRepository::class, $repo1->getParentClass()->getName());
self::assertSame(DDC3231EntityRepository::class, $repo2->getParentClass()->getName());
$this->assertHasDeprecationMessages();
}
/**
* @param string $filter
* @param string $defaultRepository
*/
private function generateRepositories($filter, $defaultRepository = null)
{
if ($defaultRepository) {
$this->_em->getConfiguration()->setDefaultRepositoryClassName($defaultRepository);
}
$command = $this->application->find('orm:generate-repositories');
$tester = new CommandTester($command);
$tester->execute(
[
'command' => $command->getName(),
'dest-path' => $this->path,
'--filter' => $filter,
]
);
}
public function testNoMetadataClassesToProcess() : void
{
$configuration = $this->createMock(Configuration::class);
$metadataFactory = $this->createMock(ClassMetadataFactory::class);
$em = $this->createMock(EntityManagerInterface::class);
$configuration->method('getDefaultRepositoryClassName')
->willReturn('fooRepository');
$metadataFactory->method('getAllMetadata')
->willReturn([]);
$em->method('getMetadataFactory')
->willReturn($metadataFactory);
$em->method('getConfiguration')
->willReturn($configuration);
$application = new Application();
$application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($em)]));
$application->add(new GenerateRepositoriesCommand());
$command = $application->find('orm:generate-repositories');
$tester = new CommandTester($command);
$tester->execute(
[
'command' => $command->getName(),
'dest-path' => $this->path,
]
);
self::assertContains('Command orm:generate-repositories is deprecated and will be removed in Doctrine ORM 3.0.', $tester->getDisplay());
self::assertContains('[OK] No Metadata Classes to process.', $tester->getDisplay());
}
}