Completed
Pull Request — master (#524)
by
unknown
03:13
created

CliTest::testValidHelpers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModuleTest;
21
22
use DoctrineORMModule\Module;
23
use PHPUnit_Framework_TestCase;
24
use Symfony\Component\Console\Application;
25
use Zend\EventManager\Event;
26
27
/**
28
 * Tests used to verify that command line functionality is active
29
 *
30
 * @license MIT
31
 * @link    http://www.doctrine-project.org/
32
 * @author  Marco Pivetta <[email protected]>
33
 */
34
class CliTest extends PHPUnit_Framework_TestCase
35
{
36
    /**
37
     * @var \Zend\ServiceManager\ServiceManager
38
     */
39
    protected $serviceManager;
40
41
    /**
42
     * @var \Symfony\Component\Console\Application
43
     */
44
    protected $cli;
45
46
    /**
47
     * @var \Doctrine\ORM\EntityManager
48
     */
49
    protected $objectManager;
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function setUp()
55
    {
56
        $serviceManager     = ServiceManagerFactory::getServiceManager();
57
        /* @var $sharedEventManager \Zend\EventManager\SharedEventManagerInterface */
58
        $sharedEventManager = $serviceManager->get('SharedEventManager');
59
        /* @var $application \Zend\Mvc\Application */
60
        $application        = $serviceManager->get('Application');
61
        $invocations        = 0;
62
63
        $sharedEventManager->attach(
64
            'doctrine',
65
            'loadCli.post',
66
            function () use (&$invocations) {
67
                $invocations += 1;
68
            }
69
        );
70
71
        $application->bootstrap();
72
        $this->serviceManager = $serviceManager;
73
        $this->objectManager  = $serviceManager->get('doctrine.entitymanager.orm_default');
0 ignored issues
show
Documentation Bug introduced by
It seems like $serviceManager->get('do...tymanager.orm_default') can also be of type array. However, the property $objectManager is declared as type object<Doctrine\ORM\EntityManager>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
74
        $this->cli            = $serviceManager->get('doctrine.cli');
0 ignored issues
show
Documentation Bug introduced by
It seems like $serviceManager->get('doctrine.cli') can also be of type array. However, the property $cli is declared as type object<Symfony\Component\Console\Application>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
75
        $this->assertSame(1, $invocations);
76
    }
77
78
    public function testValidHelpers()
79
    {
80
        $helperSet = $this->cli->getHelperSet();
81
82
        /* @var $emHelper \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper */
83
        $emHelper = $helperSet->get('em');
84
        $this->assertInstanceOf('Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper', $emHelper);
85
        $this->assertSame($this->objectManager, $emHelper->getEntityManager());
86
87
        /* @var $dbHelper \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper */
88
        $dbHelper = $helperSet->get('db');
89
        $this->assertInstanceOf('Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper', $dbHelper);
90
        $this->assertSame($this->objectManager->getConnection(), $dbHelper->getConnection());
91
    }
92
93
    public function testOrmDefaultIsUsedAsTheEntityManagerIfNoneIsProvided()
94
    {
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->objectManager, $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'][] = '--object-manager=doctrine.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('object-manager'));
155
156
        $entityManagerOption = $command->getDefinition()->getOption('object-manager');
157
158
        $this->assertTrue($entityManagerOption->isValueOptional());
159
        $this->assertFalse($entityManagerOption->isValueRequired());
160
        $this->assertFalse($entityManagerOption->isArray());
161
        $this->assertNull($entityManagerOption->getShortcut());
162
        $this->assertSame('doctrine.entitymanager.orm_default', $entityManagerOption->getDefault());
163
        $this->assertSame('The name of the object manager to use.', $entityManagerOption->getDescription());
164
    }
165
166
    /**
167
     * @return array
168
     */
169
    public function dataProviderForTestValidCommands()
170
    {
171
        return [
172
            [
173
                'dbal:import',
174
                'Doctrine\DBAL\Tools\Console\Command\ImportCommand',
175
            ],
176
            [
177
                'dbal:run-sql',
178
                'Doctrine\DBAL\Tools\Console\Command\RunSqlCommand',
179
            ],
180
            [
181
                'orm:clear-cache:query',
182
                'Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand',
183
            ],
184
            [
185
                'orm:clear-cache:result',
186
                'Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand',
187
            ],
188
            [
189
                'orm:generate-proxies',
190
                'Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand',
191
            ],
192
            [
193
                'orm:ensure-production-settings',
194
                'Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand',
195
            ],
196
            [
197
                'orm:info',
198
                'Doctrine\ORM\Tools\Console\Command\InfoCommand',
199
            ],
200
            [
201
                'orm:schema-tool:create',
202
                'Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand',
203
            ],
204
            [
205
                'orm:schema-tool:update',
206
                'Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand',
207
            ],
208
            [
209
                'orm:schema-tool:drop',
210
                'Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand',
211
            ],
212
            [
213
                'orm:validate-schema',
214
                'Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand',
215
            ],
216
            [
217
                'orm:run-dql',
218
                'Doctrine\ORM\Tools\Console\Command\RunDqlCommand',
219
            ],
220
            [
221
                'migrations:generate',
222
                'Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand',
223
            ],
224
            [
225
                'migrations:diff',
226
                'Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand',
227
            ],
228
            [
229
                'migrations:execute',
230
                'Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand',
231
            ],
232
        ];
233
    }
234
}
235