TestConnectionCommandTest::testFail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MovingImage\Bundle\VMProApiBundle\Tests;
6
7
use MovingImage\Bundle\VMProApiBundle\Command\TestConnectionCommand;
8
use MovingImage\Client\VMPro\Entity\Channel;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Component\Console\Application;
11
use Symfony\Component\Console\Tester\CommandTester;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use MovingImage\Client\VMPro\ApiClient;
14
15
class TestConnectionCommandTest extends TestCase
16
{
17
    private function createCommandTester(ContainerInterface $container, Application $application = null)
18
    {
19
        if (null === $application) {
20
            $application = new Application();
21
        }
22
23
        $application->setAutoExit(false);
24
        $command = new TestConnectionCommand();
25
        $command->setContainer($container);
26
        $application->add($command);
27
28
        return new CommandTester($application->find('vmpro-api:test-connection'));
29
    }
30
31
    private function getContainer($success = true)
32
    {
33
        $container = $this->getMockBuilder(ContainerInterface::class)->getMock();
34
        $client = $this->getMockBuilder(ApiClient::class)
35
            ->disableOriginalConstructor()
36
            ->getMock();
37
38
        if (true === $success) {
39
            $channel = (new Channel())
40
                ->setName('Test');
41
42
            $client
43
                ->expects($this->once())
44
                ->method('getChannels')
45
                ->with(5)
46
                ->willReturn($channel);
47
        } else {
48
            $client
49
                ->expects($this->once())
50
                ->method('getChannels')
51
                ->with(5)
52
                ->will($this->throwException(new \Exception()));
53
        }
54
55
        $container
56
            ->expects($this->once())
57
            ->method('get')
58
            ->with('vmpro_api.client')
59
            ->willReturn($client);
60
61
        $container
62
            ->expects($this->once())
63
            ->method('getParameter')
64
            ->with('vm_pro_api_default_vm_id')
65
            ->willReturn(5);
66
67
        return $container;
68
    }
69
70
    public function testSuccess()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $container = $this->getContainer(true);
73
        $commandTester = $this->createCommandTester($container);
0 ignored issues
show
Documentation introduced by
$container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...ion\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
75
        $this->assertEquals(0, $commandTester->execute([]));
76
    }
77
78
    public function testFail()
79
    {
80
        $container = $this->getContainer(false);
81
        $commandTester = $this->createCommandTester($container);
0 ignored issues
show
Documentation introduced by
$container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...ion\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
83
        $this->assertEquals(1, $commandTester->execute([]));
84
    }
85
}
86