tomkyle/mock-psr
Composer 安装命令:
composer require --dev tomkyle/mock-psr
包简介
Mock common PSR standard components in PhpUnit tests
关键字:
README 文档
README
Effortless PHPUnit mocks for common PSR interfaces – no more boilerplate, just clean tests.
Installation
$ composer require --dev tomkyle/mock-psr
Usage
This library provides two ways to use the PSR mocking functionality:
Option 1: Individual Traits
Each PSR standard has its own trait that can be used independently in your test classes:
<?php use tomkyle\MockPsr\MockPsr6CacheTrait; use tomkyle\MockPsr\MockPsr7MessagesTrait; use tomkyle\MockPsr\MockPsr11ContainerTrait; use tomkyle\MockPsr\MockPsr15RequestHandlerTrait; use tomkyle\MockPsr\MockPsr17FactoriesTrait; use tomkyle\MockPsr\MockPsr18ClientTrait; # Bonus use tomkyle\MockPsr\MockPdoTrait; class YourTest extends \PHPUnit\Framework\TestCase { use MockPsr7MessagesTrait; use MockPsr11ContainerTrait; public function testSomething() { $response = $this->mockResponse(200, 'OK'); $container = $this->mockContainer(['service' => 'value']); } }
Option 2: PsrMockFactory (Recommended)
For convenience, use the PsrMockFactory class which includes all traits and provides access to all mocking methods:
<?php use tomkyle\MockPsr\PsrMockFactory; class YourTest extends \PHPUnit\Framework\TestCase { public function testSomething() { $factory = new PsrMockFactory('test'); $response = $factory->mockResponse(200, 'OK'); $container = $factory->mockContainer(['service' => 'value']); $client = $factory->mockClient($response); } }
Examples
PSR-7 Messages
<?php use tomkyle\MockPsr\PsrMockFactory; class SomeUnitTest extends \PHPUnit\Framework\TestCase { public function testSomething() { $factory = new PsrMockFactory('test'); // Psr\Http\Message\ServerRequestInterface $server_request = $factory->mockServerRequest(); $attributes = array(); $headers = array(); $server_request = $factory->mockServerRequest($attributes, $headers); // Psr\Http\Message\UriInterface $uri = $factory->mockUri("https://test.com"); // Psr\Http\Message\RequestInterface $request = $factory->mockRequest("GET", $uri); $request = $factory->mockRequest("GET", "/home"); // Psr\Http\Message\StreamInterface $stream = $factory->mockStream("body string"); // Psr\Http\Message\ResponseInterface $response = $factory->mockResponse(200, $stream); $response = $factory->mockResponse(404, "body string"); } }
PSR-11 Container
Pass an optional array with things the Container has; calling has and get methods will behave like expected, including throwing Psr\Container\NotFoundExceptionInterface.
<?php use tomkyle\MockPsr\PsrMockFactory; class SomeUnitTest extends \PHPUnit\Framework\TestCase { public function testSomething() { $factory = new PsrMockFactory('test'); // Psr\Container\ContainerInterface $container = $factory->mockContainer(); $container = $factory->mockContainer([ 'foo' => 'bar', 'qux' => 'baz' ]); $container->has("foo"); // true $container->has("hello"); // false $container->get("hello"); // throws 'NotFoundExceptionInterface' } }
PSR-6 Cache
<?php use tomkyle\MockPsr\PsrMockFactory; class SomeUnitTest extends \PHPUnit\Framework\TestCase { public function testSomething() { $factory = new PsrMockFactory('test'); // Psr\Cache\CacheItemInterface $cache_item = $factory->mockCacheItem('cached value'); $cache_item = $factory->mockCacheItem('value', ['isHit' => true]); $missing_item = $factory->mockMissingCacheItem('missing-key'); // Psr\Cache\CacheItemPoolInterface $cache_pool = $factory->mockCacheItemPool(); $cache_pool = $factory->mockCacheItemPool($cache_item); $cache_pool = $factory->mockCacheItemPool([ 'key1' => 'value1', 'key2' => $cache_item ]); } }
PSR-15 RequestHandler
Includes MockPsr7MessagesTrait
<?php use tomkyle\MockPsr\PsrMockFactory; class SomeUnitTest extends \PHPUnit\Framework\TestCase { public function testSomething() { $factory = new PsrMockFactory('test'); // Psr\Http\Server\RequestHandlerInterface $request_handler = $factory->mockRequestHandler(); $response = $factory->mockResponse(404, "body string"); $request_handler = $factory->mockRequestHandler( $response ); } }
PSR-17 HTTP Factories
Includes MockPsr7MessagesTrait
<?php use tomkyle\MockPsr\PsrMockFactory; class SomeUnitTest extends \PHPUnit\Framework\TestCase { public function testSomething() { $factory = new PsrMockFactory('test'); // Psr\Http\Message\RequestFactoryInterface $request_factory = $factory->mockRequestFactory(); $request = $factory->mockRequest(); $request_factory = $factory->mockRequestFactory( $request ); // Psr\Http\Message\ResponseFactoryInterface $response_factory = $factory->mockResponseFactory(); $response = $factory->mockResponse(404, "body string"); $response_factory = $factory->mockResponseFactory( $response ); } }
PSR-18 HTTP Client
Includes MockPsr7MessagesTrait
<?php use tomkyle\MockPsr\PsrMockFactory; class SomeUnitTest extends \PHPUnit\Framework\TestCase { public function testSomething() { $factory = new PsrMockFactory('test'); // Psr\Http\Client\ClientInterface $client = $factory->mockClient(); $response = $factory->mockResponse(404, "body string"); $client = $factory->mockClient( $response ); } }
PDO and PDOStatements
<?php use tomkyle\MockPsr\PsrMockFactory; class SomeUnitTest extends \PHPUnit\Framework\TestCase { public function testSomething() { $factory = new PsrMockFactory('test'); // \PDOStatement $execution_result = true; $stmt = $factory->mockPdoStatement($execution_result); $stmt = $factory->mockPdoStatement(true, array("foo" => "bar")); // \PDO $pdo = $factory->mockPdo(); $pdo = $factory->mockPdo(array("attribute" => \PDO::ATTR_ERRMODE)); $stmt_2 = $pdo->prepare("SELECT"); // $stmt_2 will be a PDOStatement mock } }
Development
After cloning the repo, install dev dependencies like so:
$ composer install $ npm install
This package has predefined test setups for code quality, code readability and unit tests. Check them out at the scripts section of package.json. When working on this library, you basically only need:
$ npm run watch
tomkyle/mock-psr 适用场景与选型建议
tomkyle/mock-psr 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.17k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「http」 「cache」 「testing」 「phpunit」 「mock」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tomkyle/mock-psr 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tomkyle/mock-psr 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tomkyle/mock-psr 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
A fast and intuitive dependency injection container.
Laravel 5 - Repositories to the database layer
http客户端
Dependency injection container for the Monolith framework.
The PSR-11 container bridges
统计信息
- 总下载量: 3.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-21