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 () { |
|
|
|
|
44
|
|
|
switch (func_get_arg(0)) { |
45
|
|
|
case 'messages': |
46
|
|
|
return 10; |
47
|
|
|
break; |
|
|
|
|
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)); |
|
|
|
|
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
|
|
|
|
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.