Completed
Push — master ( 90700d...2d56b7 )
by Amy
03:23
created

AbstractCommandTest::assertIsContainerAware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Mongrate\MongrateBundle\Tests\Command;
4
5
use Mongrate\MongrateBundle\MongrateBundle;
6
use Mongrate\MongrateBundle\Tests\TestKernel;
7
use Symfony\Component\Console\Application;
8
use Symfony\Bundle\FrameworkBundle\Console\Application as FrameworkApplication;
9
use Symfony\Component\Console\Input\ArrayInput;
10
use Symfony\Component\Console\Output\NullOutput;
11
use Symfony\Component\DependencyInjection\Container;
12
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
abstract class AbstractCommandTest extends \PHPUnit_Framework_TestCase
16
{
17
    protected $application;
18
19
    protected $config;
20
21
    public function setUp()
22
    {
23
        $this->application = new Application();
24
25
        $this->config = [
26
            'migrations_directory' => '/',
27
            'mongodb_server' => 'localhost',
28
            'mongodb_db' => 'test'
29
        ];
30
    }
31
32
    protected function assertCommandIsFound($name, $class)
33
    {
34
        $command = $this->application->find($name);
35
        $this->assertEquals($name, $command->getName());
36
        $this->assertNotEmpty($command->getDescription());
37
        $this->assertInstanceOf($class, $command);
38
39
        $this->assertCommandIsRegisteredInBundle($name, $class);
40
    }
41
42
    private function assertCommandIsRegisteredInBundle($name, $class)
43
    {
44
        $mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
45
            ->setMethods(['getParameter'])
46
            ->getMock();
47
        $mockContainer->expects($this->any())
48
            ->method('getParameter')
49
            ->with('mongrate_bundle')
50
            ->will($this->returnValue($this->config));
51
52
        $kernel = new TestKernel($mockContainer);
53
54
        $frameworkApplication = new FrameworkApplication($kernel);
55
56
        $bundle = new MongrateBundle();
57
        $bundle->registerCommands($frameworkApplication);
58
59
        $this->assertInstanceOf($class, $frameworkApplication->find($name));
60
    }
61
62
    public function assertIsContainerAware($name, $class)
63
    {
64
        self::assertInstanceOf(
65
            ContainerAwareInterface::class,
66
            new $class($this->config),
67
            sprintf('The %s command is not instance of ContainerAwareInterface', $name)
68
        );
69
    }
70
71
    public function assertContainerWasInjectedToService($name, $class)
72
    {
73
        $this->application->add(new $class($this->config));
74
75
        $command = $this->application->get($name);
76
77
        $command->setContainer(new Container());
78
        $command->initialize(New ArrayInput([]), new NullOutput());
0 ignored issues
show
Bug introduced by
The method initialize() cannot be called from this context as it is declared protected in class Symfony\Component\Console\Command\Command.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
79
80
        self::assertAttributeInstanceOf(
81
            ContainerInterface::class,
82
            'container',
83
            \PHPUnit_Framework_Assert::getObjectAttribute($command, 'service'),
84
            sprintf('Container wasn\'t injected to the MigrationService by %s command', $name)
85
        );
86
    }
87
}
88