https://github.com/zfegg/expressive-test
Mezzio (Zend Expressive) abstract test case
https://github.com/zfegg/expressive-test
expressive-test mezzio-test php
Last synced: 5 months ago
JSON representation
Mezzio (Zend Expressive) abstract test case
- Host: GitHub
- URL: https://github.com/zfegg/expressive-test
- Owner: zfegg
- License: mit
- Created: 2018-03-23T03:47:00.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-07-27T08:30:54.000Z (almost 2 years ago)
- Last Synced: 2025-01-26T07:11:18.137Z (over 1 year ago)
- Topics: expressive-test, mezzio-test, php
- Language: PHP
- Homepage:
- Size: 69.3 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Mezzio (Expressive) handler test
================================
[](https://github.com/zfegg/expressive-test/actions?query=workflow%3A%22qa%22)
[](https://coveralls.io/github/zfegg/expressive-test?branch=master)
[](https://packagist.org/packages/zfegg/expressive-test)
Using test tools like Laravel in Mezzio (Expressive) unit tests.
使用类似Laravel的测试工具在 Mezzio (Expressive) 的单元测试中.
Installation / 安装使用
-----------------------
Install via composer.
```bash
composer require zfegg/expressive-test --dev
```
Usage / 使用
--------------
### `runApp(...)` in test.
```php
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);
}
}
```
### Simple test methods like Laravel
```php
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');
}
}
```
### Test support methods
- `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)`
### Response assert methods
- `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`
For pass a middleware. As default it will pass [`ErrorHandler::class`](src/Helper/SetupApplicationTrait.php#L55).
```php
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();
}
}
```