Passed
Pull Request — master (#278)
by Asmir
03:32
created

DoctrineCommandsTest::getApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 25
rs 9.7333
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Bundle\MigrationsBundle\Tests\DependencyInjection;
6
7
use Doctrine\Bundle\MigrationsBundle\DependencyInjection\DoctrineMigrationsExtension;
8
use Doctrine\Migrations\Tools\Console\Command\DiffCommand;
9
use Doctrine\Migrations\Tools\Console\Command\DumpSchemaCommand;
10
use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand;
11
use Doctrine\Migrations\Tools\Console\Command\GenerateCommand;
12
use Doctrine\Migrations\Tools\Console\Command\LatestCommand;
13
use Doctrine\Migrations\Tools\Console\Command\ListCommand;
14
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand;
15
use Doctrine\Migrations\Tools\Console\Command\RollupCommand;
16
use Doctrine\Migrations\Tools\Console\Command\StatusCommand;
17
use Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand;
18
use Doctrine\Migrations\Tools\Console\Command\UpToDateCommand;
19
use Doctrine\Migrations\Tools\Console\Command\VersionCommand;
20
use Doctrine\ORM\EntityManager;
21
use PHPUnit\Framework\MockObject\MockObject;
22
use PHPUnit\Framework\TestCase;
23
use Symfony\Bundle\FrameworkBundle\Console\Application;
24
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
25
use Symfony\Component\DependencyInjection\ContainerBuilder;
26
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
27
use Symfony\Component\HttpKernel\KernelInterface;
28
use function sys_get_temp_dir;
29
30
class DoctrineCommandsTest extends TestCase
31
{
32
    /**
33
     * @dataProvider getCommands
34
     */
35
    public function testCommandRegistered(string $name, string $instance) : void
36
    {
37
        $application = $this->getApplication();
38
39
        self::assertInstanceOf($instance, $application->find($name));
40
    }
41
42
    /**
43
     * @return string[][]
44
     */
45
    public function getCommands() : array
46
    {
47
        return [
48
            ['doctrine:migrations:diff', DiffCommand::class],
49
            ['doctrine:migrations:dump-schema', DumpSchemaCommand::class],
50
            ['doctrine:migrations:execute', ExecuteCommand::class],
51
            ['doctrine:migrations:generate', GenerateCommand::class],
52
            ['doctrine:migrations:latest', LatestCommand::class],
53
            ['doctrine:migrations:list', ListCommand::class],
54
            ['doctrine:migrations:migrate', MigrateCommand::class],
55
            ['doctrine:migrations:rollup', RollupCommand::class],
56
            ['doctrine:migrations:status', StatusCommand::class],
57
            ['doctrine:migrations:sync-metadata-storage', SyncMetadataCommand::class],
58
            ['doctrine:migrations:up-to-date', UpToDateCommand::class],
59
            ['doctrine:migrations:version', VersionCommand::class],
60
        ];
61
    }
62
63
    /**
64
     * @return MockObject|KernelInterface
65
     */
66
    private function getKernel(ContainerBuilder $container)
67
    {
68
        $kernel = $this
69
            ->getMockBuilder(KernelInterface::class)
70
            ->getMock();
71
72
        $kernel
73
            ->expects(self::any())
74
            ->method('getContainer')
75
            ->willReturn($container);
76
77
        $kernel
78
            ->expects(self::once())
79
            ->method('getBundles')
80
            ->willReturn([]);
81
82
        return $kernel;
83
    }
84
85
    private function getApplication() : Application
86
    {
87
        $container = new ContainerBuilder(new ParameterBag([
88
            'kernel.debug' => false,
89
            'kernel.bundles' => [],
90
            'kernel.cache_dir' => sys_get_temp_dir(),
91
            'kernel.environment' => 'test',
92
            'kernel.project_dir' => __DIR__ . '/../',
93
        ]));
94
95
        $kernel      = $this->getKernel($container);
96
        $application = new Application($kernel);
97
        $container->set('application', $application);
98
99
        $em = $this->createMock(EntityManager::class);
100
        $container->set('doctrine.orm.default_entity_manager', $em);
101
102
        $container->addCompilerPass(new AddConsoleCommandPass());
103
104
        $extension = new DoctrineMigrationsExtension();
105
        $extension->load([], $container);
106
107
        $container->compile();
108
109
        return $application;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $application could return the type null which is incompatible with the type-hinted return Symfony\Bundle\FrameworkBundle\Console\Application. Consider adding an additional type-check to rule them out.
Loading history...
110
    }
111
}
112