/src/Opulence/Framework/Tests/Http/Testing/PhpUnit/Assertions/ResponseAssertionsTest.php

https://github.com/opulencephp/Opulence · PHP · 173 lines · 106 code · 17 blank · 50 comment · 0 complexity · c5b9d19495815564cc477289451e5235 MD5 · raw file

  1. <?php
  2. /*
  3. * Opulence
  4. *
  5. * @link https://www.opulencephp.com
  6. * @copyright Copyright (C) 2017 David Young
  7. * @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md
  8. */
  9. namespace Opulence\Framework\Tests\Http\Testing\PhpUnit\Assertions;
  10. use DateTime;
  11. use Opulence\Framework\Http\Testing\PhpUnit\Assertions\ResponseAssertions;
  12. use Opulence\Http\Responses\Cookie;
  13. use Opulence\Http\Responses\JsonResponse;
  14. use Opulence\Http\Responses\RedirectResponse;
  15. use Opulence\Http\Responses\Response;
  16. use Opulence\Http\Responses\ResponseHeaders;
  17. /**
  18. * Tests the response assertions
  19. */
  20. class ResponseAssertionsTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /** @var ResponseAssertions The assertions to use in tests */
  23. private $assertions = null;
  24. /**
  25. * Sets up the tests
  26. */
  27. public function setUp()
  28. {
  29. $this->assertions = new ResponseAssertions();
  30. }
  31. /**
  32. * Tests asserting that a path redirects to another
  33. */
  34. public function testAssertingRedirect()
  35. {
  36. $this->assertions->setResponse(new RedirectResponse('/redirectedPath'));
  37. $this->assertSame($this->assertions, $this->assertions->redirectsTo('/redirectedPath'));
  38. }
  39. /**
  40. * Tests asserting that a response has certain content
  41. */
  42. public function testAssertingResponseHasContent()
  43. {
  44. $this->assertions->setResponse(new Response('FooBar'));
  45. $this->assertSame($this->assertions, $this->assertions->contentEquals('FooBar'));
  46. }
  47. /**
  48. * Tests asserting that a response has a certain cookie
  49. */
  50. public function testAssertingResponseHasCookie()
  51. {
  52. $response = new Response();
  53. $response->getHeaders()->setCookie(
  54. new Cookie('foo', 'bar', new DateTime('+1 week'))
  55. );
  56. $this->assertions->setResponse($response);
  57. $this->assertSame($this->assertions, $this->assertions->hasCookie('foo'));
  58. $this->assertSame($this->assertions, $this->assertions->cookieValueEquals('foo', 'bar'));
  59. }
  60. /**
  61. * Tests asserting that a response has a certain header
  62. */
  63. public function testAssertingResponseHasHeader()
  64. {
  65. $response = new Response();
  66. $response->getHeaders()->set('foo', 'bar');
  67. $this->assertions->setResponse($response);
  68. $this->assertSame($this->assertions, $this->assertions->hasHeader('foo'));
  69. $this->assertSame($this->assertions, $this->assertions->headerEquals('foo', 'bar'));
  70. }
  71. /**
  72. * Tests asserting that a response has status code
  73. */
  74. public function testAssertingResponseHasStatusCode()
  75. {
  76. $response = new Response('', ResponseHeaders::HTTP_BAD_GATEWAY);
  77. $this->assertions->setResponse($response);
  78. $this->assertSame(
  79. $this->assertions,
  80. $this->assertions->statusCodeEquals(ResponseHeaders::HTTP_BAD_GATEWAY)
  81. );
  82. }
  83. /**
  84. * Tests asserting that a response is an internal server error
  85. */
  86. public function testAssertingResponseIsInternalServerError()
  87. {
  88. $response = new Response('', ResponseHeaders::HTTP_INTERNAL_SERVER_ERROR);
  89. $this->assertions->setResponse($response);
  90. $this->assertSame($this->assertions, $this->assertions->isInternalServerError());
  91. }
  92. /**
  93. * Tests asserting that a response is not found
  94. */
  95. public function testAssertingResponseIsNotFound()
  96. {
  97. $response = new Response('', ResponseHeaders::HTTP_NOT_FOUND);
  98. $this->assertions->setResponse($response);
  99. $this->assertSame($this->assertions, $this->assertions->isNotFound());
  100. }
  101. /**
  102. * Tests asserting that a response is OK
  103. */
  104. public function testAssertingResponseIsOK()
  105. {
  106. $response = new Response('', ResponseHeaders::HTTP_OK);
  107. $this->assertions->setResponse($response);
  108. $this->assertSame($this->assertions, $this->assertions->isOK());
  109. }
  110. /**
  111. * Tests asserting that a response is unauthorized
  112. */
  113. public function testAssertingResponseIsUnauthorized()
  114. {
  115. $response = new Response('', ResponseHeaders::HTTP_UNAUTHORIZED);
  116. $this->assertions->setResponse($response);
  117. $this->assertSame($this->assertions, $this->assertions->isUnauthorized());
  118. }
  119. /**
  120. * Tests asserting response JSON contains
  121. */
  122. public function testAssertingResponseJsonContains()
  123. {
  124. $response = new JsonResponse(['foo' => 'bar', 'baz' => ['subkey' => 'subvalue']]);
  125. $this->assertions->setResponse($response);
  126. $this->assertSame($this->assertions, $this->assertions->jsonContains(['foo' => 'bar']));
  127. $this->assertSame(
  128. $this->assertions,
  129. $this->assertions->jsonContains(['baz' => ['subkey' => 'subvalue']])
  130. );
  131. $this->assertSame(
  132. $this->assertions,
  133. $this->assertions->jsonContains(['subkey' => 'subvalue'])
  134. );
  135. }
  136. /**
  137. * Tests asserting response JSON contains key
  138. */
  139. public function testAssertingResponseJsonContainsKey()
  140. {
  141. $response = new JsonResponse(['foo' => 'bar', 'baz' => ['subkey' => 'subvalue']]);
  142. $this->assertions->setResponse($response);
  143. $this->assertSame($this->assertions, $this->assertions->jsonContainsKey('foo'));
  144. $this->assertSame($this->assertions, $this->assertions->jsonContainsKey('subkey'));
  145. }
  146. /**
  147. * Tests asserting response JSON equals
  148. */
  149. public function testAssertingResponseJsonEquals()
  150. {
  151. $response = new JsonResponse(['foo' => 'bar', 'baz' => ['subkey' => 'subvalue']]);
  152. $this->assertions->setResponse($response);
  153. $this->assertSame($this->assertions,
  154. $this->assertions->jsonEquals(['foo' => 'bar', 'baz' => ['subkey' => 'subvalue']]));
  155. }
  156. }