forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueSyncQueueTest.php
More file actions
executable file
·136 lines (112 loc) · 3.39 KB
/
Copy pathQueueSyncQueueTest.php
File metadata and controls
executable file
·136 lines (112 loc) · 3.39 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
<?php
namespace Illuminate\Tests\Queue;
use Exception;
use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Jobs\SyncJob;
use Illuminate\Queue\SyncQueue;
use LogicException;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class QueueSyncQueueTest extends TestCase
{
protected function tearDown(): void
{
m::close();
Container::setInstance(null);
}
public function testPushShouldFireJobInstantly()
{
unset($_SERVER['__sync.test']);
$sync = new SyncQueue;
$container = new Container;
$sync->setContainer($container);
$sync->push(SyncQueueTestHandler::class, ['foo' => 'bar']);
$this->assertInstanceOf(SyncJob::class, $_SERVER['__sync.test'][0]);
$this->assertEquals(['foo' => 'bar'], $_SERVER['__sync.test'][1]);
}
public function testFailedJobGetsHandledWhenAnExceptionIsThrown()
{
unset($_SERVER['__sync.failed']);
$sync = new SyncQueue;
$container = new Container;
Container::setInstance($container);
$events = m::mock(Dispatcher::class);
$events->shouldReceive('dispatch')->times(3);
$container->instance('events', $events);
$container->instance(Dispatcher::class, $events);
$sync->setContainer($container);
try {
$sync->push(FailingSyncQueueTestHandler::class, ['foo' => 'bar']);
} catch (Exception $e) {
$this->assertTrue($_SERVER['__sync.failed']);
}
Container::setInstance();
}
public function testCreatesPayloadObject()
{
$sync = new SyncQueue;
$container = new Container;
$container->bind(\Illuminate\Contracts\Events\Dispatcher::class, \Illuminate\Events\Dispatcher::class);
$container->bind(\Illuminate\Contracts\Bus\Dispatcher::class, \Illuminate\Bus\Dispatcher::class);
$container->bind(\Illuminate\Contracts\Container\Container::class, \Illuminate\Container\Container::class);
$sync->setContainer($container);
SyncQueue::createPayloadUsing(function ($connection, $queue, $payload) {
return ['data' => ['extra' => 'extraValue']];
});
try {
$sync->push(new SyncQueueJob());
} catch (LogicException $e) {
$this->assertEquals('extraValue', $e->getMessage());
}
}
}
class SyncQueueTestEntity implements QueueableEntity
{
public function getQueueableId()
{
return 1;
}
public function getQueueableConnection()
{
//
}
public function getQueueableRelations()
{
//
}
}
class SyncQueueTestHandler
{
public function fire($job, $data)
{
$_SERVER['__sync.test'] = func_get_args();
}
}
class FailingSyncQueueTestHandler
{
public function fire($job, $data)
{
throw new Exception;
}
public function failed()
{
$_SERVER['__sync.failed'] = true;
}
}
class SyncQueueJob implements ShouldQueue
{
use InteractsWithQueue;
public function handle()
{
throw new LogicException($this->getValueFromJob('extra'));
}
public function getValueFromJob($key)
{
$payload = $this->job->payload();
return $payload['data'][$key] ?? null;
}
}