BuildCommandTest   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 1
lcom 1
cbo 6
dl 0
loc 97
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B testRun() 0 94 1
1
<?php
2
3
namespace JK\SamBundle\Tests\Command;
4
5
use JK\SamBundle\Command\BuildCommand;
6
use JK\SamBundle\Event\Subscriber\NotificationSubscriber;
7
use PHPUnit\Framework\MockObject\MockObject;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\Console\Input\ArrayInput;
10
use Symfony\Component\Console\Output\BufferedOutput;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
14
class BuildCommandTest extends TestCase
15
{
16
    public function testRun()
17
    {
18
        /** @var ContainerInterface|MockObject $container */
19
        $container = $this
20
            ->getMockBuilder(ContainerInterface::class)
21
            ->disableOriginalConstructor()
22
            ->getMock()
23
        ;
24
        
25
        // mock event dispatcher
26
        $eventDispatcher = $this
27
            ->getMockBuilder(EventDispatcherInterface::class)
28
            ->disableOriginalConstructor()
29
            ->getMock()
30
        ;
31
        
32
        // mock assets configuration
33
        $configuration = [
34
            'debug' => true,
35
            'filters' => [
36
                'compass' => [],
37
                'merge' => [],
38
                'minify' => [],
39
                'copy' => [],
40
            ],
41
            'tasks' => [
42
                'main.css' => [
43
                    'filters' => [
44
                        'compass',
45
                        'merge',
46
                        'minify',
47
                        'copy',
48
                    ],
49
                    'sources' => [
50
                        'tests/Resources/assets/scss/main.scss',
51
                        'tests/Resources/assets/scss/custom.scss',
52
                    ],
53
                    'destinations' => [
54
                        'web/css/main.css',
55
                    ],
56
                ],
57
            ],
58
        ];
59
        
60
        // mock notification subscriber
61
        $notificationSubscriber = $this
62
            ->getMockBuilder(NotificationSubscriber::class)
63
            ->getMock()
64
        ;
65
        $notificationSubscriber
66
            ->expects($this->atLeastOnce())
67
            ->method('getNotifications')
68
            ->willReturn([
69
                'Success !!!',
70
            ])
71
        ;
72
        
73
        $container
74
            ->method('getParameter')
75
            ->willReturnMap([
76
                ['jk.assets', $configuration],
77
                ['kernel.root_dir', realpath(__DIR__.'/../..')],
78
            ])
79
        ;
80
        $c = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
81
        $container
82
            ->method('get')
83
            ->willReturnMap([
84
                ['event_dispatcher', $c, $eventDispatcher],
85
                ['jk.assets.notification_subscriber', $c,  $notificationSubscriber]
86
            ])
87
        ;
88
        
89
        $command = new BuildCommand();
90
        $command->setContainer($container);
0 ignored issues
show
Bug introduced by
It seems like $container can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, JK\SamBundle\Command\Abs...Command::setContainer() does only seem to accept null|object<Symfony\Comp...ion\ContainerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
91
    
92
        $input = new ArrayInput([]);
93
        $output = new BufferedOutput();
94
        $command->run($input, $output);
95
    
96
        $content = $output->fetch();
97
    
98
        $this->assertContains('Building tasks', $content);
99
        $this->assertContains('Tasks build', $content);
100
        $this->assertContains('Building filters', $content);
101
        $this->assertContains('Filters build', $content);
102
        $this->assertContains('Running tasks', $content);
103
        $this->assertContains('Running main.css', $content);
104
        $this->assertContains('x Success !!!', $content);
105
        $this->assertContains('[OK] Assets build end', $content);
106
    
107
        $fileContent = 'body{color:blue}a{color:red}';
108
        $this->assertEquals(file_get_contents(__DIR__.'/../../web/css/main.css'), $fileContent);
109
    }
110
}
111