CommandHandlerTest::testInvokeExceptionInCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 28
rs 9.6333
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TogglJiraTest\Handler;
5
6
use Psr\Log\LoggerInterface;
7
use TogglJira\Command\CommandInterface;
8
use TogglJira\Exception\CommandNotFoundException;
9
use TogglJira\Handler\CommandHandler;
10
use TogglJiraTest\BaseContainerTest;
11
use Zend\Console\Adapter\AdapterInterface;
12
use Zend\Console\Request;
13
use Zend\Stdlib\Parameters;
14
use ZF\Console\Route;
15
16
class CommandHandlerTest extends BaseContainerTest
17
{
18
    /**
19
     * @return void
20
     * @throws CommandNotFoundException
21
     * @throws \Psr\Container\ContainerExceptionInterface
22
     * @throws \Psr\Container\NotFoundExceptionInterface
23
     */
24
    public function testInvoke(): void
25
    {
26
        $route = \Mockery::mock(Route::class);
27
        $route->shouldReceive('getName')->once()->andReturn('gimmeSomething');
28
        $route->shouldReceive('getMatches')->once()->andReturn(['test' => true]);
29
30
        $console = \Mockery::mock(AdapterInterface::class);
31
32
        $request = \Mockery::mock(Request::class);
33
        $params = \Mockery::mock(Parameters::class);
34
        $params->shouldReceive('fromArray')->once()->with(['test' => true]);
35
        $request->shouldReceive('getParams')->once()->andReturn($params);
36
37
        $executeCommand = \Mockery::mock(CommandInterface::class);
38
        $executeCommand->shouldReceive('execute')->once()->withArgs([$request, $console])->andReturn(0);
39
40
        $container = $this->getContainer();
41
        $container->shouldReceive('has')->once()->with('gimmeSomething')->andReturn(true);
42
        $container->shouldReceive('get')
43
            ->once()
44
            ->with('gimmeSomething')
45
            ->andReturn($executeCommand);
46
47
        $container->shouldReceive('get')->once()->with(Request::class)->andReturn($request);
48
49
        $command = new CommandHandler($container);
50
        $this->assertSame(0, $command->__invoke($route, $console));
51
    }
52
53
    /**
54
     * @expectedException \TogglJira\Exception\CommandNotFoundException
55
     * @return void
56
     * @throws CommandNotFoundException
57
     * @throws \Psr\Container\ContainerExceptionInterface
58
     * @throws \Psr\Container\NotFoundExceptionInterface
59
     */
60
    public function testInvokeException(): void
61
    {
62
        $route = \Mockery::mock(Route::class);
63
        $route->shouldReceive('getName')->once()->andReturn('gimmeSomething');
64
        $route->shouldReceive('getMatches')->once()->andReturn(['test' => true]);
65
66
        $console = \Mockery::mock(AdapterInterface::class);
67
68
        $container = $this->getContainer();
69
        $container->shouldReceive('has')->once()->with('gimmeSomething')->andReturn(false);
70
71
        $command = new CommandHandler($container);
72
        $this->assertSame(1, $command->__invoke($route, $console));
73
    }
74
75
    /**
76
     * @return void
77
     * @throws CommandNotFoundException
78
     * @throws \Psr\Container\ContainerExceptionInterface
79
     * @throws \Psr\Container\NotFoundExceptionInterface
80
     */
81
    public function testInvokeExceptionInCommand(): void
82
    {
83
        $route = \Mockery::mock(Route::class);
84
        $route->shouldReceive('getName')->once()->andReturn('mockCommand');
85
        $route->shouldReceive('getMatches')->once()->andReturn(['test' => true]);
86
87
        $request = \Mockery::mock(Request::class);
88
        $params = \Mockery::mock(Parameters::class);
89
        $params->shouldReceive('fromArray')->once()->with(['test' => true]);
90
        $request->shouldReceive('getParams')->once()->andReturn($params);
91
92
        $console = \Mockery::mock(AdapterInterface::class);
93
94
        $commandMock = \Mockery::mock(CommandInterface::class);
95
        $commandMock->shouldReceive('execute')->andThrow(\Exception::class);
96
97
        $container = $this->getContainer();
98
        $container->shouldReceive('has')->once()->with('mockCommand')->andReturn(true);
99
        $container->shouldReceive('get')->once()->with('mockCommand')->andReturn($commandMock);
100
        $container->shouldReceive('get')->once()->with(Request::class)->andReturn($request);
101
102
        $loggerMock = \Mockery::mock(LoggerInterface::class);
103
        $loggerMock->shouldReceive('error')->once();
104
105
        $command = new CommandHandler($container);
106
107
        $command->setLogger($loggerMock);
108
        $this->assertSame(1, $command->__invoke($route, $console));
109
    }
110
}
111