|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OldSound\RabbitMqBundle\Tests\RabbitMq; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
6
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; |
|
7
|
|
|
use OldSound\RabbitMqBundle\Event\AMQPEvent; |
|
8
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\BaseAmqp; |
|
9
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\Consumer; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
|
|
12
|
|
|
class BaseAmqpTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
public function testLazyConnection() |
|
16
|
|
|
{ |
|
17
|
|
|
$connection = $this->getMockBuilder('PhpAmqpLib\Connection\AbstractConnection') |
|
18
|
|
|
->disableOriginalConstructor() |
|
19
|
|
|
->getMock(); |
|
20
|
|
|
|
|
21
|
|
|
$connection |
|
22
|
|
|
->method('connectOnConstruct') |
|
23
|
|
|
->willReturn(false); |
|
24
|
|
|
$connection |
|
25
|
|
|
->expects(static::never()) |
|
26
|
|
|
->method('channel'); |
|
27
|
|
|
|
|
28
|
|
|
new Consumer($connection, null); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testNotLazyConnection() |
|
32
|
|
|
{ |
|
33
|
|
|
$connection = $this->getMockBuilder('PhpAmqpLib\Connection\AbstractConnection') |
|
34
|
|
|
->disableOriginalConstructor() |
|
35
|
|
|
->getMock(); |
|
36
|
|
|
|
|
37
|
|
|
$connection |
|
38
|
|
|
->method('connectOnConstruct') |
|
39
|
|
|
->willReturn(true); |
|
40
|
|
|
$connection |
|
41
|
|
|
->expects(static::once()) |
|
42
|
|
|
->method('channel'); |
|
43
|
|
|
|
|
44
|
|
|
new Consumer($connection, null); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testDispatchEvent() |
|
48
|
|
|
{ |
|
49
|
|
|
/** @var BaseAmqp|MockObject $baseAmqpConsumer */ |
|
50
|
|
|
$baseAmqpConsumer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\BaseAmqp') |
|
51
|
|
|
->disableOriginalConstructor() |
|
52
|
|
|
->getMock(); |
|
53
|
|
|
|
|
54
|
|
|
$eventDispatcher = $this->getMockBuilder('Symfony\Contracts\EventDispatcher\EventDispatcherInterface') |
|
55
|
|
|
->disableOriginalConstructor() |
|
56
|
|
|
->getMock(); |
|
57
|
|
|
|
|
58
|
|
|
$baseAmqpConsumer->expects($this->atLeastOnce()) |
|
59
|
|
|
->method('getEventDispatcher') |
|
60
|
|
|
->willReturn($eventDispatcher); |
|
61
|
|
|
|
|
62
|
|
|
$eventDispatcher->expects($this->once()) |
|
63
|
|
|
->method('dispatch') |
|
64
|
|
|
->with(new AMQPEvent(), AMQPEvent::ON_CONSUME) |
|
65
|
|
|
->willReturn(new AMQPEvent()); |
|
66
|
|
|
|
|
67
|
|
|
$this->invokeMethod('dispatchEvent', $baseAmqpConsumer, array(AMQPEvent::ON_CONSUME, new AMQPEvent())); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $name |
|
72
|
|
|
* @param MockObject $obj |
|
73
|
|
|
* @param array $params |
|
74
|
|
|
* |
|
75
|
|
|
* @return mixed |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function invokeMethod($name, $obj, $params) |
|
78
|
|
|
{ |
|
79
|
|
|
$class = new \ReflectionClass(get_class($obj)); |
|
80
|
|
|
$method = $class->getMethod($name); |
|
81
|
|
|
$method->setAccessible(true); |
|
82
|
|
|
|
|
83
|
|
|
return $method->invokeArgs($obj, $params); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|