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
|
<?php
declare(strict_types=1);
namespace Pheanstalk\Tests\Unit\Command;
use Pheanstalk\Contract\CommandInterface;
use Pheanstalk\Exception\UnsupportedResponseException;
use Pheanstalk\Values\RawResponse;
use Pheanstalk\Values\ResponseType;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
abstract class CommandTestBase extends TestCase
{
/**
* @return list<ResponseType>
*/
abstract protected static function getSupportedResponses(): array;
abstract protected function getSubject(): CommandInterface;
/**
* @phpstan-return iterable<array{0: ResponseType}>
*/
final public static function supportedResponseProvider(): iterable
{
foreach (static::getSupportedResponses() as $responseType) {
yield [$responseType];
}
}
/**
* This test confirms that for each response type that the command supports at least one test method exists.
* Note: While this test failing is a clear indicator something is wrong, passing this test does not mean that
* your test class is properly covering all or any functionality of the command class
*/
#[DataProvider('supportedResponseProvider')]
final public function testSupportedResponseIsTested(ResponseType $type): void
{
$method = "testInterpret" . ucfirst($type->name);
Assert::assertTrue(method_exists($this, $method), "It seems a test for response {$type->value} is missing or misnamed. Could not find method '$method' on " . static::class);
}
/**
* @phpstan-return iterable<array{0: ResponseType}>
*/
final public static function unsupportedResponseProvider(): iterable
{
$supported = static::getSupportedResponses();
foreach (ResponseType::cases() as $responseType) {
if (!in_array($responseType, $supported, true)) {
yield [$responseType];
}
}
}
#[DataProvider('unsupportedResponseProvider')]
final public function testUnsupportedResponses(ResponseType $type): void
{
$this->expectException(UnsupportedResponseException::class);
$this->getSubject()->interpret(new RawResponse($type));
}
}
|