1. Go to this page and download the library: Download rybakit/phpunit-extras 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/ */
rybakit / phpunit-extras example snippets
use PHPUnitExtras\TestCase;
final class MyTest extends TestCase
{
// ...
}
use PHPUnit\Framework\TestCase;
use PHPUnitExtras\Annotation\Annotations;
final class MyTest extends TestCase
{
use Annotations;
protected function setUp() : void
{
$this->processAnnotations(static::class, $this->getName(false) ?? '');
}
// ...
}
/**
* @uires condition server.AWS_SECRET_ACCESS_KEY
*/
final class AwsS3AdapterTest extends TestCase
{
// ...
}
namespace App\Tests;
/**
* @example %target_class%
* @example %target_class_full%
*/
final class FoobarTest extends TestCase
{
// ...
}
/**
* @example %target_method%
* @example %target_method_full%
*/
public function testFoobar() : void
{
// ...
}
/**
* @log %tmp_dir%/%target_class%.%target_method%.log testing Foobar
*/
public function testFoobar() : void
{
// ...
}
namespace App\Tests\PhpUnit;
use PHPUnitExtras\Annotation\Processor\Processor;
final class SqlProcessor implements Processor
{
private $conn;
public function __construct(\PDO $conn)
{
$this->conn = $conn;
}
public function getName() : string
{
return 'sql';
}
public function process(string $value) : void
{
$this->conn->exec($value);
}
}
namespace App\Tests\PhpUnit;
use PHPUnitExtras\Annotation\PlaceholderResolver\PlaceholderResolver;
use PHPUnitExtras\Annotation\Target;
final class TableNameResolver implements PlaceholderResolver
{
public function getName() : string
{
return 'table_name';
}
/**
* Replaces all occurrences of "%table_name%" with
* "table_<short-class-name>[_<short-method-name>]".
*/
public function resolve(string $value, Target $target) : string
{
$tableName = 'table_'.$target->getClassShortName();
if ($target->isOnMethod()) {
$tableName .= '_'.$target->getMethodShortName();
}
return strtr($value, ['%table_name%' => $tableName]);
}
}
namespace App\Tests;
use App\Tests\PhpUnit\SqlProcessor;
use App\Tests\PhpUnit\TableNameResolver;
use PHPUnitExtras\Annotation\AnnotationProcessorBuilder;
use PHPUnitExtras\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
protected function createAnnotationProcessorBuilder() : AnnotationProcessorBuilder
{
return parent::createAnnotationProcessorBuilder()
->addProcessor(new SqlProcessor($this->getConnection()))
->addPlaceholderResolver(new TableNameResolver());
}
protected function getConnection() : \PDO
{
// TODO: Implement getConnection() method.
}
}
namespace App\Tests\PhpUnit;
use PHPUnitExtras\Annotation\AnnotationExtension as BaseAnnotationExtension;
use PHPUnitExtras\Annotation\AnnotationProcessorBuilder;
class AnnotationExtension extends BaseAnnotationExtension
{
private $dsn;
private $conn;
public function __construct($dsn = 'mysql:host=localhost;dbname=test')
{
$this->dsn = $dsn;
}
protected function createAnnotationProcessorBuilder() : AnnotationProcessorBuilder
{
return parent::createAnnotationProcessorBuilder()
->addProcessor(new SqlProcessor($this->getConnection()))
->addPlaceholderResolver(new TableNameResolver());
}
protected function getConnection() : \PDO
{
return $this->conn ?? $this->conn = new \PDO($this->dsn);
}
}
namespace App\Tests\PhpUnit;
use PHPUnit\Framework\Assert;
use PHPUnitExtras\Expectation\Expectation;
final class FileCreatedExpectation implements Expectation
{
private $filename;
public function __construct(string $filename)
{
Assert::assertFileDoesNotExist($filename);
$this->filename = $filename;
}
public function verify() : void
{
Assert::assertFileExists($this->filename);
}
}
use PHPUnit\Framework\TestCase;
use PHPUnitExtras\Expectation\Expectations;
final class MyTest extends TestCase
{
use Expectations;
protected function tearDown() : void
{
$this->verifyExpectations();
}
// ...
}
public function testDumpPdfToFile() : void
{
$filename = sprintf('%s/foobar.pdf', sys_get_temp_dir());
$this->expect(new FileCreatedExpectation($filename));
$this->generator->dump($filename);
}
namespace App\Tests\PhpUnit;
use PHPUnitExtras\Expectation\Expectation;
trait FileExpectations
{
public function expectFileToBeCreated(string $filename) : void
{
$this->expect(new FileCreatedExpectation($filename));
}
// ...
abstract protected function expect(Expectation $expectation) : void;
}
namespace App\Tests\PhpUnit;
use PHPUnitExtras\Expectation\ExpressionContext;
final class SelectStatementCountContext implements ExpressionContext
{
private $conn;
private $expression;
private $initialValue;
private $finalValue;
private function __construct(\PDO $conn, string $expression)
{
$this->conn = $conn;
$this->expression = $expression;
$this->initialValue = $this->getValue();
}
public static function exactly(\PDO $conn, int $count) : self
{
return new self($conn, "new_count === old_count + $count");
}
public static function atLeast(\PDO $conn, int $count) : self
{
return new self($conn, "new_count >= old_count + $count");
}
public static function atMost(\PDO $conn, int $count) : self
{
return new self($conn, "new_count <= old_count + $count");
}
public function getExpression() : string
{
return $this->expression;
}
public function getValues() : array
{
if (null === $this->finalValue) {
$this->finalValue = $this->getValue();
}
return [
'old_count' => $this->initialValue,
'new_count' => $this->finalValue,
];
}
private function getValue() : int
{
$stmt = $this->conn->query("SHOW GLOBAL STATUS LIKE 'Com_select'");
$stmt->execute();
return (int) $stmt->fetchColumn(1);
}
}
namespace App\Tests\PhpUnit;
use PHPUnitExtras\Expectation\Expectation;
use PHPUnitExtras\Expectation\ExpressionExpectation;
trait SelectStatementExpectations
{
public function expectSelectStatementToBeExecuted(int $count) : void
{
$context = SelectStatementCountContext::exactly($this->getConnection(), $count);
$this->expect(new ExpressionExpectation($context));
}
public function expectSelectStatementToBeExecutedOnce() : void
{
$this->expectSelectStatementToBeExecuted(1);
}
// ...
abstract protected function expect(Expectation $expectation) : void;
abstract protected function getConnection() : \PDO;
}
use App\Tests\PhpUnit\SelectStatementExpectations;
use PHPUnitExtras\TestCase;
final class CacheableRepositoryTest extends TestCase
{
use SelectStatementExpectations;
public function testFindByIdCachesResultSet() : void
{
$repository = $this->createRepository();
$this->expectSelectStatementToBeExecutedOnce();
$repository->findById(1);
$repository->findById(1);
}
// ...
protected function getConnection() : \PDO
{
// TODO: Implement getConnection() method.
}
}
xml
<extension class="App\Tests\PhpUnit\AnnotationExtension">
<arguments>
<string>sqlite::memory:</string>
</arguments>
</extension>
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.