Testing API's with Behat

Testing API's with Behat

B4e6cd606ce4f2122d78f259e46ea64a?s=128

Jens Segers

June 09, 2016
Tweet

Transcript

  1. None
  2. @jenssegers TESTING API’S WITH BEHAT

  3. MAKING SURE STUFF WORKS @jenssegers

  4. We’re hiring!

  5. None
  6. We’re hiring!

  7. TOOLS

  8. FACTORIES & FAKER Faker: Factories: $faker->randomFloat(2) $faker->url() $faker->text() $faker->address() Factory::listing();

  9. 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
  10. MOCKERY - “Pretend” objects for use instead of real objects

    - Programmable stubs - Replace or inspect behaviour of external services, objects, DBAL, ...
  11. 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();
  12. MOCKERY $this->app[‘dbal’] = Mockery::mock(DBAL::class); $listing = Factory::listing([‘id’ => 123]); $this->app[‘dbal’]->shouldReceive(‘listingGetById’)

    ->with(123) ->once() ->andReturn($listing); Mockery::close();
  13. Y U NO PHPUNIT?

  14. $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()); ...
  15. BEHAT

  16. 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]
  17. 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 """
  18. /** * @Given I have a valid key for agency

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

    :id */ public function iHaveAValidKeyForAgency($id) { $key = $this->aRandomValidKey(); $this->request = $this->request->withHeader( 'Authorization', 'Realo' . $key ); }
  20. 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 """
  21. /** * @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); }
  22. 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 """
  23. /** * @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)); }
  24. 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 """
  25. /** * @Then the response code should be :code */

    public function theResponseCodeShouldBe($code) { $response = $this->response(); Assert::assertNotNull($response); Assert::assertEquals($code, $response->getStatusCode()); }
  26. MOAR? docs.behat.org

  27. None
  28. None
  29. None