-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathPayloadCollectionTest.php
More file actions
185 lines (144 loc) · 4.95 KB
/
PayloadCollectionTest.php
File metadata and controls
185 lines (144 loc) · 4.95 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Queue.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Tests\Payloads;
use ArrayIterator;
use CodeIgniter\Queue\Payloads\Payload;
use CodeIgniter\Queue\Payloads\PayloadCollection;
use Tests\Support\TestCase;
/**
* @internal
*/
final class PayloadCollectionTest extends TestCase
{
private PayloadCollection $collection;
private Payload $payload1;
private Payload $payload2;
protected function setUp(): void
{
parent::setUp();
// Create sample payloads
$this->payload1 = new Payload('job1', ['key1' => 'value1']);
$this->payload1->setQueue('queue1');
$this->payload2 = new Payload('job2', ['key2' => 'value2']);
$this->payload2->setQueue('queue2');
// Create an empty collection
$this->collection = new PayloadCollection();
}
public function testEmptyCollectionCount(): void
{
$this->assertCount(0, $this->collection);
}
public function testAddPayload(): void
{
$result = $this->collection->add($this->payload1);
$this->assertInstanceOf(PayloadCollection::class, $result);
$this->assertCount(1, $this->collection);
}
public function testAddMultiplePayloads(): void
{
$this->collection->add($this->payload1);
$this->collection->add($this->payload2);
$this->assertCount(2, $this->collection);
}
public function testShiftPayload(): void
{
$this->collection->add($this->payload1);
$this->collection->add($this->payload2);
$first = $this->collection->shift();
$this->assertSame($this->payload1, $first);
$this->assertCount(1, $this->collection);
}
public function testShiftFromEmptyCollection(): void
{
$result = $this->collection->shift();
$this->assertNull($result);
}
public function testGetIterator(): void
{
$this->collection->add($this->payload1);
$this->collection->add($this->payload2);
$iterator = $this->collection->getIterator();
$this->assertInstanceOf(ArrayIterator::class, $iterator);
$this->assertCount(2, $iterator);
}
public function testToArray(): void
{
$this->collection->add($this->payload1);
$this->collection->add($this->payload2);
$array = $this->collection->toArray();
$this->assertCount(2, $array);
// Check array structure
$this->assertArrayHasKey('job', $array[0]);
$this->assertArrayHasKey('data', $array[0]);
$this->assertArrayHasKey('metadata', $array[0]);
$this->assertSame('job1', $array[0]['job']);
$this->assertSame(['key1' => 'value1'], $array[0]['data']);
}
public function testJsonSerialize(): void
{
$this->collection->add($this->payload1);
$this->collection->add($this->payload2);
$json = json_encode($this->collection);
$decoded = json_decode($json, true);
$this->assertIsArray($decoded);
$this->assertCount(2, $decoded);
$this->assertSame('job1', $decoded[0]['job']);
$this->assertSame('job2', $decoded[1]['job']);
}
public function testFromArray(): void
{
$arrayData = [
[
'job' => 'job1',
'data' => ['key1' => 'value1'],
'metadata' => ['queue' => 'queue1'],
],
[
'job' => 'job2',
'data' => ['key2' => 'value2'],
'metadata' => ['queue' => 'queue2'],
],
];
$collection = PayloadCollection::fromArray($arrayData);
$this->assertInstanceOf(PayloadCollection::class, $collection);
$this->assertCount(2, $collection);
$first = $collection->shift();
$this->assertInstanceOf(Payload::class, $first);
$this->assertSame('job1', $first->getJob());
$this->assertSame(['key1' => 'value1'], $first->getData());
}
public function testInvalidDataInFromArray(): void
{
$arrayData = [
['invalid' => 'data'], // Missing job and data
[
'job' => 'job2',
'data' => ['key2' => 'value2'],
],
];
$collection = PayloadCollection::fromArray($arrayData);
// Should only have created one valid payload
$this->assertCount(1, $collection);
}
public function testIteration(): void
{
$this->collection->add($this->payload1);
$this->collection->add($this->payload2);
$count = 0;
$jobs = [];
foreach ($this->collection as $payload) {
$count++;
$jobs[] = $payload->getJob();
}
$this->assertSame(2, $count);
$this->assertSame(['job1', 'job2'], $jobs);
}
}