Repository navigation
Expand file tree
/
Copy pathJobMessageTest.php
More file actions
117 lines (99 loc) · 4.04 KB
/
Copy pathJobMessageTest.php
File metadata and controls
117 lines (99 loc) · 4.04 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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\AsyncQueue;
use Hyperf\AsyncQueue\JobInterface;
use Hyperf\AsyncQueue\JobMessage;
use Hyperf\AsyncQueue\MessageInterface;
use HyperfTest\AsyncQueue\Stub\DemoJob;
use HyperfTest\AsyncQueue\Stub\OldJobMessage;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class JobMessageTest extends TestCase
{
public function testJobMessageSerialize()
{
$id = rand(0, 9999);
$message = new JobMessage(
new DemoJob($id)
);
$serialized = $message->__serialize();
$this->assertEquals($serialized[0], $serialized['job']);
$this->assertEquals($serialized[1], $serialized['attempts']);
$this->assertArrayHasKey('job', $serialized);
$this->assertArrayHasKey('attempts', $serialized);
$this->assertInstanceOf(MessageInterface::class, $message);
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(DemoJob::class, $message->job());
$this->assertSame($id, $message->job()->id);
}
public function testJobMessageSerializeCompatible()
{
$id = rand(0, 9999);
$message = new JobMessage(
new DemoJob($id)
);
$serialized = $message->__serialize();
$this->assertEquals($serialized[0], $serialized['job']);
$this->assertEquals($serialized[1], $serialized['attempts']);
$message = unserialize(serialize($message));
$this->assertInstanceOf(MessageInterface::class, $message);
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(DemoJob::class, $message->job());
$this->assertSame($id, $message->job()->id);
$this->assertSame(0, $message->getAttempts());
$serialized = [
'job' => $serialized['job'] ?? $serialized[0],
'attempts' => 3,
];
$message->__unserialize($serialized);
$this->assertInstanceOf(MessageInterface::class, $message);
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(DemoJob::class, $message->job());
$this->assertSame($id, $message->job()->id);
$this->assertSame(3, $message->getAttempts());
$serialized = [new DemoJob($id), 5];
$message->__unserialize($serialized);
$this->assertInstanceOf(MessageInterface::class, $message);
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(DemoJob::class, $message->job());
$this->assertSame($id, $message->job()->id);
$this->assertSame(5, $message->getAttempts());
}
public function testUnserializeAsOldJobMessage()
{
$id = rand(0, 9999);
$message = new JobMessage(
new DemoJob($id)
);
$serialized = serialize($message);
$serialized = str_replace(
sprintf('O:%d:"%s', strlen(JobMessage::class), JobMessage::class),
sprintf('O:%d:"%s', strlen(OldJobMessage::class), OldJobMessage::class),
$serialized
);
$message = unserialize($serialized);
$this->assertInstanceOf(MessageInterface::class, $message);
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(DemoJob::class, $message->job());
$this->assertSame($id, $message->job()->id);
$this->assertSame(0, $message->getAttempts());
}
}