Skip to content

Commit 5cf044e

Browse files
committed
[amqp][symfony] Fix driver is not properly set bug.
1 parent 4f48071 commit 5cf044e

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpTools\Tests;
4+
5+
use Enqueue\AmqpTools\SignalSocketHelper;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class SignalSocketHelperTest extends TestCase
9+
{
10+
/**
11+
* @var SignalSocketHelper
12+
*/
13+
private $signalHelper;
14+
15+
private $backupSigTermHandler;
16+
17+
private $backupSigIntHandler;
18+
19+
public function setUp()
20+
{
21+
parent::setUp();
22+
23+
$this->backupSigTermHandler = pcntl_signal_get_handler(SIGTERM);
24+
$this->backupSigIntHandler = pcntl_signal_get_handler(SIGINT);
25+
26+
pcntl_signal(SIGTERM, SIG_DFL);
27+
pcntl_signal(SIGINT, SIG_DFL);
28+
29+
$this->signalHelper = new SignalSocketHelper();
30+
}
31+
32+
public function tearDown()
33+
{
34+
parent::tearDown();
35+
36+
if ($this->signalHelper) {
37+
$this->signalHelper->afterSocket();
38+
}
39+
40+
if ($this->backupSigTermHandler) {
41+
pcntl_signal(SIGTERM, $this->backupSigTermHandler);
42+
}
43+
44+
if ($this->backupSigIntHandler) {
45+
pcntl_signal(SIGINT, $this->backupSigIntHandler);
46+
}
47+
}
48+
49+
public function testShouldReturnFalseByDefault()
50+
{
51+
$this->assertFalse($this->signalHelper->wasThereSignal());
52+
}
53+
54+
public function testShouldRegisterHandlerOnBeforeSocket()
55+
{
56+
$this->signalHelper->beforeSocket();
57+
58+
$this->assertAttributeSame(false, 'wasThereSignal', $this->signalHelper);
59+
$this->assertAttributeSame([], 'handlers', $this->signalHelper);
60+
}
61+
62+
public function testShouldRegisterHandlerOnBeforeSocketAndBackupCurrentOne()
63+
{
64+
$handler = function () {};
65+
66+
pcntl_signal(SIGTERM, $handler);
67+
68+
$this->signalHelper->beforeSocket();
69+
70+
$this->assertAttributeSame(false, 'wasThereSignal', $this->signalHelper);
71+
72+
$handlers = $this->readAttribute($this->signalHelper, 'handlers');
73+
74+
$this->assertInternalType('array', $handlers);
75+
$this->assertArrayHasKey(SIGTERM, $handlers);
76+
$this->assertSame($handler, $handlers[SIGTERM]);
77+
}
78+
79+
public function testRestoreDefaultPropertiesOnAfterSocket()
80+
{
81+
$this->signalHelper->beforeSocket();
82+
$this->signalHelper->afterSocket();
83+
84+
$this->assertAttributeSame(null, 'wasThereSignal', $this->signalHelper);
85+
$this->assertAttributeSame([], 'handlers', $this->signalHelper);
86+
}
87+
88+
public function testRestorePreviousHandlerOnAfterSocket()
89+
{
90+
$handler = function () {};
91+
92+
pcntl_signal(SIGTERM, $handler);
93+
94+
$this->signalHelper->beforeSocket();
95+
$this->signalHelper->afterSocket();
96+
97+
$this->assertSame($handler, pcntl_signal_get_handler(SIGTERM));
98+
}
99+
100+
public function testThrowsIfBeforeSocketCalledSecondTime()
101+
{
102+
$this->signalHelper->beforeSocket();
103+
104+
$this->expectException(\LogicException::class);
105+
$this->expectExceptionMessage('The wasThereSignal property should be null but it is not. The afterSocket method might not have been called.');
106+
$this->signalHelper->beforeSocket();
107+
}
108+
109+
public function testShouldReturnTrueOnWasThereSignal()
110+
{
111+
$this->signalHelper->beforeSocket();
112+
113+
posix_kill(getmypid(), SIGINT);
114+
pcntl_signal_dispatch();
115+
116+
$this->assertTrue($this->signalHelper->wasThereSignal());
117+
118+
$this->signalHelper->afterSocket();
119+
}
120+
}

pkg/enqueue/Tests/Symfony/AmqpTransportFactoryTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,39 @@ public function testShouldAllowAddSslOptions()
137137
], $config);
138138
}
139139

140+
public function testThrowIfNotSupportedDriverSet()
141+
{
142+
$transport = new AmqpTransportFactory();
143+
$tb = new TreeBuilder();
144+
$rootNode = $tb->root('foo');
145+
146+
$transport->addConfiguration($rootNode);
147+
$processor = new Processor();
148+
149+
$this->expectException(InvalidConfigurationException::class);
150+
$this->expectExceptionMessage('Invalid configuration for path "foo.driver": Unexpected driver given "invalidDriver"');
151+
$processor->process($tb->buildTree(), [[
152+
'driver' => 'invalidDriver',
153+
]]);
154+
}
155+
156+
public function testShouldAllowSetDriver()
157+
{
158+
$transport = new AmqpTransportFactory();
159+
$tb = new TreeBuilder();
160+
$rootNode = $tb->root('foo');
161+
162+
$transport->addConfiguration($rootNode);
163+
$processor = new Processor();
164+
$config = $processor->process($tb->buildTree(), [[
165+
'driver' => 'ext',
166+
]]);
167+
168+
$this->assertEquals([
169+
'driver' => 'ext',
170+
], $config);
171+
}
172+
140173
public function testShouldAllowAddConfigurationAsString()
141174
{
142175
$transport = new AmqpTransportFactory();

0 commit comments

Comments
 (0)