zfegg / expressive-test
Zend Expressive 的 PHPUnit 抽象测试用例
0.7.1
2024-06-14 18:37 UTC
Requires
- php: >=8.1
- dflydev/fig-cookies: ^2.0 | ^3.0
- laminas/laminas-diactoros: ^3.0
- laminas/laminas-servicemanager: ^3.3 | ^4.0
- mezzio/mezzio: ^3.0
- phpunit/phpunit: >=8.0
Requires (Dev)
README
在 Mezzio (Expressive) 的单元测试中使用类似 Laravel 的测试工具。
在 Mezzio (Expressive) 的单元测试中使用类似 Laravel 的测试工具。
安装/安装使用
通过 composer 安装。
composer require zfegg/expressive-test --dev
使用/使用
在测试中使用 runApp(...)
use Psr\Http\Message\ResponseInterface; use Zfegg\ExpressiveTest\AbstractActionTestCase; class HomePageTest extends AbstractActionTestCase { public function testHome() { $response = $this->runApp( 'POST', '/?test=1', ['body' => '2'], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"a":"b"}', ['cookie' => '3'] ); $this->assertInstanceOf(ResponseInterface::class, $response); } public function testPassMiddlewareOrMockService() { $this->container->setService('some middleware', new PassMiddleware()); $mock = $this->createMock(SomeService::class); $this->container->setService('mock some service', $mock); $response = $this->runApp( 'POST', '/?test=1', ['body' => '2'], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"a":"b"}', ['cookie' => '3'] ); $this->assertInstanceOf(ResponseInterface::class, $response); } }
简单的测试方法,类似于 Laravel
use Psr\Http\Message\ResponseInterface; use Zfegg\ExpressiveTest\AbstractActionTestCase; class HomePageTest extends AbstractActionTestCase { public function testHome() { /* $this->get($uri, $headers = []); $this->getJson($uri, $headers = []); $this->post($uri, $data = [], $headers = []); $this->postJson($uri, $data = [], $headers = []); $this->put($uri, $data = [], $headers = []); $this->putJson($uri, $data = [], $headers = []); $this->patch($uri, $data = [], $headers = []); $this->patchJson($uri, $data = [], $headers = []); $this->delete($uri, $data = [], $headers = []); $this->json($method, $uri, $data = [], $headers = []); $this->call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null); */ $response = $this->getJson('/?test=1'); $response->assertOk(); $response->assertSuccessful(); } public function testRedirectLogin() { $response = $this->getJson('/redirect'); $response->assertRedirect('/login'); } }
测试支持方法
get($uri, $headers = [])
getJson($uri, $headers = [])
post($uri, $data = [], $headers = [])
postJson($uri, $data = [], $headers = [])
put($uri, $data = [], $headers = [])
putJson($uri, $data = [], $headers = [])
patch($uri, $data = [], $headers = [])
patchJson($uri, $data = [], $headers = [])
delete($uri, $data = [], $headers = [])
json($method, $uri, $data = [], $headers = [])
call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
响应断言方法
assertCookie
assertCookieExpired
assertCookieMissing
assertCookieNotExpired
assertCreated
assertDontSee
assertDontSeeText
assertExactJson
assertForbidden
assertHeader
assertHeaderMissing
assertJson
assertJsonCount
assertJsonMessage
assertJsonMissing
assertJsonMissingExact
assertJsonPath
assertJsonStructure
assertLocation
assertNoContent
assertNotFound
assertOk
assertRedirect
assertSee
assertSeeText
assertStatus
assertSuccessful
assertUnauthorized
PassMiddleware
为了通过中间件。默认情况下,它将通过 ErrorHandler::class
。
use Psr\Http\Message\ResponseInterface; use Zfegg\ExpressiveTest\AbstractActionTestCase; use Zfegg\ExpressiveTest\PassMiddleware; class HomePageTest extends AbstractActionTestCase { public function testHome() { // Pass a authentication middleware. $this->container->setService(AuthenticationMiddleware::class, PassMiddleware::class); $response = $this->getJson('/api/users'); $response->assertOk(); } }