BaseConsumerCommandTest.php$1 ➔ __construct()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 5
rs 10
c 2
b 0
f 0
1
<?php
2
namespace NeedleProject\LaravelRabbitMq\Command;
3
4
use PHPUnit\Framework\TestCase;
5
use NeedleProject\LaravelRabbitMq\ConsumerInterface;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Style\SymfonyStyle;
9
10
class BaseConsumerCommandTest extends TestCase
11
{
12
    public function testHandleCollaboration()
13
    {
14
        $consumerMock = $this->createMock(ConsumerInterface::class);
15
        $inputInterfaceMock = $this->createMock(InputInterface::class);
16
        $outputInterfaceMock = $this->createMock(OutputInterface::class);
17
18
        $consumerCommand = new class($consumerMock, $inputInterfaceMock, $outputInterfaceMock) extends BaseConsumerCommand {
19
20
            private $consumerMock = null;
21
22
            public function __construct($consumerMock, $inputInterfaceMock, $outputInterfaceMock)
23
            {
24
                $this->input = $inputInterfaceMock;
25
                $this->output = $outputInterfaceMock;
26
                $this->consumerMock = $consumerMock;
27
            }
28
29
            protected function getConsumer(string $consumerAliasName): ConsumerInterface
30
            {
31
                return $this->consumerMock;
32
            }
33
        };
34
35
        // actual collaboration scenarios
36
        $inputInterfaceMock->expects($this->exactly(3))
37
            ->method('getOption')
38
            ->with($this->logicalOr(
39
                $this->equalTo('time'),
40
                $this->equalTo('messages'),
41
                $this->equalTo('memory')
42
            ))
43
            ->will($this->returnCallback(function () {
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\TestCase::returnCallback() has been deprecated: Use <code>$double->willReturnCallback()</code> instead of <code>$double->will($this->returnCallback())</code> ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

43
            ->will(/** @scrutinizer ignore-deprecated */ $this->returnCallback(function () {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
44
                switch (func_get_arg(0)) {
45
                    case 'messages':
46
                        return 10;
47
                        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
48
                    case 'time':
49
                        return 20;
50
                        break;
51
                    case 'memory':
52
                        return 128;
53
                        break;
54
                    case 'consumer':
55
                        return 'fooBar';
56
                        break;
57
                }
58
            }));
59
        $inputInterfaceMock->expects($this->once())
60
            ->method('getArgument')
61
            ->willReturn('fooBar');
62
63
        $consumerMock->expects($this->once())
64
            ->method('startConsuming')
65
            ->with(10, 20, 128)
66
            ->willReturn(null);
67
68
        $consumerCommand->handle();
69
    }
70
71
    public function testFailConsumeCollaboration()
72
    {
73
        $consumerMock = $this->createMock(ConsumerInterface::class);
74
        $inputInterfaceMock = $this->createMock(InputInterface::class);
75
        $outputInterfaceMock = $this->createMock(SymfonyStyle::class);
76
77
        $consumerCommand = new class($consumerMock, $inputInterfaceMock, $outputInterfaceMock) extends BaseConsumerCommand {
78
79
            private $consumerMock = null;
80
81
            public function __construct($consumerMock, $inputInterfaceMock, $outputInterfaceMock)
82
            {
83
                $this->input = $inputInterfaceMock;
84
                $this->consumerMock = $consumerMock;
85
                $this->output = $outputInterfaceMock;
86
            }
87
88
            protected function getConsumer(string $consumerAliasName): ConsumerInterface
89
            {
90
                return $this->consumerMock;
91
            }
92
        };
93
94
        // actual collaboration scenarios
95
        $inputInterfaceMock->expects($this->exactly(3))
96
            ->method('getOption')
97
            ->will($this->returnValue(2));
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\TestCase::returnValue() has been deprecated: Use <code>$double->willReturn()</code> instead of <code>$double->will($this->returnValue())</code> ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

97
            ->will(/** @scrutinizer ignore-deprecated */ $this->returnValue(2));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
98
        $inputInterfaceMock->expects($this->once())
99
            ->method('getArgument')
100
            ->willReturn('fooBar');
101
102
        $consumerMock->expects($this->once())
103
            ->method('startConsuming')
104
            ->will($this->throwException(new \Exception('foo')));
105
106
        $outputInterfaceMock->expects($this->once())
107
            ->method('error')
108
            ->with('foo')
109
            ->willReturn(null);
110
111
        $consumerCommand->handle();
112
    }
113
}
114