1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Tests\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\DependencyInjection\DtcQueueExtension; |
6
|
|
|
use Dtc\QueueBundle\Model\PriorityJobManager; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
|
10
|
|
|
class DtcQueueExtensionTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function testDtcQueueExtension() |
13
|
|
|
{ |
14
|
|
|
$dtcQueueExtension = new DtcQueueExtension(); |
15
|
|
|
$configs = []; |
16
|
|
|
$containerBuilder = new ContainerBuilder(); |
17
|
|
|
$dtcQueueExtension->load($configs, $containerBuilder); |
18
|
|
|
|
19
|
|
|
self::assertEquals('dtc_queue', $dtcQueueExtension->getAlias()); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function tryBadConfigs(array $configs) |
23
|
|
|
{ |
24
|
|
|
return $this->tryConfigs($configs, false); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testBeanstalkExtension() |
28
|
|
|
{ |
29
|
|
|
$configs = ['config' => ['beanstalkd' => ['host' => null]]]; |
30
|
|
|
$this->tryBadConfigs($configs); |
31
|
|
|
$configs = ['config' => ['beanstalkd' => ['tube' => null]]]; |
32
|
|
|
$this->tryBadConfigs($configs); |
33
|
|
|
|
34
|
|
|
$configs = ['config' => ['beanstalkd' => ['host' => 'somehost']]]; |
35
|
|
|
$containerBuilder = $this->tryConfigs($configs); |
36
|
|
|
self::assertEquals('somehost', $containerBuilder->getParameter('dtc_queue.beanstalkd.host')); |
37
|
|
|
|
38
|
|
|
$configs = ['config' => ['beanstalkd' => ['host' => 'somehost', 'tube' => 'something']]]; |
39
|
|
|
$containerBuilder = $this->tryConfigs($configs); |
40
|
|
|
|
41
|
|
|
self::assertEquals('something', $containerBuilder->getParameter('dtc_queue.beanstalkd.tube')); |
42
|
|
|
self::assertEquals('somehost', $containerBuilder->getParameter('dtc_queue.beanstalkd.host')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testPriority() { |
46
|
|
|
$configs = []; |
47
|
|
|
$containerBuilder = $this->tryConfigs($configs); |
48
|
|
|
self::assertEquals(255, $containerBuilder->getParameter('dtc_queue.priority_max')); |
49
|
|
|
self::assertEquals(PriorityJobManager::PRIORITY_DESC, $containerBuilder->getParameter('dtc_queue.priority_direction')); |
50
|
|
|
$configs = ['config' => ['priority_max' => 200]]; |
51
|
|
|
$containerBuilder = $this->tryConfigs($configs); |
52
|
|
|
self::assertEquals(200, $containerBuilder->getParameter('dtc_queue.priority_max')); |
53
|
|
|
$configs = ['config' => ['priority_max' => null]]; |
54
|
|
|
$this->tryBadConfigs($configs); |
55
|
|
|
$configs = ['config' => ['priority_max' => 0]]; |
56
|
|
|
$this->tryBadConfigs($configs); |
57
|
|
|
$configs = ['config' => ['priority_direction' => 'asdf']]; |
58
|
|
|
$this->tryBadConfigs($configs); |
59
|
|
|
|
60
|
|
|
$configs = ['config' => ['priority_direction' => PriorityJobManager::PRIORITY_ASC]]; |
61
|
|
|
$containerBuilder = $this->tryConfigs($configs); |
62
|
|
|
self::assertEquals(PriorityJobManager::PRIORITY_ASC, $containerBuilder->getParameter('dtc_queue.priority_direction')); |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function tryConfigs(array $configs, $good = true) |
67
|
|
|
{ |
68
|
|
|
$dtcQueueExtension = new DtcQueueExtension(); |
69
|
|
|
$containerBuilder = new ContainerBuilder(); |
70
|
|
|
|
71
|
|
|
$failed = false; |
72
|
|
|
try { |
73
|
|
|
$dtcQueueExtension->load($configs, $containerBuilder); |
74
|
|
|
if (!$good) { |
75
|
|
|
$failed = true; |
76
|
|
|
} |
77
|
|
|
} catch (\Exception $exception) { |
78
|
|
|
if ($good) { |
79
|
|
|
self::fail($exception->getMessage()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
self::assertFalse($failed); |
83
|
|
|
|
84
|
|
|
return $containerBuilder; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testRabbitMq() |
88
|
|
|
{ |
89
|
|
|
$configs = ['config' => ['rabbit_mq' => []]]; |
90
|
|
|
$this->tryBadConfigs($configs); |
91
|
|
|
|
92
|
|
|
$configs = ['config' => ['rabbit_mq' => ['host' => 'somehost', 'port' => 1234, 'user' => 'auser', 'password' => 'pass']]]; |
93
|
|
|
$containerBuilder = $this->tryConfigs($configs); |
94
|
|
|
$this->arrayTest($containerBuilder, 'dtc_queue.rabbit_mq', 'host', 'somehost'); |
95
|
|
|
$this->arrayTest($containerBuilder, 'dtc_queue.rabbit_mq', 'port', 1234); |
96
|
|
|
$this->arrayTest($containerBuilder, 'dtc_queue.rabbit_mq', 'user', 'auser'); |
97
|
|
|
$this->arrayTest($containerBuilder, 'dtc_queue.rabbit_mq', 'password', 'pass'); |
98
|
|
|
|
99
|
|
|
$configs = ['config' => ['rabbit_mq' => ['host' => 'somehost', 'port' => 1234, 'user' => 'auser', 'password' => 'pass', 'options' => ['insist' => true]]]]; |
100
|
|
|
$containerBuilder = $this->tryConfigs($configs); |
101
|
|
|
$this->arrayTest($containerBuilder, 'dtc_queue.rabbit_mq', 'options', ['insist' => true]); |
102
|
|
|
|
103
|
|
|
$configs = ['config' => ['rabbit_mq' => ['host' => 'somehost', 'port' => 1234, 'user' => 'auser', 'ssl_options' => ['a' => true], 'password' => 'pass']]]; |
104
|
|
|
$this->tryBadConfigs($configs); |
105
|
|
|
|
106
|
|
|
$configs = ['config' => ['rabbit_mq' => ['host' => 'somehost', 'port' => 1234, 'user' => 'auser', 'ssl' => true, 'ssl_options' => ['a' => true], 'password' => 'pass']]]; |
107
|
|
|
$containerBuilder = $this->tryConfigs($configs); |
108
|
|
|
$this->arrayTest($containerBuilder, 'dtc_queue.rabbit_mq', 'ssl_options', ['a' => true]); |
109
|
|
|
$rabbitMq = $containerBuilder->getParameter('dtc_queue.rabbit_mq'); |
110
|
|
|
self::assertFalse(isset($rabbitMq['options'])); |
111
|
|
|
|
112
|
|
|
$configs = ['config' => ['rabbit_mq' => ['host' => 'somehost', 'port' => 1234, 'user' => 'auser', 'ssl' => true, 'ssl_options' => ['a' => ['something']], 'password' => 'pass']]]; |
113
|
|
|
$this->tryBadConfigs($configs); |
114
|
|
|
|
115
|
|
|
$configs = ['config' => ['rabbit_mq' => ['host' => 'somehost', 'port' => 1234, 'user' => 'auser', 'ssl' => true, 'ssl_options' => ['peer_fingerprint' => ['something' => 'else']], 'password' => 'pass']]]; |
116
|
|
|
$containerBuilder = $this->tryConfigs($configs); |
117
|
|
|
$this->arrayTest($containerBuilder, 'dtc_queue.rabbit_mq', 'ssl_options', ['peer_fingerprint' => ['something' => 'else']]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param ContainerBuilder $containerBuilder |
122
|
|
|
* @param string $parameter |
123
|
|
|
* @param string $key |
124
|
|
|
* @param mixed $result |
125
|
|
|
*/ |
126
|
|
|
protected function arrayTest(ContainerBuilder $containerBuilder, $parameter, $key, $result) |
127
|
|
|
{ |
128
|
|
|
$arr = $containerBuilder->getParameter($parameter); |
129
|
|
|
self::assertArrayHasKey($key, $arr); |
130
|
|
|
self::assertEquals($result, $arr[$key]); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|