Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/TestSuite/Constraint/Queue/JobCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);

namespace Cake\Queue\TestSuite\Constraint\Queue;

use Cake\Queue\TestSuite\TestQueueClient;

/**
* JobCount
*
* Asserts that a specific count of jobs were queued
*
* @internal
*/
class JobCount extends QueueConstraintBase
{
/**
* Checks if job count matches
*
* @param mixed $other Expected count
* @return bool
*/
public function matches(mixed $other): bool
{
$expectedCount = $other;
$actualCount = TestQueueClient::getQueuedJobCount();

return $actualCount === $expectedCount;
}

/**
* Assertion message
*
* @return string
*/
public function toString(): string
{
return 'job count matches';
}
}
48 changes: 48 additions & 0 deletions src/TestSuite/Constraint/Queue/JobQueued.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace Cake\Queue\TestSuite\Constraint\Queue;

/**
* JobQueued
*
* Asserts that a job of a specific class was queued
*
* @internal
*/
class JobQueued extends QueueConstraintBase
{
/**
* Checks if job was queued
*
* @param mixed $other Job class name
* @return bool
*/
public function matches(mixed $other): bool
{
$jobClass = $other;
$jobs = $this->getJobs();

foreach ($jobs as $job) {
if ($job['jobClass'] === $jobClass) {
return true;
}
}

return false;
}

/**
* Assertion message
*
* @return string
*/
public function toString(): string
{
if ($this->at !== null) {
return sprintf('job #%d was queued', $this->at);
}

return 'job was queued';
}
}
58 changes: 58 additions & 0 deletions src/TestSuite/Constraint/Queue/JobQueuedTimes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);

namespace Cake\Queue\TestSuite\Constraint\Queue;

use Cake\Queue\TestSuite\TestQueueClient;

/**
* JobQueuedTimes
*
* Asserts that a job was queued a specific number of times
*
* @internal
*/
class JobQueuedTimes extends QueueConstraintBase
{
/**
* Expected number of times
*
* @var int
*/
protected int $times;

/**
* Constructor
*
* @param int $times Expected number of times
*/
public function __construct(int $times)
{
$this->times = $times;
}

/**
* Checks if job was queued the expected number of times
*
* @param mixed $other Job class name
* @return bool
*/
public function matches(mixed $other): bool
{
$jobClass = $other;
$jobs = TestQueueClient::getQueuedJobsByClass($jobClass);
$actualCount = count($jobs);

return $actualCount === $this->times;
}

/**
* Assertion message
*
* @return string
*/
public function toString(): string
{
return sprintf('job was queued %d times', $this->times);
}
}
37 changes: 37 additions & 0 deletions src/TestSuite/Constraint/Queue/NoJobQueued.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

namespace Cake\Queue\TestSuite\Constraint\Queue;

use Cake\Queue\TestSuite\TestQueueClient;

/**
* NoJobQueued
*
* Asserts that no jobs were queued
*
* @internal
*/
class NoJobQueued extends QueueConstraintBase
{
/**
* Checks if no jobs were queued
*
* @param mixed $other Ignored
* @return bool
*/
public function matches(mixed $other): bool
{
return TestQueueClient::getQueuedJobCount() === 0;
}

/**
* Assertion message
*
* @return string
*/
public function toString(): string
{
return 'no jobs were queued';
}
}
52 changes: 52 additions & 0 deletions src/TestSuite/Constraint/Queue/QueueConstraintBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);

namespace Cake\Queue\TestSuite\Constraint\Queue;

use Cake\Queue\TestSuite\TestQueueClient;
use PHPUnit\Framework\Constraint\Constraint;

/**
* Base class for all queue assertion constraints
*
* @internal
*/
abstract class QueueConstraintBase extends Constraint
{
/**
* Job index to check
*
* @var int|null
*/
protected ?int $at = null;

/**
* Constructor
*
* @param int|null $at Optional index of specific job to check
*/
public function __construct(?int $at = null)
{
$this->at = $at;
}

/**
* Get the jobs or job to check
*
* @return array<array<string, mixed>>
*/
protected function getJobs(): array
{
$jobs = TestQueueClient::getQueuedJobs();

if ($this->at !== null) {
if (!isset($jobs[$this->at])) {
return [];
}

return [$jobs[$this->at]];
}

return $jobs;
}
}
Loading