Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing API's with Behat

Testing API's with Behat

Avatar for Jens Segers

Jens Segers

June 09, 2016
Tweet

More Decks by Jens Segers

Other Decks in Technology

Transcript

  1. static function listing($overrides = []) { $faker = Faker\Factory::create(‘nl_BE’); $listing

    = new Listing([ ‘id’ => $faker->numberBetween(100000, 200000), ‘price’ => $faker->randomFloat(2), ‘agencyUrl => $faker->url(), ‘description’ => $faker->text(), ]); } FACTORIES & FAKER
  2. MOCKERY - “Pretend” objects for use instead of real objects

    - Programmable stubs - Replace or inspect behaviour of external services, objects, DBAL, ...
  3. MOCKERY $filesystem = Mockery::mock(Filesystem::class); $cache = new FileCache($filesystem); $cache->shouldReceive(‘write’)->once(); $cache->shouldReceive(‘read’)->times(2)->andReturn(‘bar’);

    $cache->shouldReceive(‘delete’)->never(); $cache->set(‘foo’, ‘bar’); $cache->get(‘foo’); $cache->get(‘foo’); Mockery::close();
  4. $this->app[‘request’] = new Request(‘/1.0/listings/123’); $listing = ListingFactory::create([‘id’ => 123]); $this->app[‘dbal’]

    = Mockery::mock(DBAL::class) ->shouldReceive(‘listingGetById’) ->with(‘123’) ->once() ->andReturn($listing); $response = $this->app->run(); $this->assertJson($response->getBody()); ...
  5. BEHAT - Behaviour driven acceptance testing framework - Contains features,

    scenarios and contexts - Written in natural language (Gherkin DSL): Given: initial condition [context] When: user action(s) [action] Then: expected final conditions [outcome]
  6. Feature: Retrieving single listings Scenario: Getting an existing listing Given

    I have a valid key for agency "1" And listing "1" exists with """ companyId: 1 """ When I send a "GET" request to "/1.0/listings/1" Then the response code should be 200 And the response should be JSON And the response should contain """ id: 1 """
  7. /** * @Given I have a valid key for agency

    :arg1 */ public function iHaveAValidKeyForAgency($arg1) { throw new PendingException(); }
  8. /** * @Given I have a valid key for agency

    :id */ public function iHaveAValidKeyForAgency($id) { $key = $this->aRandomValidKey(); $this->request = $this->request->withHeader( 'Authorization', 'Realo' . $key ); }
  9. Feature: Retrieving single listings Scenario: Getting an existing listing Given

    I have a valid key for agency "1" And listing "1" exists with """ companyId: 1 """ When I send a "GET" request to "/1.0/listings/1" Then the response code should be 200 And the response should be JSON And the response should contain """ id: 1 """
  10. /** * @Given listing :id exists with */ public function

    listingExistsWith($id, PyStringNode $string) { $overrides = $this->parse($string); $overrides[‘id’] = (int) $id; $listing = ModelFactory::listing($overrides); $this->dbal ->shouldReceive(‘listingGetById’) ->with($id) ->andReturn($listing); $this->addressExists($listing->addressId); }
  11. Feature: Retrieving single listings Scenario: Getting an existing listing Given

    I have a valid key for agency "1" And listing "1" exists with """ companyId: 1 """ When I send a "GET" request to "/1.0/listings/1" Then the response code should be 200 And the response should be JSON And the response should contain """ id: 1 """
  12. /** * @When I send a :method request to :url

    with */ public function iSendARequestToWith($method, $url, $string) { $data = $this->parse($string); $stream = new Stream(json_encode($data)); $this->request = $this->request ->withMethod($method) ->withUri(Uri::createFromString($url) ->withHeader(‘Content-Type’, ‘application/json’) ->withBody(new Body($stream)); }
  13. Feature: Retrieving single listings Scenario: Getting an existing listing Given

    I have a valid key for agency "1" And listing "1" exists with """ companyId: 1 """ When I send a "GET" request to "/1.0/listings/1" Then the response code should be 200 And the response should be JSON And the response should contain """ id: 1 """
  14. /** * @Then the response code should be :code */

    public function theResponseCodeShouldBe($code) { $response = $this->response(); Assert::assertNotNull($response); Assert::assertEquals($code, $response->getStatusCode()); }