Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

@jenssegers TESTING API’S WITH BEHAT

Slide 3

Slide 3 text

MAKING SURE STUFF WORKS @jenssegers

Slide 4

Slide 4 text

We’re hiring!

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

We’re hiring!

Slide 7

Slide 7 text

TOOLS

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

MOCKERY - “Pretend” objects for use instead of real objects - Programmable stubs - Replace or inspect behaviour of external services, objects, DBAL, ...

Slide 11

Slide 11 text

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();

Slide 12

Slide 12 text

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();

Slide 13

Slide 13 text

Y U NO PHPUNIT?

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

BEHAT

Slide 16

Slide 16 text

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]

Slide 17

Slide 17 text

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 """

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

/** * @Given I have a valid key for agency :id */ public function iHaveAValidKeyForAgency($id) { $key = $this->aRandomValidKey(); $this->request = $this->request->withHeader( 'Authorization', 'Realo' . $key ); }

Slide 20

Slide 20 text

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 """

Slide 21

Slide 21 text

/** * @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); }

Slide 22

Slide 22 text

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 """

Slide 23

Slide 23 text

/** * @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)); }

Slide 24

Slide 24 text

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 """

Slide 25

Slide 25 text

/** * @Then the response code should be :code */ public function theResponseCodeShouldBe($code) { $response = $this->response(); Assert::assertNotNull($response); Assert::assertEquals($code, $response->getStatusCode()); }

Slide 26

Slide 26 text

MOAR? docs.behat.org

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content