2017 © Pedro Peláez
 

library api-testcase

A testcase that extends PHPUnit to help with testing HTTP (mainly API) endpoints.

image

brunty/api-testcase

A testcase that extends PHPUnit to help with testing HTTP (mainly API) endpoints.

  • Tuesday, April 11, 2017
  • by Brunty
  • Repository
  • 2 Watchers
  • 6 Stars
  • 18 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 13 % Grown

The README.md

Brunty\ApiTestCase

Build Status Coverage Status SensioLabsInsight, (*1)

Just some basic helper stuff to help test API endpoints., (*2)

Compatibility

  • PHP 7.0 and above
  • PHPUnit 6.0 and above
  • Guzzlehttp 6.2 and above

Installation

composer require brunty/api-testcase --dev, (*3)

Usage

Add an environment variable to your PHPUnit Configuration that's your API's base URL:, (*4)

<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
    <php>
        <env name="api_base_url" value="http://httpbin.org"/>
    </php>
</phpunit>

Extend the \Brunty\ApiTestCase class. If you need to configure the client, call $this->configureClientOptions($options); before calling parent::setUp():, (*5)

<?php

use Brunty\ApiTestCase;

class BooksApiTest extends ApiTestCase
{
    public function setUp()
    {
        $options = [
            // ...
        ];

        // use this if you want to add additional options to the client when it's constructed
        $this->configureClientOptions($options);
        parent::setUp();
    }
}

Methods and requests available

The test case uses Guzzle (\GuzzleHttp\Client) under the surface, so requests are effectively just made through that. If you need to access the client, you can do so with $this->client(); within your test class., (*6)

GET

get(string $path [, array $options]), (*7)

<?php

use Brunty\ApiTestCase;

class BooksApiTest extends ApiTestCase
{
    /**
     * @test
     */
    public function the_api_retrieves_all_books()
    {
        $this->get('/books');
        $this->assertResponseOk();
    }
}

POST

post(string $path [, array $options]), (*8)

<?php

use Brunty\ApiTestCase;

class BooksApiTest extends ApiTestCase
{
    /**
     * @test
     */
    public function the_api_creates_a_book()
    {
        $this->post('/books', ['title' => 'My Book']);
        $this->assertResponseOk();
    }
}

PATCH

patch(string $path [, array $options]), (*9)

<?php

use Brunty\ApiTestCase;

class BooksApiTest extends ApiTestCase
{
    /**
     * @test
     */
    public function the_api_updates_a_book()
    {
        $this->patch('/books/1', ['title' => 'My Updated Book']);
        $this->assertResponseOk();
    }
}

PUT

put(string $path [, array $options]), (*10)

<?php

use Brunty\ApiTestCase;

class BooksApiTest extends ApiTestCase
{
    /**
     * @test
     */
    public function the_api_creates_or_updates_a_book()
    {
        $this->put('/books', ['title' => 'My Updated Book']);
        $this->assertResponseOk();
    }
}

DELETE

delete(string $path [, array $options]), (*11)

<?php

use Brunty\ApiTestCase;

class BooksApiTest extends ApiTestCase
{
    /**
     * @test
     */
    public function the_api_deletes_a_book()
    {
        $this->delete('/books/1');
        $this->assertResponseOk();
    }
}

Headers & Responses

getHeader(string $name), (*12)

Returns a response header matching the name., (*13)

response(), (*14)

Returns the response object., (*15)

statusCode(), (*16)

Returns the status code from the response., (*17)

rawResponseBody(), (*18)

Returns the contents of the body of the response., (*19)

responseBody($asArray), (*20)

Returns the response body, parsed into either an array (if $asArray is true) or: \stdClass if the response was JSON, \SimpleXmlElement if the response was XML., (*21)

If the content type of the response cannot be determined to be either XML or JSON, a \Brunty\ContentTypeNotFound exception will be thrown., (*22)

getContentType(), (*23)

Returns the value of the first Content-Type header element., (*24)

contentTypeIsXml(), (*25)

Returns true if the content type is XML, false otherwise., (*26)

contentTypeIsJson(), (*27)

Returns true if the content type is JSON, false otherwise., (*28)

The \Brunty\Response class contains a list of constants for all HTTP status codes - these can help make status code assertions more readable - for example:, (*29)

$this->assertResponseStatus(\Brunty\Response::HTTP_NO_CONTENT); as opposed to $this->assertResponseStatus(204);, (*30)

Assertions

Assertion Notes
assertResponseStatus($status)
assertResponseOk() (Response code 200)
assertResponseWasSuccess() (200 <= Response Code < 300)
assertResponseWasRedirect() (300 <= Response Code < 400) Note that you may need to set the allow_redirects option to false otherwise status codes of the page after the redirect can be used.
assertResponseWasClientError() (400 <= Response Code < 500)
assertResponseWasServerError() (500 <= Response Code)
assertResponseWasJson()
assertResponseWasXml()
assertResponseHasKey($key)
assertNodeIsValue($xPathQuery, $value) Runs the xpath query against the result (yes, even for JSON - though | that's a bit experimental) and asserts that the value is correct - currently only works with strings.
assertRedirectedTo($path) Path can be absolute, or relative to the root api_base_url

Contributing

This started as a project of boredom one Friday evening, if you find yourself using this, and want more features, please feel free to suggest them, or submit a PR!, (*31)

Although this project is small, openness and inclusivity are taken seriously. To that end the following code of conduct has been adopted., (*32)

Contributor Code of Conduct, (*33)

The Versions

11/04 2017

dev-master

9999999-dev

A testcase that extends PHPUnit to help with testing HTTP (mainly API) endpoints.

  Sources   Download

MIT

The Requires

 

The Development Requires

api phpunit http testcase

09/04 2017

1.0.2

1.0.2.0

A testcase that extends PHPUnit to help with testing HTTP (mainly API) endpoints.

  Sources   Download

MIT

The Requires

 

The Development Requires

api phpunit http testcase

18/02 2017

1.0.1

1.0.1.0

A testcase that extends PHPUnit to help with testing API endpoints.

  Sources   Download

MIT

The Requires

 

The Development Requires

api phpunit testcase

12/02 2017

1.0

1.0.0.0

A testcase that extends PHPUnit to help with testing API endpoints.

  Sources   Download

MIT

The Requires

 

The Development Requires

api phpunit testcase

19/12 2016

0.4.0

0.4.0.0

A testcase that extends PHPUnit to help with testing API endpoints.

  Sources   Download

MIT

The Requires

 

The Development Requires

api phpunit testcase

10/12 2016

0.3.0

0.3.0.0

A testcase that extends PHPUnit to help with testing API endpoints.

  Sources   Download

MIT

The Requires

 

The Development Requires

api phpunit testcase

10/12 2016

0.2.0

0.2.0.0

A testcase that extends PHPUnit to help with testing API endpoints.

  Sources   Download

MIT

The Requires

 

api phpunit testcase

09/12 2016

0.1.0

0.1.0.0

A testcase that extends PHPUnit to help with testing API endpoints.

  Sources   Download

MIT

The Requires

 

api phpunit testcase