建立測試檔案
$ php artisan make:test ApiTest
tests\Feature\ApiTest.php
class ApiTest extends TestCase
{
/**
* 測試取得 token
*
* @return void
*/
public function testGetToken()
{
$parameters = [
'grant_type' => 'client_credentials',
'client_id' => '3',
'client_secret' => '2Ur6aIyJCZpAeuTwcWHyCq9YGYsPYwN23GDg4xJW',
];
// 傳送參數
$response = $this->call('POST', 'oauth/token', $parameters);
$this->assertEquals(200, $response->getStatusCode());
// 測試回傳欄位是否存在
$data = json_decode($response->getContent(), true);
$this->assertArrayHasKey('token_type', $data);
$this->assertArrayHasKey('expires_in', $data);
$this->assertArrayHasKey('access_token', $data);
}
}
Unit test
$ vendor\bin\phpunit tests\Feature\ApiTest.php
Docs
參考來源
https://laracasts.com/discuss/channels/testing/how-to-test-json-structure-with-phpunit