tomkyle / mock-psr
用于在 PhpUnit 测试中模拟常见 PSR 组件的特性
1.6.0
2024-08-29 07:52 UTC
Requires
- php: ^8.2
- nyholm/psr7: ^1.4
- phpspec/prophecy: ^1.15
- phpunit/phpunit: ^11.0
- psr/cache: ^1.0|^2.0|^3.0
- psr/container: ^1.0|^2.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0|^2.0
- psr/http-server-handler: ^1.0
- psr/log: ^1.1|^2.0|^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- guzzlehttp/guzzle: ^7.0
- phpstan/phpstan: ^1.11
- rector/rector: ^1.2
- symfony/cache: ^5.0|^6.0|^7.0
README
安装
$ composer require --dev tomkyle/mock-psr
使用
<?php use tomkyle\MockPsr\MockPsr11ContainerTrait; use tomkyle\MockPsr\MockPsr6CacheTrait; use tomkyle\MockPsr\MockPsr7MessagesTrait; use tomkyle\MockPsr\MockPsr15RequestHandlerTrait; use tomkyle\MockPsr\MockPsr18ClientTrait; # Bonus use tomkyle\MockPsr\MockPdoTrait;
示例
PSR-7 消息
<?php use tomkyle\MockPsr\MockPsr7MessagesTrait; class SomeUnitTest extends \PHPUnit\Framework\TestCase { use MockPsr7MessagesTrait; public function testSomething() { // Psr\Http\Message\ServerRequestInterface $server_request = $this->mockServerRequest(); $attributes = array(); $headers = array(); $server_request = $this->mockServerRequest($attributes, $headers); // Psr\Http\Message\UriInterface $uri = $this->mockUri("https://test.com"); // Psr\Http\Message\RequestInterface $request = $this->mockRequest("GET", $uri); $request = $this->mockRequest("GET", "/home"); // Psr\Http\Message\StreamInterface $stream = $this->mockStream("body string"); // Psr\Http\Message\ResponseInterface $response = $this->mockResponse(200, $stream); $response = $this->mockResponse(404, "body string"); } }
PSR-11 容器
传递一个可选的数组,包含容器具有的内容;调用 has 和 get 方法将按预期行为,包括抛出 Psr\Container\NotFoundExceptionInterface
。
<?php use tomkyle\MockPsr\MockPsr11ContainerTrait; class SomeUnitTest extends \PHPUnit\Framework\TestCase { use MockPsr11ContainerTrait; public function testSomething() { // Psr\Container\ContainerInterface $container = $this->mockContainer(); $container = $this->mockContainer([ 'foo' => 'bar', 'qux' => 'baz' ]); $container->has("foo"); // true $container->has("hello"); // false $container->get("hello"); // throws 'NotFoundExceptionInterface' } }
PSR-15 请求处理器
包括 MockPsr7MessagesTrait
<?php use tomkyle\MockPsr\MockPsr15RequestHandlerTrait; class SomeUnitTest extends \PHPUnit\Framework\TestCase { use MockPsr15RequestHandlerTrait; public function testSomething() { // Psr\Http\Server\RequestHandlerInterface $request_handler = $this->mockRequestHandler(); $response = $this->mockResponse(404, "body string"); $request_handler = $this->mockRequestHandler( $response ); } }
PSR-17 HTTP 工厂
包括 MockPsr7MessagesTrait
<?php use tomkyle\MockPsr\MockPsr17FactoriesTrait; class SomeUnitTest extends \PHPUnit\Framework\TestCase { use MockPsr17FactoriesTrait; public function testSomething() { // Psr\Http\Message\RequestFactoryInterface $request_factory = $this->mockRequestFactory(); $request = $this->mockRequest(); $request_factory = $this->mockRequestFactory( $request ); // Psr\Http\Message\ResponseFactoryInterface $response_factory = $this->mockResponseFactory(); $response = $this->mockResponse(404, "body string"); $response_factory = $this->mockResponseFactory( $response ); } }
PSR-18 HTTP 客户端
包括 MockPsr7MessagesTrait
<?php use tomkyle\MockPsr\MockPsr18ClientTrait; class SomeUnitTest extends \PHPUnit\Framework\TestCase { use MockPsr18ClientTrait; public function testSomething() { // Psr\Http\Client\ClientInterface $client = $this->mockClient(); $response = $this->mockResponse(404, "body string"); $client = $this->mockClient( $response ); } }
PDO 和 PDOStatements
<?php use tomkyle\MockPsr\MockPdoTrait; class SomeUnitTest extends \PHPUnit\Framework\TestCase { use MockPdoTrait; public function testSomething() { // \PDOStatement $execution_result = true; $stmt = $this->mockPdoStatement($execution_result); $stmt = $this->mockPdoStatement(true, array("foo" => "bar")); // \PDO $pdo = $this->mockPdo(); $pdo = $this->mockPdo($stmt); $stmt_2 = $pdo->prepare("SELECT"); $stmt_2 == $stmt } }
开发
运行所有测试
此包包含用于代码质量、代码可读性和单元测试的预定义测试设置。请查看 composer.json 的 scripts
部分。
$ composer test # ... which includes $ composer phpstan $ composer phpcs $ composer phpunit
单元测试
默认配置为 phpunit.xml.dist. 创建自定义的 phpunit.xml 以应用您自己的设置。也可以访问 phpunit.readthedocs.io · Packagist
$ composer phpunit
# ... or
$ vendor/bin/phpunit
PhpStan
默认配置为 phpstan.neon.dist. 创建自定义的 phpstan.neon 以应用您自己的设置。也可以访问 phpstan.org · GitHub · Packagist
$ composer phpstan
# ... which includes
$ vendor/bin/phpstan analyse
PhpCS
默认配置为 .php-cs-fixer.dist.php. 创建自定义的 .php-cs-fixer.php 以应用您自己的设置。也可以访问 cs.symfony.com · GitHub · Packagist
$ composer phpcs
# ... which aliases
$ vendor/bin/php-cs-fixer fix --verbose --diff --dry-run
应用所有 CS 修正
$ composer phpcs:apply
# ... which aliases
$ vendor/bin/php-cs-fixer fix --verbose --diff
在 PHP 8.2 上,需要设置环境变量 PHP_CS_FIXER_IGNORE_ENV
$ PHP_CS_FIXER_IGNORE_ENV=1 composer phpcs