Exemple #1
0
 public function createAnExpectationWithProxyToTest(AcceptanceTester $I)
 {
     $expectation = PhiremockClient::on(A::postRequest()->andUrl(Is::equalTo('/potato'))->andHeader('X-Potato', Is::sameStringAs('bAnaNa'))->andScenarioState('PotatoScenario', 'Scenario.START')->andBody(Is::equalTo('{"key": "This is the body"}')))->proxyTo('https://es.wikipedia.org/wiki/Proxy');
     $this->phiremock->createExpectation($expectation);
     $I->sendGET('/__phiremock/expectations');
     $I->seeResponseCodeIs('200');
     $I->seeResponseIsJson();
     $I->seeResponseEquals('[{"scenarioName":"PotatoScenario","scenarioStateIs":"Scenario.START",' . '"newScenarioState":null,"request":{"method":"post","url":{"isEqualTo":"\\/potato"},' . '"body":{"isEqualTo":"{\\"key\\": \\"This is the body\\"}"},"headers":{"X-Potato":' . '{"isSameString":"bAnaNa"}}},"response":{"statusCode":200,"body":null,"headers":' . 'null,"delayMillis":null},' . '"proxyTo":"https:\\/\\/es.wikipedia.org\\/wiki\\/Proxy","priority":0}]');
 }
 public function failWhenInvalidValueSpecifiedTest(AcceptanceTester $I)
 {
     $I->wantTo('check if the request fails when and invalid value is specified');
     $request = new Request();
     $request->setBody(new Condition('isEqualTo', null));
     $response = new Response();
     $response->setStatusCode(201);
     $expectation = new Expectation();
     $expectation->setRequest($request)->setResponse($response);
     $I->haveHttpHeader('Content-Type', 'application/json');
     $I->sendPOST('/__phiremock/expectations', $expectation);
     $I->seeResponseCodeIs(500);
     $I->seeResponseIsJson();
     $I->seeResponseEquals('{"result" : "ERROR", "details" : ["Condition value can not be null"]}');
 }
 public function createSpecificationWithEmptyHeadersTest(AcceptanceTester $I)
 {
     $I->wantTo('create a specification with no headers in response');
     $request = new Request();
     $request->setUrl(new Condition('isEqualTo', '/the/request/url'));
     $response = new Response();
     $specification = new Expectation();
     $specification->setRequest($request)->setResponse($response);
     $I->haveHttpHeader('Content-Type', 'application/json');
     $I->sendPOST('/__phiremock/expectations', $specification);
     $I->sendGET('/__phiremock/expectations');
     $I->seeResponseCodeIs(200);
     $I->seeResponseIsJson();
     $I->seeResponseEquals('[{"scenarioName":null,"scenarioStateIs":null,"newScenarioState":null,' . '"request":{"method":null,"url":{"isEqualTo":"\\/the\\/request\\/url"},"body":null,"headers":null},' . '"response":{"statusCode":200,"body":null,"headers":null,"delayMillis":null},' . '"proxyTo":null,"priority":0}]');
 }
 public function failWhithInvalidDelayTest(AcceptanceTester $I)
 {
     $I->wantTo('create an expectation with an invalid delay specification');
     $request = new Request();
     $request->setUrl(new Condition('isEqualTo', '/the/request/url'));
     $response = new Response();
     $response->setDelayMillis('potato');
     $expectation = new Expectation();
     $expectation->setRequest($request)->setResponse($response);
     $I->haveHttpHeader('Content-Type', 'application/json');
     $I->sendPOST('/__phiremock/expectations', $expectation);
     $I->seeResponseCodeIs('500');
     $I->seeResponseIsJson();
     $I->seeResponseEquals('{"result" : "ERROR", "details" : ' . '{"response":"delayMillis: Field delayMillis, ' . 'was set with invalid value: \'potato\'"}}');
 }
 public function useDefaultWhenNoStatusCodeIsSetTest(AcceptanceTester $I)
 {
     $I->wantTo('fail when the status code is not set');
     $request = new Request();
     $request->setUrl(new Condition('isEqualTo', '/the/request/url'));
     $response = (new Response())->setStatusCode(null);
     $expectation = new Expectation();
     $expectation->setRequest($request)->setResponse($response);
     $I->haveHttpHeader('Content-Type', 'application/json');
     $I->sendPOST('/__phiremock/expectations', $expectation);
     $I->seeResponseCodeIs(201);
     $I->sendGET('/__phiremock/expectations');
     $I->seeResponseCodeIs(200);
     $I->seeResponseIsJson();
     $I->seeResponseEquals('[{"scenarioName":null,"scenarioStateIs":null,"newScenarioState":null,' . '"request":{"method":null,"url":{"isEqualTo":"\\/the\\/request\\/url"},"body":null,"headers":null},' . '"response":{"statusCode":200,"body":null,"headers":null,"delayMillis":null},' . '"proxyTo":null,"priority":0}]');
 }
 public function returnCreatedExpectationTest(AcceptanceTester $I)
 {
     $request = new Request();
     $urlCondition = new Condition('isEqualTo', '/the/request/url');
     $request->setUrl($urlCondition);
     $response = new Response();
     $response->setStatusCode(201);
     $expectation = new Expectation();
     $expectation->setRequest($request)->setResponse($response);
     $I->haveHttpHeader('Content-Type', 'application/json');
     $I->sendPOST('/__phiremock/expectations', $expectation);
     $I->sendGET('/__phiremock/expectations');
     $I->seeResponseCodeIs('200');
     $I->seeResponseIsJson();
     $I->seeResponseEquals('[{"scenarioName":null,"scenarioStateIs":null,"newScenarioState":null,' . '"request":{"method":null,"url":{"isEqualTo":"\\/the\\/request\\/url"},"body":null,"headers":null},' . '"response":{"statusCode":201,"body":null,"headers":null,"delayMillis":null},' . '"proxyTo":null,"priority":0}]');
 }
 public function creationWithMoreThanOneHeaderConditionTest(AcceptanceTester $I)
 {
     $I->wantTo('create an expectation that checks more than one header');
     $request = new Request();
     $request->setHeaders(['Content-Type' => new Condition('matches', '/application/'), 'Content-Length' => new Condition('isEqualTo', '25611'), 'Content-Encoding' => new Condition('isEqualTo', 'gzip')]);
     $response = new Response();
     $response->setStatusCode(201);
     $expectation = new Expectation();
     $expectation->setRequest($request)->setResponse($response);
     $I->haveHttpHeader('Content-Type', 'application/json');
     $I->sendPOST('/__phiremock/expectations', $expectation);
     $I->sendGET('/__phiremock/expectations');
     $I->seeResponseCodeIs('200');
     $I->seeResponseIsJson();
     $I->seeResponseEquals('[{"scenarioName":null,"scenarioStateIs":null,"newScenarioState":null,' . '"request":{"method":null,"url":null,"body":null,"headers":{' . '"Content-Type":{"matches":"\\/application\\/"},' . '"Content-Length":{"isEqualTo":"25611"},' . '"Content-Encoding":{"isEqualTo":"gzip"}}},' . '"response":{"statusCode":201,"body":null,"headers":null,"delayMillis":null},' . '"proxyTo":null,"priority":0}]');
 }
<?php

$I = new AcceptanceTester($scenario);
$I->wantTo('test various search examples');
// simple search of child table
$I->sendGet('/attendees?page=1&per_page=5');
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseJsonMatchesJsonPath('$.attendees[*].id');
// complex search on child table
$I->sendGet('/attendees?page=1&per_page=5');
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseJsonMatchesJsonPath('$.attendees[*].id');
// simple search of complex table
$I->sendGet('/events?page=1&per_page=5');
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseJsonMatchesJsonPath('$.events[*].id');
// test with syntax
$I->sendGet('/events?page=1&per_page=5&with=cabins,locations,programs,sessions');
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseJsonMatchesJsonPath('$.events[*].id');
$I->seeResponseJsonMatchesJsonPath('$.locations[*].id');
$I->seeResponseJsonMatchesJsonPath('$.programs[*].id');
$I->seeResponseJsonMatchesJsonPath('$.cabins[*].id');
$I->seeResponseJsonMatchesJsonPath('$.sessions[*].id');
// test with + single individual syntax
$I->sendGet('/events/1?page=1&per_page=5&with=cabins,locations,programs,sessions');
$I->seeResponseCodeIs(200);
 public function failWhenInvalidMethodSpecifiedTest(AcceptanceTester $I)
 {
     $I->wantTo('create a specification that checks url using matches');
     $request = new Request();
     $request->setMethod('potato');
     $response = new Response();
     $response->setStatusCode(201);
     $expectation = new Expectation();
     $expectation->setRequest($request)->setResponse($response);
     $I->haveHttpHeader('Content-Type', 'application/json');
     $I->sendPOST('/__phiremock/expectations', $expectation);
     $I->seeResponseCodeIs(500);
     $I->seeResponseIsJson();
     $I->seeResponseEquals('{"result" : "ERROR", "details" : {"request":"method: Field method, was set with invalid value: \'potato\'"}}');
 }
 public function creationWithAllOptionsFilledTest(AcceptanceTester $I)
 {
     $I->wantTo('create an expectation with all possible option filled');
     $request = (new Request())->setUrl(new Condition('isEqualTo', '/the/request/url'))->setBody(new Condition('isEqualTo', 'the body'))->setMethod('get')->setHeaders(['Content-Type' => new Condition('matches', '/json/'), 'Accepts' => new Condition('isEqualTo', 'application/json'), 'X-Some-Random-Header' => new Condition('isEqualTo', 'random value')]);
     $response = (new Response())->setStatusCode(201)->setBody('Response body')->setDelayMillis(5000)->setHeaders(['X-Special-Header' => 'potato', 'Location' => 'href://potato.tmt']);
     $expectation = (new Expectation())->setRequest($request)->setResponse($response)->setScenarioName('potato')->setScenarioStateIs('tomato')->setNewScenarioState('banana')->setPriority(3);
     $I->haveHttpHeader('Content-Type', 'application/json');
     $I->sendPOST('/__phiremock/expectations', $expectation);
     $I->sendGET('/__phiremock/expectations');
     $I->seeResponseCodeIs('200');
     $I->seeResponseIsJson();
     $I->seeResponseEquals('[{"scenarioName":"potato","scenarioStateIs":"tomato","newScenarioState":"banana",' . '"request":{' . '"method":"get","url":{"isEqualTo":"\\/the\\/request\\/url"},' . '"body":{"isEqualTo":"the body"},' . '"headers":{' . '"Content-Type":{"matches":"\\/json\\/"},' . '"Accepts":{"isEqualTo":"application\\/json"},' . '"X-Some-Random-Header":{"isEqualTo":"random value"}}},' . '"response":{' . '"statusCode":201,"body":"Response body","headers":{' . '"X-Special-Header":"potato",' . '"Location":"href:\\/\\/potato.tmt"},' . '"delayMillis":5000},' . '"proxyTo":null,"priority":3}]');
 }