Completed
Push — master ( 5cf5b8...189f41 )
by Andreas
05:29 queued 05:22
created

CliTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 13
dl 0
loc 153
rs 10
c 3
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 23 1
A testValidHelpers() 0 14 1
A testValidCommands() 0 18 1
A dataProviderForTestValidCommands() 0 65 1
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 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');
74
        $this->cli            = $serviceManager->get('doctrine.cli');
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::class, $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::class, $dbHelper);
90
        $this->assertSame($this->objectManager->getConnection(), $dbHelper->getConnection());
91
    }
92
93
    /**
94
     * @param string $commandName
95
     * @param string $className
96
     *
97
     * @dataProvider dataProviderForTestValidCommands
98
     */
99
    public function testValidCommands($commandName, $className)
100
    {
101
        /* @var $command \Symfony\Component\Console\Command\Command */
102
        $command = $this->cli->get($commandName);
103
        $this->assertInstanceOf($className, $command);
104
105
        // check for the entity-manager option
106
        $this->assertTrue($command->getDefinition()->hasOption('object-manager'));
107
108
        $entityManagerOption = $command->getDefinition()->getOption('object-manager');
109
110
        $this->assertTrue($entityManagerOption->isValueOptional());
111
        $this->assertFalse($entityManagerOption->isValueRequired());
112
        $this->assertFalse($entityManagerOption->isArray());
113
        $this->assertNull($entityManagerOption->getShortcut());
114
        $this->assertSame('doctrine.entitymanager.orm_default', $entityManagerOption->getDefault());
115
        $this->assertSame('The name of the object manager to use.', $entityManagerOption->getDescription());
116
    }
117
118
    /**
119
     * @return array
120
     */
121
    public function dataProviderForTestValidCommands()
122
    {
123
        return [
124
            [
125
                'dbal:import',
126
                \Doctrine\DBAL\Tools\Console\Command\ImportCommand::class,
127
            ],
128
            [
129
                'dbal:run-sql',
130
                \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand::class,
131
            ],
132
            [
133
                'orm:clear-cache:query',
134
                \Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand::class,
135
            ],
136
            [
137
                'orm:clear-cache:result',
138
                \Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand::class,
139
            ],
140
            [
141
                'orm:generate-proxies',
142
                \Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand::class,
143
            ],
144
            [
145
                'orm:ensure-production-settings',
146
                \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand::class,
147
            ],
148
            [
149
                'orm:info',
150
                \Doctrine\ORM\Tools\Console\Command\InfoCommand::class,
151
            ],
152
            [
153
                'orm:schema-tool:create',
154
                \Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand::class,
155
            ],
156
            [
157
                'orm:schema-tool:update',
158
                \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand::class,
159
            ],
160
            [
161
                'orm:schema-tool:drop',
162
                \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand::class,
163
            ],
164
            [
165
                'orm:validate-schema',
166
                \Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand::class,
167
            ],
168
            [
169
                'orm:run-dql',
170
                \Doctrine\ORM\Tools\Console\Command\RunDqlCommand::class,
171
            ],
172
            [
173
                'migrations:generate',
174
                \Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand::class,
175
            ],
176
            [
177
                'migrations:diff',
178
                \Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand::class,
179
            ],
180
            [
181
                'migrations:execute',
182
                \Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand::class,
183
            ],
184
        ];
185
    }
186
}
187