PHP code example of savage-days / phpunit-case-coverage
1. Go to this page and download the library: Download savage-days/phpunit-case-coverage library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
savage-days / phpunit-case-coverage example snippets
use CaseCoverage\Attribute\Group;
use CaseCoverage\CaseDefinition;
use CaseCoverage\CaseInstance;
use CaseCoverage\CaseSet;
final class PaymentClientCases extends CaseSet
{
protected static function target(): string
{
return PaymentClient::class;
}
public static function approved(): CaseDefinition
{
return self::on('charge')->returns(
new PaymentResponse(PaymentStatus::Approved),
);
}
public static function declined(): CaseDefinition
{
return self::on('charge')->returns(
new PaymentResponse(PaymentStatus::Declined),
);
}
#[Group('temporaryFailures')]
public static function timeout(): CaseDefinition
{
return self::on('charge')->throws(new TimeoutException());
}
#[Group('temporaryFailures')]
public static function connectionError(): CaseDefinition
{
return self::on('charge')->throws(new ConnectionException());
}
public static function committed(): CaseDefinition
{
return self::on('commit')->completes();
}
/** @return array<string, array{0: CaseInstance}> */
public static function temporaryFailures(): array
{
return self::forGroup(__FUNCTION__);
}
}
self::on('method')->returns($value); // a normal return value
self::on('method')->throws($exception); // a Throwable
self::on('voidMethod')->completes(); // normal completion of a void method
public function testApprovedPayment(): void
{
$client = $this->createStub(PaymentClient::class);
PaymentClientCases::case('approved')->apply($client);
$service = new PaymentService($client);
self::assertTrue($service->pay(1000)->isSuccessful());
}
use PHPUnit\Framework\Attributes\DataProviderExternal;
#[DataProviderExternal(PaymentClientCases::class, 'temporaryFailures')]
public function testTemporaryFailuresAreReportedAsRetryable(CaseInstance $case): void
{
$client = $this->createStub(PaymentClient::class);
$case->apply($client);
$service = new PaymentService($client);
self::assertTrue($service->pay(1000)->isRetryable());
}
/** @dataProvider \App\Tests\Cases\PaymentClientCases::temporaryFailures */
public function testTemporaryFailuresAreReportedAsRetryable(CaseInstance $case): void
{
// ...
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.