-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKafkaProducerBuilderTest.php
189 lines (152 loc) · 5.42 KB
/
KafkaProducerBuilderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace PhpKafka\Tests\Unit\Kafka\Producer;
use PhpKafka\Exception\KafkaProducerException;
use PhpKafka\Message\Encoder\EncoderInterface;
use PhpKafka\Producer\KafkaProducerBuilder;
use PhpKafka\Producer\KafkaProducerInterface;
use PHPUnit\Framework\TestCase;
/**
* @covers \PhpKafka\Producer\KafkaProducerBuilder
*/
class KafkaProducerBuilderTest extends TestCase
{
/** @var $kafkaProducerBuilder KafkaProducerBuilder */
protected $kafkaProducerBuilder;
/**
* @return void
*/
public function setUp(): void
{
$this->kafkaProducerBuilder = KafkaProducerBuilder::create();
}
/**
* @return void
* @throws \ReflectionException
*/
public function testAddConfig(): void
{
$config = ['auto.offset.reset' => 'earliest'];
$clone = $this->kafkaProducerBuilder->withAdditionalConfig($config);
$config = ['auto.offset.reset' => 'latest'];
$clone = $clone->withAdditionalConfig($config);
$reflectionProperty = new \ReflectionProperty($clone, 'config');
$reflectionProperty->setAccessible(true);
self::assertSame($config, $reflectionProperty->getValue($clone));
}
/**
* @return void
* @throws \ReflectionException
*/
public function testAddBroker(): void
{
$clone = $this->kafkaProducerBuilder->withAdditionalBroker('localhost');
$reflectionProperty = new \ReflectionProperty($clone, 'brokers');
$reflectionProperty->setAccessible(true);
self::assertSame(['localhost'], $reflectionProperty->getValue($clone));
}
/**
* @return void
* @throws \ReflectionException
*/
public function testSetEncoder(): void
{
$encoder = $this->getMockForAbstractClass(EncoderInterface::class);
$clone = $this->kafkaProducerBuilder->withEncoder($encoder);
$reflectionProperty = new \ReflectionProperty($clone, 'encoder');
$reflectionProperty->setAccessible(true);
self::assertInstanceOf(EncoderInterface::class, $reflectionProperty->getValue($clone));
}
/**
* @return void
* @throws \ReflectionException
*/
public function testSetDeliveryReportCallback(): void
{
$callback = function () {
// Anonymous test method, no logic required
};
$clone = $this->kafkaProducerBuilder->withDeliveryReportCallback($callback);
$reflectionProperty = new \ReflectionProperty($clone, 'deliverReportCallback');
$reflectionProperty->setAccessible(true);
self::assertSame($callback, $reflectionProperty->getValue($clone));
}
/**
* @return void
* @throws \ReflectionException
*/
public function testSetErrorCallback(): void
{
$callback = function () {
// Anonymous test method, no logic required
};
$clone = $this->kafkaProducerBuilder->withErrorCallback($callback);
$reflectionProperty = new \ReflectionProperty($clone, 'errorCallback');
$reflectionProperty->setAccessible(true);
self::assertSame($callback, $reflectionProperty->getValue($clone));
}
/**
* @return void
* @throws \ReflectionException
*/
public function testSetOAuthBearerTokenRefreshCallback(): void
{
$callback = function () {
// Anonymous test method, no logic required
};
$clone = $this->kafkaProducerBuilder->withOAuthBearerTokenRefreshCallback($callback);
$reflectionProperty = new \ReflectionProperty($clone, 'oauthBearerCallback');
$reflectionProperty->setAccessible(true);
self::assertSame($callback, $reflectionProperty->getValue($clone));
}
/**
* @throws KafkaProducerException
*/
public function testBuildNoBroker(): void
{
self::expectException(KafkaProducerException::class);
$this->kafkaProducerBuilder->build();
}
/**
* @return void
*/
public function testBuild(): void
{
$callback = function ($kafka, $errId, $msg) {
// Anonymous test method, no logic required
};
$producer = $this->kafkaProducerBuilder
->withAdditionalBroker('localhost')
->withDeliveryReportCallback($callback)
->withErrorCallback($callback)
->withLogCallback($callback)
->withOAuthBearerTokenRefreshCallback($callback)
->build();
self::assertInstanceOf(KafkaProducerInterface::class, $producer);
}
/**
* @return void
* @throws \ReflectionException
*/
public function testKafkaProducerBuilderConfig(): void
{
$callback = function ($kafka, $errId, $msg) {
// Anonymous test method, no logic required
};
$producer = $this->kafkaProducerBuilder
->withAdditionalBroker('localhost')
->withDeliveryReportCallback($callback)
->withErrorCallback($callback)
->withLogCallback($callback)
->build();
$reflectionProperty = new \ReflectionProperty($this->kafkaProducerBuilder, 'config');
$reflectionProperty->setAccessible(true);
self::assertSame(
[
'socket.timeout.ms' => '50',
'internal.termination.signal' => (string) SIGIO
],
$reflectionProperty->getValue($this->kafkaProducerBuilder)
);
self::assertInstanceOf(KafkaProducerInterface::class, $producer);
}
}