This PR moves the JSON API responses used in the tests to fixture files within the tests folder. This allows reuse and the ability to lint/validate these fixtures if required - although not covered in this PR. I've added TestCase::getPsr7StreamForFixture and TestCase::getPsr7JsonResponseForFixture to reduce code duplication and enable some assertions around the expected fixures/responses. Thanks,
42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
use GuzzleHttp\Psr7;
|
|
|
|
abstract class TestCase extends PHPUnit_Framework_TestCase
|
|
{
|
|
/**
|
|
* Returns a PSR7 Stream for a given fixture.
|
|
*
|
|
* @param string $fixture The fixture to create the stream for.
|
|
* @return Psr7Stream
|
|
*/
|
|
protected function getPsr7StreamForFixture($fixture): Psr7\Stream
|
|
{
|
|
$path = sprintf('%s/Fixtures/%s', __DIR__, $fixture);
|
|
|
|
$this->assertFileExists($path);
|
|
|
|
$stream = Psr7\stream_for(file_get_contents($path));
|
|
|
|
$this->assertInstanceOf(Psr7\Stream::class, $stream);
|
|
|
|
return $stream;
|
|
}
|
|
|
|
/**
|
|
* Returns a PSR7 Response (JSON) for a given fixture.
|
|
*
|
|
* @param string $fixture The fixture to create the response for.
|
|
* @param integer $statusCode A HTTP Status Code for the response.
|
|
* @return Psr7Response
|
|
*/
|
|
protected function getPsr7JsonResponseForFixture($fixture, $statusCode = 200): Psr7\Response
|
|
{
|
|
$stream = $this->getPsr7StreamForFixture($fixture);
|
|
|
|
$this->assertNotNull(json_decode($stream));
|
|
$this->assertEquals(JSON_ERROR_NONE, json_last_error());
|
|
|
|
return new Psr7\Response($statusCode, ['Content-Type' => 'application/json'], $stream);
|
|
}
|
|
}
|