TestCase.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. use GuzzleHttp\Psr7;
  3. use PHPUnit\Framework\TestCase as BaseTestCase;
  4. /**
  5. * Class TestCase
  6. * @SuppressWarnings(PHPMD.NumberOfChildren)
  7. */
  8. abstract class TestCase extends BaseTestCase
  9. {
  10. /**
  11. * Returns a PSR7 Stream for a given fixture.
  12. *
  13. * @param string $fixture The fixture to create the stream for.
  14. * @return Psr7\Stream
  15. */
  16. protected function getPsr7StreamForFixture($fixture): Psr7\Stream
  17. {
  18. $path = sprintf('%s/Fixtures/%s', __DIR__, $fixture);
  19. $this->assertFileExists($path);
  20. $stream = Psr7\stream_for(file_get_contents($path));
  21. $this->assertInstanceOf(Psr7\Stream::class, $stream);
  22. return $stream;
  23. }
  24. /**
  25. * Returns a PSR7 Response (JSON) for a given fixture.
  26. *
  27. * @param string $fixture The fixture to create the response for.
  28. * @param integer $statusCode A HTTP Status Code for the response.
  29. * @return Psr7\Response
  30. */
  31. protected function getPsr7JsonResponseForFixture($fixture, $statusCode = 200): Psr7\Response
  32. {
  33. $stream = $this->getPsr7StreamForFixture($fixture);
  34. $this->assertNotNull(json_decode($stream));
  35. $this->assertEquals(JSON_ERROR_NONE, json_last_error());
  36. return new Psr7\Response($statusCode, ['Content-Type' => 'application/json'], $stream);
  37. }
  38. }