Skip to content

Commit 86d2a49

Browse files
Add tests to ensure the connector is reading the config and creating the queue with the proper arguments.
1 parent cd40163 commit 86d2a49

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed

src/Support/Arr.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@
66

77
class Arr
88
{
9+
/**
10+
* Get all of the given array except for a specified array of keys.
11+
*
12+
* @param array $array
13+
* @param array|string $keys
14+
*
15+
* @return array
16+
*/
17+
public static function except($array, $keys)
18+
{
19+
return class_exists(BaseArr::class) ? BaseArr::except($array, $keys) : array_except($array, $keys);
20+
}
21+
922
/**
1023
* Get a subset of the items from the given array.
1124
*

tests/ArrTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
class ArrTest extends TestCase
88
{
9+
public function test_array_except()
10+
{
11+
$array = ['name' => 'taylor', 'age' => 26];
12+
$this->assertEquals(['age' => 26], Arr::except($array, ['name']));
13+
$this->assertEquals(['age' => 26], Arr::except($array, 'name'));
14+
}
15+
916
public function test_array_only()
1017
{
1118
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];

tests/ConnectorTest.php

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ShiftOneLabs\LaravelSqsFifoQueue\Tests;
44

55
use InvalidArgumentException;
6+
use ShiftOneLabs\LaravelSqsFifoQueue\Support\Arr;
67
use ShiftOneLabs\LaravelSqsFifoQueue\SqsFifoQueue;
78
use ShiftOneLabs\LaravelSqsFifoQueue\Queue\Connectors\SqsFifoConnector;
89

@@ -27,4 +28,211 @@ public function test_sqs_fifo_driver_throws_exception_with_invalid_queue_name()
2728

2829
$connector->connect($config);
2930
}
31+
32+
public function test_sqs_fifo_driver_creates_queue_with_missing_prefix()
33+
{
34+
$config = $this->getConfig([], ['prefix']);
35+
$connector = new SqsFifoConnector();
36+
37+
$queue = $connector->connect($config);
38+
39+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
40+
41+
if (property_exists($queue, 'prefix')) {
42+
$this->assertEquals('', $this->getRestrictedValue($queue, 'prefix'));
43+
}
44+
}
45+
46+
public function test_sqs_fifo_driver_creates_queue_with_empty_prefix()
47+
{
48+
$config = $this->getConfig(['prefix' => '']);
49+
$connector = new SqsFifoConnector();
50+
51+
$queue = $connector->connect($config);
52+
53+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
54+
55+
if (property_exists($queue, 'prefix')) {
56+
$this->assertEquals('', $this->getRestrictedValue($queue, 'prefix'));
57+
}
58+
}
59+
60+
public function test_sqs_fifo_driver_creates_queue_with_valid_prefix()
61+
{
62+
$config = $this->getConfig();
63+
$connector = new SqsFifoConnector();
64+
65+
$queue = $connector->connect($config);
66+
67+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
68+
69+
if (property_exists($queue, 'prefix')) {
70+
$this->assertNotEmpty($config['prefix']);
71+
$this->assertEquals($config['prefix'], $this->getRestrictedValue($queue, 'prefix'));
72+
}
73+
}
74+
75+
public function test_sqs_fifo_driver_creates_queue_with_missing_suffix()
76+
{
77+
$config = $this->getConfig([], ['suffix']);
78+
$connector = new SqsFifoConnector();
79+
80+
$queue = $connector->connect($config);
81+
82+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
83+
$this->assertEquals('', $this->getRestrictedValue($queue, 'suffix'));
84+
}
85+
86+
public function test_sqs_fifo_driver_creates_queue_with_empty_suffix()
87+
{
88+
$config = $this->getConfig(['suffix' => '']);
89+
$connector = new SqsFifoConnector();
90+
91+
$queue = $connector->connect($config);
92+
93+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
94+
$this->assertEquals('', $this->getRestrictedValue($queue, 'suffix'));
95+
}
96+
97+
public function test_sqs_fifo_driver_creates_queue_with_valid_suffix()
98+
{
99+
$config = $this->getConfig();
100+
$connector = new SqsFifoConnector();
101+
102+
$queue = $connector->connect($config);
103+
104+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
105+
$this->assertNotEmpty($config['suffix']);
106+
$this->assertEquals($config['suffix'], $this->getRestrictedValue($queue, 'suffix'));
107+
}
108+
109+
public function test_sqs_fifo_driver_creates_queue_with_default_group_when_missing_group()
110+
{
111+
$config = $this->getConfig([], ['group']);
112+
$connector = new SqsFifoConnector();
113+
114+
$queue = $connector->connect($config);
115+
116+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
117+
$this->assertEquals('default', $this->getRestrictedValue($queue, 'group'));
118+
}
119+
120+
public function test_sqs_fifo_driver_creates_queue_with_empty_group()
121+
{
122+
$config = $this->getConfig(['group' => '']);
123+
$connector = new SqsFifoConnector();
124+
125+
$queue = $connector->connect($config);
126+
127+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
128+
$this->assertEquals('', $this->getRestrictedValue($queue, 'group'));
129+
}
130+
131+
public function test_sqs_fifo_driver_creates_queue_with_valid_group()
132+
{
133+
$config = $this->getConfig();
134+
$connector = new SqsFifoConnector();
135+
136+
$queue = $connector->connect($config);
137+
138+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
139+
$this->assertNotEmpty($config['group']);
140+
$this->assertEquals($config['group'], $this->getRestrictedValue($queue, 'group'));
141+
}
142+
143+
public function test_sqs_fifo_driver_creates_queue_with_default_deduplicator_when_missing_deduplicator()
144+
{
145+
$config = $this->getConfig([], ['deduplicator']);
146+
$connector = new SqsFifoConnector();
147+
148+
$queue = $connector->connect($config);
149+
150+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
151+
$this->assertEquals('unique', $this->getRestrictedValue($queue, 'deduplicator'));
152+
}
153+
154+
public function test_sqs_fifo_driver_creates_queue_with_empty_deduplicator()
155+
{
156+
$config = $this->getConfig(['deduplicator' => '']);
157+
$connector = new SqsFifoConnector();
158+
159+
$queue = $connector->connect($config);
160+
161+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
162+
$this->assertEquals('', $this->getRestrictedValue($queue, 'deduplicator'));
163+
}
164+
165+
public function test_sqs_fifo_driver_creates_queue_with_valid_deduplicator()
166+
{
167+
$config = $this->getConfig();
168+
$connector = new SqsFifoConnector();
169+
170+
$queue = $connector->connect($config);
171+
172+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
173+
$this->assertNotEmpty($config['deduplicator']);
174+
$this->assertEquals($config['deduplicator'], $this->getRestrictedValue($queue, 'deduplicator'));
175+
}
176+
177+
public function test_sqs_fifo_driver_creates_queue_with_default_allow_delay_when_missing_allow_delay()
178+
{
179+
$config = $this->getConfig([], ['allow_delay']);
180+
$connector = new SqsFifoConnector();
181+
182+
$queue = $connector->connect($config);
183+
184+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
185+
$this->assertEquals(false, $this->getRestrictedValue($queue, 'allowDelay'));
186+
}
187+
188+
public function test_sqs_fifo_driver_creates_queue_with_empty_allow_delay()
189+
{
190+
$config = $this->getConfig(['allow_delay' => '']);
191+
$connector = new SqsFifoConnector();
192+
193+
$queue = $connector->connect($config);
194+
195+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
196+
$this->assertEquals(false, $this->getRestrictedValue($queue, 'allowDelay'));
197+
}
198+
199+
public function test_sqs_fifo_driver_creates_queue_with_valid_allow_delay_as_false()
200+
{
201+
$config = $this->getConfig(['allow_delay' => false]);
202+
$connector = new SqsFifoConnector();
203+
204+
$queue = $connector->connect($config);
205+
206+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
207+
$this->assertFalse($config['allow_delay']);
208+
$this->assertEquals($config['allow_delay'], $this->getRestrictedValue($queue, 'allowDelay'));
209+
}
210+
211+
public function test_sqs_fifo_driver_creates_queue_with_valid_allow_delay_as_true()
212+
{
213+
$config = $this->getConfig(['allow_delay' => true]);
214+
$connector = new SqsFifoConnector();
215+
216+
$queue = $connector->connect($config);
217+
218+
$this->assertInstanceOf(SqsFifoQueue::class, $queue);
219+
$this->assertTrue($config['allow_delay']);
220+
$this->assertEquals($config['allow_delay'], $this->getRestrictedValue($queue, 'allowDelay'));
221+
}
222+
223+
protected function getConfig($overrides = [], $except = [])
224+
{
225+
return Arr::except(array_merge([
226+
'driver' => 'sqs-fifo',
227+
'key' => 'ABCDEFGHIJKLMNOPQRST',
228+
'secret' => '1a23bc/deFgHijKl4mNOp5qrS6TUVwXyz7ABCDef',
229+
'prefix' => 'https://sqs.us-east-1.amazonaws.com/123456789012',
230+
'suffix' => '-staging',
231+
'queue' => 'queuename.fifo',
232+
'region' => 'us-east-1',
233+
'group' => 'default',
234+
'deduplicator' => 'unique',
235+
'allow_delay' => false,
236+
], $overrides), $except);
237+
}
30238
}

tests/TestCase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public function setUpQueueConnection()
136136
'region' => getenv('SQS_REGION') ?: '',
137137
'group' => 'default',
138138
'deduplicator' => 'unique',
139+
'allow_delay' => false,
139140
], 'sqs-fifo');
140141

141142
$queue->addConnection([
@@ -145,6 +146,7 @@ public function setUpQueueConnection()
145146
'region' => getenv('SQS_REGION') ?: '',
146147
'group' => 'default',
147148
'deduplicator' => 'unique',
149+
'allow_delay' => false,
148150
], 'sqs-fifo-no-credentials');
149151
}
150152

@@ -165,6 +167,23 @@ protected function callRestrictedMethod($object, $method, array $args = [])
165167
return $reflectionMethod->invokeArgs($object, $args);
166168
}
167169

170+
/**
171+
* Use reflection to get the value of a restricted (private/protected)
172+
* property on an object.
173+
*
174+
* @param object $object
175+
* @param string $property
176+
*
177+
* @return mixed
178+
*/
179+
protected function getRestrictedValue($object, $property)
180+
{
181+
$reflectionProperty = new ReflectionProperty($object, $property);
182+
$reflectionProperty->setAccessible(true);
183+
184+
return $reflectionProperty->getValue($object);
185+
}
186+
168187
/**
169188
* Use reflection to set the value of a restricted (private/protected)
170189
* property on an object.

0 commit comments

Comments
 (0)