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() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$container = $this->getContainer(true); |
73
|
|
|
$commandTester = $this->createCommandTester($container); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$this->assertEquals(0, $commandTester->execute([])); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testFail() |
79
|
|
|
{ |
80
|
|
|
$container = $this->getContainer(false); |
81
|
|
|
$commandTester = $this->createCommandTester($container); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$this->assertEquals(1, $commandTester->execute([])); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.