Skip to content
This repository has been archived by the owner on Dec 17, 2022. It is now read-only.

Commit

Permalink
Updates for PHP7 minimum
Browse files Browse the repository at this point in the history
  • Loading branch information
Brunty committed Feb 12, 2017
1 parent 211f812 commit 8b5e17d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: php

php: [5.6, 7.0, 7.1]
php: [7.0, 7.1]

before_script:
- composer selfupdate
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Just some basic helper stuff to help test API endpoints.

## Compatibility

* PHP 5.6 and above
* PHPUnit 5.7 and above
* PHP 7.0 and above
* PHPUnit 6.0 and above
* Guzzlehttp 6.2 and above

## Installation
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
}
},
"require": {
"php": "^5.6|^7.0",
"phpunit/phpunit": "^5.7",
"php": "^7.0",
"guzzlehttp/guzzle": "^6.2",
"spatie/array-to-xml": "^2.2"
"spatie/array-to-xml": "^2.2",
"phpunit/phpunit": "^6.0"
},
"require-dev": {
"satooshi/php-coveralls": "^1.0"
Expand Down
121 changes: 28 additions & 93 deletions src/ApiTestCase.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

declare(strict_types = 1);

namespace Brunty;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use PHPUnit\Framework\TestCase;
use SimpleXMLElement;
use Spatie\ArrayToXml\ArrayToXml;

class ApiTestCase extends TestCase
Expand Down Expand Up @@ -54,89 +57,52 @@ public function setUp()
);
}

/**
* @return Client
*/
public function client()
public function client(): Client
{
return $this->client;
}

/**
* @return GuzzleResponse
*/
public function response()
public function response(): GuzzleResponse
{
return $this->response;
}

/**
* @return mixed
*/
public function statusCode()
public function statusCode(): int
{
return $this->statusCode;
}

/**
* @param string $path
* @param array $options
*/
public function get($path, array $options = [])
public function get(string $path, array $options = [])
{
$this->request('get', $path, $options);
}

/**
* @param string $path
* @param array $options
*/
public function post($path, array $options = [])
public function post(string $path, array $options = [])
{
$this->request('post', $path, $options);
}

/**
* @param string $path
* @param array $options
*/
public function patch($path, array $options = [])
public function patch(string $path, array $options = [])
{
$this->request('patch', $path, $options);
}

/**
* @param string $path
* @param array $options
*/
public function put($path, array $options = [])
public function put(string $path, array $options = [])
{
$this->request('put', $path, $options);
}

/**
* @param string $path
* @param array $options
*/
public function delete($path, array $options = [])
public function delete(string $path, array $options = [])
{
$this->request('delete', $path, $options);
}

/**
* @param $name
*
* @return \string[]
*/
public function getHeader($name)
public function getHeader(string $name): array
{
return $this->response->getHeader($name);
}

/**
* @param int $status
*/
public function assertResponseStatus($status)
public function assertResponseStatus(int $status)
{
self::assertEquals(
$status,
Expand Down Expand Up @@ -186,10 +152,7 @@ public function assertResponseWasServerError()
);
}

/**
* @param $path
*/
public function assertRedirectedTo($path)
public function assertRedirectedTo(string $path)
{
$headers = $this->response->getHeader('X-Guzzle-Redirect-History');
$path = $this->absolutePath($path);
Expand All @@ -212,26 +175,23 @@ public function assertResponseWasXml()
);
}

public function assertResponseHasKey($key)
public function assertResponseHasKey(string $key)
{
$content = $this->responseBody(true);

self::assertTrue(isset($content[$key]), sprintf('Response body does not have the key %s', $key));
}

/**
* @param string $query
* @param string $query
* @param string|null $value
*/
public function assertNodeIsValue($query, $value)
public function assertNodeIsValue(string $query, $value)
{
self::assertEquals($value, $this->query($query));
}

/**
* @return string
*/
public function rawResponseBody()
public function rawResponseBody(): string
{
return $this->response->getBody()->getContents();
}
Expand All @@ -242,7 +202,7 @@ public function rawResponseBody()
* @return array|\SimpleXMLElement|\stdClass
* @throws ContentTypeNotFoundException
*/
public function responseBody($asArray = false)
public function responseBody(bool $asArray = false)
{
if ($this->contentTypeIsJson()) {
return json_decode($this->rawResponseBody(), $asArray);
Expand All @@ -260,36 +220,22 @@ public function responseBody($asArray = false)
throw new ContentTypeNotFoundException(sprintf('Content-Type not recognised: "%s"', $this->getContentType()));
}

/**
* @return mixed
*/
public function getContentType()
public function getContentType(): string
{
return $this->response->getHeader('Content-Type')[0];
}

/**
* @return bool
*/
public function contentTypeIsXml()
public function contentTypeIsXml(): bool
{
return $this->getContentType() === self::XML_CONTENT_TYPE;
}

/**
* @return bool
*/
public function contentTypeIsJson()
public function contentTypeIsJson(): bool
{
return $this->getContentType() === self::JSON_CONTENT_TYPE;
}

/**
* @param $type
* @param $path
* @param $options
*/
private function request($type, $path, $options)
private function request(string $type, string $path, array $options = [])
{
$options = $options + [
'allow_redirects' => [
Expand All @@ -308,12 +254,7 @@ private function request($type, $path, $options)
}
}

/**
* @param $path
*
* @return mixed
*/
private function absolutePath($path)
private function absolutePath(string $path): string
{
$baseUrl = $this->baseUrl();

Expand All @@ -324,18 +265,12 @@ private function absolutePath($path)
return $path;
}

/**
* @return mixed
*/
private function baseUrl()
private function baseUrl(): string
{
return $_ENV['api_base_url'];
}

/**
* @return \SimpleXMLElement
*/
private function getBodyAsXml()
private function getBodyAsXml(): SimpleXMLElement
{
if ($this->bodyAsXml === null) {
if ($this->contentTypeIsXml()) {
Expand All @@ -351,11 +286,11 @@ private function getBodyAsXml()
}

/**
* @param $query
* @param string $query
*
* @return null|string
*/
private function query($query)
private function query(string $query)
{
$xml = $this->getBodyAsXml();

Expand Down

0 comments on commit 8b5e17d

Please sign in to comment.