Skip to content

Commit fe7f359

Browse files
committed
Implements a comprehensive TestSuite for the Queue plugin.
1 parent 0f09498 commit fe7f359

16 files changed

+2016
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cake\Queue\TestSuite\Constraint\Queue;
5+
6+
use Cake\Queue\TestSuite\TestQueueClient;
7+
8+
/**
9+
* JobCount
10+
*
11+
* Asserts that a specific count of jobs were queued
12+
*
13+
* @internal
14+
*/
15+
class JobCount extends QueueConstraintBase
16+
{
17+
/**
18+
* Checks if job count matches
19+
*
20+
* @param mixed $other Expected count
21+
* @return bool
22+
*/
23+
public function matches(mixed $other): bool
24+
{
25+
$expectedCount = $other;
26+
$actualCount = TestQueueClient::getQueuedJobCount();
27+
28+
return $actualCount === $expectedCount;
29+
}
30+
31+
/**
32+
* Assertion message
33+
*
34+
* @return string
35+
*/
36+
public function toString(): string
37+
{
38+
return 'job count matches';
39+
}
40+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cake\Queue\TestSuite\Constraint\Queue;
5+
6+
/**
7+
* JobQueued
8+
*
9+
* Asserts that a job of a specific class was queued
10+
*
11+
* @internal
12+
*/
13+
class JobQueued extends QueueConstraintBase
14+
{
15+
/**
16+
* Checks if job was queued
17+
*
18+
* @param mixed $other Job class name
19+
* @return bool
20+
*/
21+
public function matches(mixed $other): bool
22+
{
23+
$jobClass = $other;
24+
$jobs = $this->getJobs();
25+
26+
foreach ($jobs as $job) {
27+
if ($job['jobClass'] === $jobClass) {
28+
return true;
29+
}
30+
}
31+
32+
return false;
33+
}
34+
35+
/**
36+
* Assertion message
37+
*
38+
* @return string
39+
*/
40+
public function toString(): string
41+
{
42+
if ($this->at !== null) {
43+
return sprintf('job #%d was queued', $this->at);
44+
}
45+
46+
return 'job was queued';
47+
}
48+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cake\Queue\TestSuite\Constraint\Queue;
5+
6+
use Cake\Queue\TestSuite\TestQueueClient;
7+
8+
/**
9+
* JobQueuedTimes
10+
*
11+
* Asserts that a job was queued a specific number of times
12+
*
13+
* @internal
14+
*/
15+
class JobQueuedTimes extends QueueConstraintBase
16+
{
17+
/**
18+
* Expected number of times
19+
*
20+
* @var int
21+
*/
22+
protected int $times;
23+
24+
/**
25+
* Constructor
26+
*
27+
* @param int $times Expected number of times
28+
*/
29+
public function __construct(int $times)
30+
{
31+
$this->times = $times;
32+
}
33+
34+
/**
35+
* Checks if job was queued the expected number of times
36+
*
37+
* @param mixed $other Job class name
38+
* @return bool
39+
*/
40+
public function matches(mixed $other): bool
41+
{
42+
$jobClass = $other;
43+
$jobs = TestQueueClient::getQueuedJobsByClass($jobClass);
44+
$actualCount = count($jobs);
45+
46+
return $actualCount === $this->times;
47+
}
48+
49+
/**
50+
* Assertion message
51+
*
52+
* @return string
53+
*/
54+
public function toString(): string
55+
{
56+
return sprintf('job was queued %d times', $this->times);
57+
}
58+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cake\Queue\TestSuite\Constraint\Queue;
5+
6+
use Cake\Queue\TestSuite\TestQueueClient;
7+
8+
/**
9+
* NoJobQueued
10+
*
11+
* Asserts that no jobs were queued
12+
*
13+
* @internal
14+
*/
15+
class NoJobQueued extends QueueConstraintBase
16+
{
17+
/**
18+
* Checks if no jobs were queued
19+
*
20+
* @param mixed $other Ignored
21+
* @return bool
22+
*/
23+
public function matches(mixed $other): bool
24+
{
25+
return TestQueueClient::getQueuedJobCount() === 0;
26+
}
27+
28+
/**
29+
* Assertion message
30+
*
31+
* @return string
32+
*/
33+
public function toString(): string
34+
{
35+
return 'no jobs were queued';
36+
}
37+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cake\Queue\TestSuite\Constraint\Queue;
5+
6+
use Cake\Queue\TestSuite\TestQueueClient;
7+
use PHPUnit\Framework\Constraint\Constraint;
8+
9+
/**
10+
* Base class for all queue assertion constraints
11+
*
12+
* @internal
13+
*/
14+
abstract class QueueConstraintBase extends Constraint
15+
{
16+
/**
17+
* Job index to check
18+
*
19+
* @var int|null
20+
*/
21+
protected ?int $at = null;
22+
23+
/**
24+
* Constructor
25+
*
26+
* @param int|null $at Optional index of specific job to check
27+
*/
28+
public function __construct(?int $at = null)
29+
{
30+
$this->at = $at;
31+
}
32+
33+
/**
34+
* Get the jobs or job to check
35+
*
36+
* @return array<array<string, mixed>>
37+
*/
38+
protected function getJobs(): array
39+
{
40+
$jobs = TestQueueClient::getQueuedJobs();
41+
42+
if ($this->at !== null) {
43+
if (!isset($jobs[$this->at])) {
44+
return [];
45+
}
46+
47+
return [$jobs[$this->at]];
48+
}
49+
50+
return $jobs;
51+
}
52+
}

0 commit comments

Comments
 (0)