testOrmDefaultIsUsedAsTheEntityManagerIfNoneIsProvided()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace DoctrineORMModuleTest\Listener;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
8
use DoctrineORMModule\CliConfigurator;
9
use DoctrineORMModuleTest\ServiceManagerFactory;
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Component\Console\Application;
12
13
/**
14
 * @license MIT
15
 * @link    http://www.doctrine-project.org/
16
 * @author  Nicolas Eeckeloo <[email protected]>
17
 */
18
class CliConfiguratorTest extends TestCase
19
{
20
    /**
21
     * @var \Laminas\ServiceManager\ServiceManager
22
     */
23
    protected $serviceManager;
24
25
    /**
26
     * @var \Doctrine\ORM\EntityManager
27
     */
28
    protected $objectManager;
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function setUp() : void
34
    {
35
        $this->serviceManager = ServiceManagerFactory::getServiceManager();
36
        $this->objectManager  = $this->serviceManager->get('doctrine.entitymanager.orm_default');
37
    }
38
39
    public function testOrmDefaultIsUsedAsTheEntityManagerIfNoneIsProvided()
40
    {
41
        $application = new Application();
42
43
        $cliConfigurator = new CliConfigurator($this->serviceManager);
44
        $cliConfigurator->configure($application);
45
46
        /* @var $entityManagerHelper \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper */
47
        $entityManagerHelper = $application->getHelperSet()->get('entityManager');
48
49
        $this->assertInstanceOf(EntityManagerHelper::class, $entityManagerHelper);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
        $this->assertSame($this->objectManager, $entityManagerHelper->getEntityManager());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
    }
52
53
    /**
54
     * @backupGlobals enabled
55
     */
56
    public function testEntityManagerUsedCanBeSpecifiedInCommandLineArgument()
57
    {
58
        $objectManagerName = 'doctrine.entitymanager.some_other_name';
59
60
        $connection = $this->getMockBuilder(Connection::class)
0 ignored issues
show
Bug introduced by
The method disableOriginalConstructor cannot be called on $this->getMockBuilder(\D...DBAL\Connection::class) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
61
            ->disableOriginalConstructor()
62
            ->getMock();
63
64
        $entityManager = $this->getMockbuilder(EntityManager::class)
0 ignored issues
show
Bug introduced by
The method disableOriginalConstructor cannot be called on $this->getMockbuilder(\D...M\EntityManager::class) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
65
            ->disableOriginalConstructor()
66
            ->getMock();
67
68
        $entityManager
69
            ->expects($this->atLeastOnce())
0 ignored issues
show
Bug introduced by
The method atLeastOnce() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
            ->method('getConnection')
71
            ->willReturn($connection);
72
73
        $this->serviceManager->setService($objectManagerName, $entityManager);
74
75
        $application = new Application();
76
77
        $_SERVER['argv'][] = '--object-manager=' . $objectManagerName;
78
79
        $cliConfigurator = new CliConfigurator($this->serviceManager);
80
        $cliConfigurator->configure($application);
81
82
        /* @var $entityManagerHelper \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper */
83
        $entityManagerHelper = $application->getHelperSet()->get('entityManager');
84
85
        $this->assertInstanceOf(EntityManagerHelper::class, $entityManagerHelper);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
        $this->assertSame($entityManager, $entityManagerHelper->getEntityManager());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
    }
88
89
    public function testValidHelpers()
90
    {
91
        $application = new Application();
92
93
        $cliConfigurator = new CliConfigurator($this->serviceManager);
94
        $cliConfigurator->configure($application);
95
96
        $helperSet = $application->getHelperSet();
97
98
        /* @var $emHelper \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper */
99
        $emHelper = $helperSet->get('em');
100
        $this->assertInstanceOf(\Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper::class, $emHelper);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
        $this->assertSame($this->objectManager, $emHelper->getEntityManager());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
103
        /* @var $dbHelper \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper */
104
        $dbHelper = $helperSet->get('db');
105
        $this->assertInstanceOf(\Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper::class, $dbHelper);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
        $this->assertSame($this->objectManager->getConnection(), $dbHelper->getConnection());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
    }
108
109
    /**
110
     * @param string $commandName
111
     * @param string $className
112
     *
113
     * @dataProvider dataProviderForTestValidCommands
114
     */
115
    public function testValidCommands($commandName, $className)
116
    {
117
        $application = new Application();
118
119
        $cliConfigurator = new CliConfigurator($this->serviceManager);
120
        $cliConfigurator->configure($application);
121
122
        /* @var $command \Symfony\Component\Console\Command\Command */
123
        $command = $application->get($commandName);
124
        $this->assertInstanceOf($className, $command);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
126
        // check for the entity-manager option
127
        $this->assertTrue($command->getDefinition()->hasOption('object-manager'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
129
        $entityManagerOption = $command->getDefinition()->getOption('object-manager');
130
131
        $this->assertTrue($entityManagerOption->isValueOptional());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
        $this->assertFalse($entityManagerOption->isValueRequired());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
        $this->assertFalse($entityManagerOption->isArray());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
        $this->assertNull($entityManagerOption->getShortcut());
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
        $this->assertSame('doctrine.entitymanager.orm_default', $entityManagerOption->getDefault());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
        $this->assertSame('The name of the object manager to use.', $entityManagerOption->getDescription());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DoctrineORMModule...er\CliConfiguratorTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
    }
138
139
    /**
140
     * @return array
141
     */
142
    public function dataProviderForTestValidCommands()
143
    {
144
        return [
145
            [
146
                'dbal:import',
147
                \Doctrine\DBAL\Tools\Console\Command\ImportCommand::class,
148
            ],
149
            [
150
                'dbal:run-sql',
151
                \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand::class,
152
            ],
153
            [
154
                'orm:clear-cache:query',
155
                \Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand::class,
156
            ],
157
            [
158
                'orm:clear-cache:result',
159
                \Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand::class,
160
            ],
161
            [
162
                'orm:generate-proxies',
163
                \Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand::class,
164
            ],
165
            [
166
                'orm:ensure-production-settings',
167
                \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand::class,
168
            ],
169
            [
170
                'orm:info',
171
                \Doctrine\ORM\Tools\Console\Command\InfoCommand::class,
172
            ],
173
            [
174
                'orm:schema-tool:create',
175
                \Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand::class,
176
            ],
177
            [
178
                'orm:schema-tool:update',
179
                \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand::class,
180
            ],
181
            [
182
                'orm:schema-tool:drop',
183
                \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand::class,
184
            ],
185
            [
186
                'orm:validate-schema',
187
                \Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand::class,
188
            ],
189
            [
190
                'orm:run-dql',
191
                \Doctrine\ORM\Tools\Console\Command\RunDqlCommand::class,
192
            ],
193
            [
194
                'migrations:generate',
195
                \Doctrine\Migrations\Tools\Console\Command\GenerateCommand::class,
196
            ],
197
            [
198
                'migrations:diff',
199
                \Doctrine\Migrations\Tools\Console\Command\DiffCommand::class,
200
            ],
201
            [
202
                'migrations:execute',
203
                \Doctrine\Migrations\Tools\Console\Command\ExecuteCommand::class,
204
            ],
205
        ];
206
    }
207
}
208