|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ozean12\GooglePubSubBundle\Tests\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Google\Cloud\Core\Exception\ConflictException; |
|
6
|
|
|
use Google\Cloud\PubSub\PubSubClient; |
|
7
|
|
|
use Google\Cloud\PubSub\Topic; |
|
8
|
|
|
use JMS\Serializer\Serializer; |
|
9
|
|
|
use JMS\Serializer\SerializerBuilder; |
|
10
|
|
|
use Ozean12\GooglePubSubBundle\Service\Publisher\Publisher; |
|
11
|
|
|
use Ozean12\GooglePubSubBundle\Tests\DTO\TestPublishMessageDTO; |
|
12
|
|
|
use Ozean12\GooglePubSubBundle\Tests\DTO\TestPublishMessageResultDTO; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class PublisherTest |
|
16
|
|
|
*/ |
|
17
|
|
|
class PublisherTest extends \PHPUnit_Framework_TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
const TOPIC = 'test_topic'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Serializer|\PHPUnit_Framework_MockObject_MockObject |
|
23
|
|
|
*/ |
|
24
|
|
|
private $serializer; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* SetUp |
|
28
|
|
|
*/ |
|
29
|
|
|
public function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->serializer = $this->getMockBuilder('JMS\Serializer\Serializer') |
|
32
|
|
|
->disableOriginalConstructor() |
|
33
|
|
|
->getMock() |
|
34
|
|
|
; |
|
35
|
|
|
|
|
36
|
|
|
parent::setUp(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @dataProvider getTestPublishData |
|
41
|
|
|
* |
|
42
|
|
|
* @param Topic|\PHPUnit_Framework_MockObject_MockObject $topic |
|
43
|
|
|
* @param PubSubClient|\PHPUnit_Framework_MockObject_MockObject $client |
|
44
|
|
|
* @param bool $setLogger |
|
45
|
|
|
* @param int $logInfoCallsCount |
|
46
|
|
|
* @param string $message |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testPublish(Topic $topic, PubSubClient $client, $setLogger, $logInfoCallsCount, $message) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$publisher = new Publisher(self::TOPIC, $client, $this->serializer); |
|
51
|
|
|
|
|
52
|
|
|
$message = new TestPublishMessageDTO(uniqid('test_')); |
|
53
|
|
|
$result = (new TestPublishMessageResultDTO()) |
|
54
|
|
|
->setMessageIds([uniqid('test_')]) |
|
55
|
|
|
; |
|
56
|
|
|
|
|
57
|
|
|
$serializer = SerializerBuilder::create()->build(); |
|
58
|
|
|
$serializedData = $serializer->serialize($message, 'json'); |
|
59
|
|
|
$serializedResult = $serializer->toArray($result); |
|
60
|
|
|
|
|
61
|
|
|
$this->serializer |
|
|
|
|
|
|
62
|
|
|
->expects($this->once()) |
|
63
|
|
|
->method('serialize') |
|
64
|
|
|
->with($message, 'json') |
|
65
|
|
|
->willReturn($serializedData) |
|
66
|
|
|
; |
|
67
|
|
|
|
|
68
|
|
|
$this->serializer |
|
69
|
|
|
->expects($this->once()) |
|
70
|
|
|
->method('fromArray') |
|
71
|
|
|
->with($serializedResult) |
|
72
|
|
|
->willReturn($result) |
|
73
|
|
|
; |
|
74
|
|
|
|
|
75
|
|
|
$topic |
|
|
|
|
|
|
76
|
|
|
->expects($this->once()) |
|
77
|
|
|
->method('publish') |
|
78
|
|
|
->with(['data' => $serializedData], []) |
|
79
|
|
|
->willReturn($serializedResult) |
|
80
|
|
|
; |
|
81
|
|
|
|
|
82
|
|
|
if ($setLogger) { |
|
83
|
|
|
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface') |
|
84
|
|
|
->disableOriginalConstructor() |
|
85
|
|
|
->getMock() |
|
86
|
|
|
; |
|
87
|
|
|
|
|
88
|
|
|
$logger |
|
89
|
|
|
->expects($this->exactly($logInfoCallsCount)) |
|
90
|
|
|
->method('info') |
|
91
|
|
|
; |
|
92
|
|
|
|
|
93
|
|
|
$publisher->setLogger($logger); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$actualResult = $publisher->publish($message); |
|
97
|
|
|
$this->assertEquals($result, $actualResult); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getTestPublishData() |
|
104
|
|
|
{ |
|
105
|
|
|
$newTopic = $this->getTopicMock(); |
|
106
|
|
|
$newTopicWithLogger = $this->getTopicMock(); |
|
107
|
|
|
$existingTopic = $this->getTopicMock(); |
|
108
|
|
|
$existingTopicWithLogger = $this->getTopicMock(); |
|
109
|
|
|
|
|
110
|
|
|
$newTopicClient = $this->getClientMock(); |
|
111
|
|
|
$newTopicClientWithLogger = $this->getClientMock(); |
|
112
|
|
|
$existingTopicClient = $this->getClientMock(); |
|
113
|
|
|
$existingTopicClientWithLogger = $this->getClientMock(); |
|
114
|
|
|
|
|
115
|
|
|
$newTopicClient->expects($this->once())->method('createTopic')->with(self::TOPIC)->willReturn($newTopic); |
|
|
|
|
|
|
116
|
|
|
$newTopicClientWithLogger->expects($this->once())->method('createTopic')->with(self::TOPIC)->willReturn($newTopicWithLogger); |
|
117
|
|
|
|
|
118
|
|
|
$existingTopicClient->expects($this->once())->method('createTopic')->willThrowException(new ConflictException('test')); |
|
119
|
|
|
$existingTopicClient->expects($this->once())->method('topic')->with(self::TOPIC)->willReturn($existingTopic); |
|
120
|
|
|
|
|
121
|
|
|
$existingTopicClientWithLogger->expects($this->once())->method('createTopic')->willThrowException(new ConflictException('test')); |
|
122
|
|
|
$existingTopicClientWithLogger->expects($this->once())->method('topic')->with(self::TOPIC)->willReturn($existingTopicWithLogger); |
|
123
|
|
|
|
|
124
|
|
|
return [ |
|
125
|
|
|
[$newTopic, $newTopicClient, false, 0, 'New topic'], |
|
126
|
|
|
[$newTopicWithLogger, $newTopicClientWithLogger, true, 2, 'New topic with logger'], |
|
127
|
|
|
[$existingTopic, $existingTopicClient, false, 0, 'Existing topic'], |
|
128
|
|
|
[$existingTopicWithLogger, $existingTopicClientWithLogger, true, 1, 'Existing topic with logger'], |
|
129
|
|
|
]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return Topic|\PHPUnit_Framework_MockObject_MockObject |
|
134
|
|
|
*/ |
|
135
|
|
|
private function getTopicMock() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->getMockBuilder('Google\Cloud\PubSub\Topic') |
|
138
|
|
|
->disableOriginalConstructor() |
|
139
|
|
|
->getMock() |
|
140
|
|
|
; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return PubSubClient|\PHPUnit_Framework_MockObject_MockObject |
|
145
|
|
|
*/ |
|
146
|
|
|
private function getClientMock() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->getMockBuilder('Google\Cloud\PubSub\PubSubClient') |
|
149
|
|
|
->disableOriginalConstructor() |
|
150
|
|
|
->getMock() |
|
151
|
|
|
; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.