-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_Fake.php
More file actions
126 lines (111 loc) · 2.71 KB
/
Copy pathQueue_Fake.php
File metadata and controls
126 lines (111 loc) · 2.71 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
<?php
/**
* Queue_Fake class file
*
* @package Mantle
*/
namespace Mantle\Queue;
use Mantle\Queue\Queue_Manager;
use PHPUnit\Framework\Assert as PHPUnit;
use function Mantle\Support\Helpers\collect;
/**
* Queue Fake
*
* Used as a fake provider to perform assertions against.
*/
class Queue_Fake extends Queue_Manager {
/**
* Pushed jobs.
*
* @var array<mixed>
*/
protected $jobs = [];
/**
* Assert if a job was pushed based on a truth-test callback.
*
* @param string|\Closure $job
* @param callable|int|null $callback
*/
public function assertPushed( $job, $callback = null ): void {
if ( is_numeric( $callback ) ) {
$this->assertPushedTimes( $job, $callback );
return;
}
PHPUnit::assertTrue(
$this->pushed( $job, $callback )->count() > 0,
"The expected [{$job}] job was not pushed."
);
}
/**
* Assert if a job was pushed a number of times.
*
* @param string $job
* @param int $times
* @return void
*/
protected function assertPushedTimes( $job, $times = 1 ) {
$count = $this->pushed( $job )->count();
PHPUnit::assertSame(
$times,
$count,
"The expected [{$job}] job was pushed {$count} times instead of {$times} times."
);
}
/**
* Determine if a job was pushed based on a truth-test callback.
*
* @param string|\Closure $job
* @param callable|null $callback
*/
public function assertNotPushed( $job, $callback = null ): void {
PHPUnit::assertCount(
0,
$this->pushed( $job, $callback ),
"The unexpected [{$job}] job was pushed."
);
}
/**
* Assert that no jobs were pushed.
*/
public function assertNothingPushed(): void {
PHPUnit::assertEmpty( $this->jobs, 'Jobs were pushed unexpectedly.' );
}
/**
* Get all of the jobs matching a truth-test callback.
*
* @param string $job
* @param callable|null $callback
* @return \Mantle\Support\Collection
*/
public function pushed( $job, $callback = null ) {
if ( ! $this->hasPushed( $job ) ) {
return collect();
}
$callback = $callback ?: fn () => true;
return collect( $this->jobs[ $job ] )->filter(
fn ( $data ) => $callback( $data['job'], $data['queue'] ),
)->pluck( 'job' );
}
/**
* Determine if there are any stored jobs for a given class.
*
* @param string $job
*/
public function hasPushed( $job ): bool {
return isset( $this->jobs[ $job ] ) && ! empty( $this->jobs[ $job ] );
}
/**
* Push a new job onto the queue.
*
* @param string|object $job
* @param mixed $data
* @param string $queue
*/
public function push( $job, $data = '', $queue = null ): void {
$this->jobs[ is_object( $job ) ? $job::class : $job ][] = [
'data' => $data,
'job' => $job,
'queue' => $queue,
];
}
}