forked from doctrine/DoctrineORMModule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCliConfiguratorTest.php
214 lines (181 loc) · 7.13 KB
/
CliConfiguratorTest.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
declare(strict_types=1);
namespace DoctrineORMModuleTest\Listener;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Tools\Console\Command\ReservedWordsCommand;
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
use Doctrine\Migrations\Tools\Console\Command\DiffCommand;
use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand;
use Doctrine\Migrations\Tools\Console\Command\GenerateCommand;
use Doctrine\Migrations\Tools\Console\Command\VersionCommand;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand;
use Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand;
use Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand;
use Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand;
use Doctrine\ORM\Tools\Console\Command\InfoCommand;
use Doctrine\ORM\Tools\Console\Command\RunDqlCommand;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand;
use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use DoctrineORMModule\CliConfigurator;
use DoctrineORMModuleTest\ServiceManagerFactory;
use Laminas\ServiceManager\ServiceManager;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use function assert;
use function class_exists;
/**
* @link http://www.doctrine-project.org/
*/
class CliConfiguratorTest extends TestCase
{
protected ServiceManager $serviceManager;
protected EntityManager $objectManager;
public function setUp(): void
{
$this->serviceManager = ServiceManagerFactory::getServiceManager();
$this->objectManager = $this->serviceManager->get('doctrine.entitymanager.orm_default');
}
public function testOrmDefaultIsUsedAsTheEntityManagerIfNoneIsProvided(): void
{
$application = new Application();
$cliConfigurator = new CliConfigurator($this->serviceManager);
$cliConfigurator->configure($application);
$entityManagerHelper = $application->getHelperSet()->get('entityManager');
assert($entityManagerHelper instanceof EntityManagerHelper);
$this->assertInstanceOf(EntityManagerHelper::class, $entityManagerHelper);
$this->assertSame($this->objectManager, $entityManagerHelper->getEntityManager());
}
/**
* @backupGlobals enabled
*/
public function testEntityManagerUsedCanBeSpecifiedInCommandLineArgument(): void
{
$objectManagerName = 'doctrine.entitymanager.some_other_name';
$connection = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->getMock();
$entityManager = $this->getMockbuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$entityManager
->method('getConnection')
->willReturn($connection);
$this->serviceManager->setService($objectManagerName, $entityManager);
$application = new Application();
$_SERVER['argv'][] = '--object-manager=' . $objectManagerName;
$cliConfigurator = new CliConfigurator($this->serviceManager);
$cliConfigurator->configure($application);
$entityManagerHelper = $application->getHelperSet()->get('entityManager');
assert($entityManagerHelper instanceof EntityManagerHelper);
$this->assertInstanceOf(EntityManagerHelper::class, $entityManagerHelper);
$this->assertSame($entityManager, $entityManagerHelper->getEntityManager());
}
public function testValidHelpers(): void
{
$application = new Application();
$cliConfigurator = new CliConfigurator($this->serviceManager);
$cliConfigurator->configure($application);
$helperSet = $application->getHelperSet();
$emHelper = $helperSet->get('em');
assert($emHelper instanceof EntityManagerHelper);
$this->assertInstanceOf(EntityManagerHelper::class, $emHelper);
$this->assertSame($this->objectManager, $emHelper->getEntityManager());
}
/**
* @dataProvider dataProviderForTestValidCommands
*/
public function testValidCommands(string $commandName, string $className): void
{
if (! class_exists(VersionCommand::class)) {
$this->markTestIncomplete(
'Migrations must be installed to run this test.'
);
}
$application = new Application();
$cliConfigurator = new CliConfigurator($this->serviceManager);
$cliConfigurator->configure($application);
$command = $application->get($commandName);
$this->assertInstanceOf($className, $command);
// check for the entity-manager option
$this->assertTrue($command->getDefinition()->hasOption('object-manager'));
$entityManagerOption = $command->getDefinition()->getOption('object-manager');
$this->assertTrue($entityManagerOption->isValueOptional());
$this->assertFalse($entityManagerOption->isValueRequired());
$this->assertFalse($entityManagerOption->isArray());
$this->assertNull($entityManagerOption->getShortcut());
$this->assertSame('doctrine.entitymanager.orm_default', $entityManagerOption->getDefault());
$this->assertSame('The name of the object manager to use.', $entityManagerOption->getDescription());
}
/**
* @return list<array{string, class-string}>
*/
public function dataProviderForTestValidCommands(): array
{
return [
[
'dbal:run-sql',
RunSqlCommand::class,
],
[
'dbal:reserved-words',
ReservedWordsCommand::class,
],
[
'orm:clear-cache:query',
QueryCommand::class,
],
[
'orm:clear-cache:result',
ResultCommand::class,
],
[
'orm:generate-proxies',
GenerateProxiesCommand::class,
],
[
'orm:ensure-production-settings',
EnsureProductionSettingsCommand::class,
],
[
'orm:info',
InfoCommand::class,
],
[
'orm:schema-tool:create',
CreateCommand::class,
],
[
'orm:schema-tool:update',
UpdateCommand::class,
],
[
'orm:schema-tool:drop',
DropCommand::class,
],
[
'orm:validate-schema',
ValidateSchemaCommand::class,
],
[
'orm:run-dql',
RunDqlCommand::class,
],
[
'migrations:generate',
GenerateCommand::class,
],
[
'migrations:diff',
DiffCommand::class,
],
[
'migrations:execute',
ExecuteCommand::class,
],
];
}
}