kglogowski / rest-test-helper
Library to help with testing rest api
Installs: 1 115
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=7.1.0
- symfony/browser-kit: ^3.4
- symfony/framework-bundle: ^3.4
- symfony/var-dumper: ^3.4
Requires (Dev)
- incenteev/composer-parameter-handler: ^2.1
- liip/functional-test-bundle: 1.9.0
- phing/phing: 2.*
- phpmd/phpmd: 2.3.*
- phpspec/prophecy: ^1.7
- phpunit/php-invoker: ^1.1
- phpunit/phpunit: ^6.5
- sebastian/phpcpd: 2.0.*
- squizlabs/php_codesniffer: 2.*
This package is not auto-updated.
Last update: 2025-03-22 07:25:34 UTC
README
-
Run command:
composer require kglogowski/rest-test-helper
-
Basic usage:
class TestUserController extends AbstractController { const ROUTE = 'user.get'; /** * testGetSuccess */ public function testGetSuccess() { $crawler = $this->createCrawler(); $crawler ->click( Request::METHOD_GET, //Request type $this->getRequestHeaders(), //Headers, overwrite method $this->getUrl(self::ROUTE, ['id' => 1]) //Generate url ) ->checkStatus(Response::HTTP_OK) //Check status response ->child('id') //Go to child ->assertActive(ResponseCrawlerInterface::ASSERT_EQUALS, [ 1 ]) ->end() ->child('email') ->assertActive(ResponseCrawlerInterface::ASSERT_NOT_NULL) ->end() ; } }
For this example, json response should look like:
{ "id": 1, "login": "username" }
-
Test with json in request:
Request json:
{ "first_name": "FirstName", "last_name": "Surname", "email": "email@email.com", "role": "ROLE_ADMIN", "status": "ACTIVE" }
path to mock json: /mock/test.json
Code:
class TestUserController extends AbstractController { const ROUTE = 'user.post'; /** * testGetSuccess */ public function testPostSuccess() { $crawler = $this->createCrawler(); $crawler ->click( Request::METHOD_POST, $this->getRequestHeaders(), $this->getUrl(self::ROUTE), $this->getJsonMockFileContent($file) ; /** * {@inheritdoc} */ public function getMockDir(): string { return __DIR__ . '/mock/'; } } }