Skip to content

Commit 9f6b646

Browse files
author
Tommy Seus
committed
Added a option to use a different entity-manager on cli.
1 parent 928a657 commit 9f6b646

File tree

2 files changed

+170
-62
lines changed

2 files changed

+170
-62
lines changed

src/DoctrineORMModule/Module.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
use Symfony\Component\Console\Helper\DialogHelper;
2323
use Symfony\Component\Console\Helper\QuestionHelper;
24+
use Symfony\Component\Console\Input\ArgvInput;
25+
use Symfony\Component\Console\Input\InputOption;
2426
use Zend\ModuleManager\Feature\ControllerProviderInterface;
2527
use Zend\ModuleManager\Feature\ConfigProviderInterface;
2628
use Zend\ModuleManager\Feature\InitProviderInterface;
@@ -134,10 +136,25 @@ public function initializeConsole(EventInterface $event)
134136
);
135137
}
136138

137-
$cli->addCommands(array_map(array($serviceLocator, 'get'), $commands));
139+
foreach ($commands as $commandName) {
140+
/* @var $command \Symfony\Component\Console\Command\Command */
141+
$command = $serviceLocator->get($commandName);
142+
$command->getDefinition()->addOption(new InputOption(
143+
'entitymanager',
144+
null,
145+
InputOption::VALUE_OPTIONAL,
146+
'The name of the entitymanager to use. If none is provided, it will use orm_default.'
147+
));
148+
149+
$cli->add($command);
150+
}
151+
152+
$arguments = new ArgvInput();
153+
$entityManagerName = $arguments->getParameterOption('--entitymanager');
154+
$entityManagerName = !empty($entityManagerName) ? $entityManagerName : 'orm_default';
138155

139156
/* @var $entityManager \Doctrine\ORM\EntityManager */
140-
$entityManager = $serviceLocator->get('doctrine.entitymanager.orm_default');
157+
$entityManager = $serviceLocator->get('doctrine.entitymanager.' . $entityManagerName);
141158
$helperSet = $cli->getHelperSet();
142159

143160
if (class_exists('Symfony\Component\Console\Helper\QuestionHelper')) {

tests/DoctrineORMModuleTest/CliTest.php

Lines changed: 151 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919

2020
namespace DoctrineORMModuleTest;
2121

22+
use DoctrineORMModule\Module;
2223
use PHPUnit_Framework_TestCase;
24+
use Symfony\Component\Console\Application;
25+
use Zend\EventManager\Event;
2326

2427
/**
2528
* Tests used to verify that command line functionality is active
@@ -30,6 +33,11 @@
3033
*/
3134
class CliTest extends PHPUnit_Framework_TestCase
3235
{
36+
/**
37+
* @var \Zend\ServiceManager\ServiceManager
38+
*/
39+
protected $serviceManager;
40+
3341
/**
3442
* @var \Symfony\Component\Console\Application
3543
*/
@@ -61,8 +69,9 @@ function () use (&$invocations) {
6169
);
6270

6371
$application->bootstrap();
64-
$this->entityManager = $serviceManager->get('doctrine.entitymanager.orm_default');
65-
$this->cli = $serviceManager->get('doctrine.cli');
72+
$this->serviceManager = $serviceManager;
73+
$this->entityManager = $serviceManager->get('doctrine.entitymanager.orm_default');
74+
$this->cli = $serviceManager->get('doctrine.cli');
6675
$this->assertSame(1, $invocations);
6776
}
6877

@@ -81,65 +90,147 @@ public function testValidHelpers()
8190
$this->assertSame($this->entityManager->getConnection(), $dbHelper->getConnection());
8291
}
8392

84-
public function testValidCommands()
93+
public function testOrmDefaultIsUsedAsTheEntityManagerIfNoneIsProvided()
8594
{
86-
$this->assertInstanceOf('Doctrine\DBAL\Tools\Console\Command\ImportCommand', $this->cli->get('dbal:import'));
87-
$this->assertInstanceOf('Doctrine\DBAL\Tools\Console\Command\RunSqlCommand', $this->cli->get('dbal:run-sql'));
88-
$this->assertInstanceOf(
89-
'Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand',
90-
$this->cli->get('orm:clear-cache:metadata')
91-
);
92-
$this->assertInstanceOf(
93-
'Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand',
94-
$this->cli->get('orm:clear-cache:query')
95-
);
96-
$this->assertInstanceOf(
97-
'Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand',
98-
$this->cli->get('orm:clear-cache:result')
99-
);
100-
$this->assertInstanceOf(
101-
'Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand',
102-
$this->cli->get('orm:generate-proxies')
103-
);
104-
$this->assertInstanceOf(
105-
'Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand',
106-
$this->cli->get('orm:ensure-production-settings')
107-
);
108-
$this->assertInstanceOf(
109-
'Doctrine\ORM\Tools\Console\Command\InfoCommand',
110-
$this->cli->get('orm:info')
111-
);
112-
$this->assertInstanceOf(
113-
'Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand',
114-
$this->cli->get('orm:schema-tool:create')
115-
);
116-
$this->assertInstanceOf(
117-
'Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand',
118-
$this->cli->get('orm:schema-tool:update')
119-
);
120-
$this->assertInstanceOf(
121-
'Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand',
122-
$this->cli->get('orm:schema-tool:drop')
123-
);
124-
$this->assertInstanceOf(
125-
'Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand',
126-
$this->cli->get('orm:validate-schema')
127-
);
128-
$this->assertInstanceOf(
129-
'Doctrine\ORM\Tools\Console\Command\RunDqlCommand',
130-
$this->cli->get('orm:run-dql')
131-
);
132-
$this->assertInstanceOf(
133-
'Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand',
134-
$this->cli->get('migrations:generate')
135-
);
136-
$this->assertInstanceOf(
137-
'Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand',
138-
$this->cli->get('migrations:diff')
139-
);
140-
$this->assertInstanceOf(
141-
'Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand',
142-
$this->cli->get('migrations:execute')
95+
$application = new Application();
96+
$event = new Event('loadCli.post', $application, ['ServiceManager' => $this->serviceManager]);
97+
98+
$module = new Module();
99+
$module->initializeConsole($event);
100+
101+
/* @var $entityManagerHelper \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper */
102+
$entityManagerHelper = $application->getHelperSet()->get('entityManager');
103+
$this->assertInstanceOf('Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper', $entityManagerHelper);
104+
$this->assertSame($this->entityManager, $entityManagerHelper->getEntityManager());
105+
}
106+
107+
/**
108+
* @backupGlobals enabled
109+
*/
110+
public function testEntityManagerUsedCanBeSpecifiedInCommandLineArgument()
111+
{
112+
$connection = $this->getMockBuilder('Doctrine\DBAL\Connection')
113+
->disableOriginalConstructor()
114+
->getMock();
115+
116+
$entityManager = $this->getMockbuilder('Doctrine\ORM\EntityManager')
117+
->disableOriginalConstructor()
118+
->getMock();
119+
120+
$entityManager
121+
->expects($this->atLeastOnce())
122+
->method('getConnection')
123+
->willReturn($connection);
124+
125+
$this->serviceManager->setService('doctrine.entitymanager.some_other_name', $entityManager);
126+
127+
$application = new Application();
128+
$event = new Event('loadCli.post', $application, ['ServiceManager' => $this->serviceManager]);
129+
130+
$_SERVER['argv'][] = '--entitymanager=some_other_name';
131+
132+
$module = new Module();
133+
$module->initializeConsole($event);
134+
135+
/* @var $entityManagerHelper \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper */
136+
$entityManagerHelper = $application->getHelperSet()->get('entityManager');
137+
$this->assertInstanceOf('Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper', $entityManagerHelper);
138+
$this->assertSame($entityManager, $entityManagerHelper->getEntityManager());
139+
}
140+
141+
/**
142+
* @param string $commandName
143+
* @param string $className
144+
*
145+
* @dataProvider dataProviderForTestValidCommands
146+
*/
147+
public function testValidCommands($commandName, $className)
148+
{
149+
/* @var $command \Symfony\Component\Console\Command\Command */
150+
$command = $this->cli->get($commandName);
151+
$this->assertInstanceOf($className, $command);
152+
153+
// check for the entity-manager option
154+
$this->assertTrue($command->getDefinition()->hasOption('entitymanager'));
155+
156+
$entityManagerOption = $command->getDefinition()->getOption('entitymanager');
157+
158+
$this->assertTrue($entityManagerOption->isValueOptional());
159+
$this->assertFalse($entityManagerOption->isValueRequired());
160+
$this->assertFalse($entityManagerOption->isArray());
161+
$this->assertNull($entityManagerOption->getShortcut());
162+
$this->assertSame(
163+
'The name of the entitymanager to use. If none is provided, it will use orm_default.',
164+
$entityManagerOption->getDescription()
143165
);
144166
}
167+
168+
/**
169+
* @return array
170+
*/
171+
public function dataProviderForTestValidCommands()
172+
{
173+
return [
174+
[
175+
'dbal:import',
176+
'Doctrine\DBAL\Tools\Console\Command\ImportCommand',
177+
],
178+
[
179+
'dbal:run-sql',
180+
'Doctrine\DBAL\Tools\Console\Command\RunSqlCommand',
181+
],
182+
[
183+
'orm:clear-cache:query',
184+
'Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand',
185+
],
186+
[
187+
'orm:clear-cache:result',
188+
'Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand',
189+
],
190+
[
191+
'orm:generate-proxies',
192+
'Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand',
193+
],
194+
[
195+
'orm:ensure-production-settings',
196+
'Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand',
197+
],
198+
[
199+
'orm:info',
200+
'Doctrine\ORM\Tools\Console\Command\InfoCommand',
201+
],
202+
[
203+
'orm:schema-tool:create',
204+
'Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand',
205+
],
206+
[
207+
'orm:schema-tool:update',
208+
'Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand',
209+
],
210+
[
211+
'orm:schema-tool:drop',
212+
'Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand',
213+
],
214+
[
215+
'orm:validate-schema',
216+
'Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand',
217+
],
218+
[
219+
'orm:run-dql',
220+
'Doctrine\ORM\Tools\Console\Command\RunDqlCommand',
221+
],
222+
[
223+
'migrations:generate',
224+
'Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand',
225+
],
226+
[
227+
'migrations:diff',
228+
'Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand',
229+
],
230+
[
231+
'migrations:execute',
232+
'Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand',
233+
],
234+
];
235+
}
145236
}

0 commit comments

Comments
 (0)