PageRenderTime 62ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/vendor/laravel/framework/src/Illuminate/Foundation/Testing/AssertionsTrait.php

https://gitlab.com/zan_zan/laravel_sample
PHP | 203 lines | 100 code | 25 blank | 78 comment | 16 complexity | ff8b1cb4c5911f5fdd3650cf6bea4cdf MD5 | raw file
  1. <?php
  2. namespace Illuminate\Foundation\Testing;
  3. use Illuminate\View\View;
  4. use PHPUnit_Framework_Assert as PHPUnit;
  5. trait AssertionsTrait
  6. {
  7. /**
  8. * Assert that the client response has an OK status code.
  9. *
  10. * @return void
  11. */
  12. public function assertResponseOk()
  13. {
  14. $actual = $this->response->getStatusCode();
  15. return PHPUnit::assertTrue($this->response->isOk(), "Expected status code 200, got {$actual}.");
  16. }
  17. /**
  18. * Assert that the client response has a given code.
  19. *
  20. * @param int $code
  21. * @return void
  22. */
  23. public function assertResponseStatus($code)
  24. {
  25. $actual = $this->response->getStatusCode();
  26. return PHPUnit::assertEquals($code, $this->response->getStatusCode(), "Expected status code {$code}, got {$actual}.");
  27. }
  28. /**
  29. * Assert that the response view has a given piece of bound data.
  30. *
  31. * @param string|array $key
  32. * @param mixed $value
  33. * @return void
  34. */
  35. public function assertViewHas($key, $value = null)
  36. {
  37. if (is_array($key)) {
  38. return $this->assertViewHasAll($key);
  39. }
  40. if (!isset($this->response->original) || !$this->response->original instanceof View) {
  41. return PHPUnit::assertTrue(false, 'The response was not a view.');
  42. }
  43. if (is_null($value)) {
  44. PHPUnit::assertArrayHasKey($key, $this->response->original->getData());
  45. } else {
  46. PHPUnit::assertEquals($value, $this->response->original->$key);
  47. }
  48. }
  49. /**
  50. * Assert that the view has a given list of bound data.
  51. *
  52. * @param array $bindings
  53. * @return void
  54. */
  55. public function assertViewHasAll(array $bindings)
  56. {
  57. foreach ($bindings as $key => $value) {
  58. if (is_int($key)) {
  59. $this->assertViewHas($value);
  60. } else {
  61. $this->assertViewHas($key, $value);
  62. }
  63. }
  64. }
  65. /**
  66. * Assert that the response view is missing a piece of bound data.
  67. *
  68. * @param string $key
  69. * @return void
  70. */
  71. public function assertViewMissing($key)
  72. {
  73. if (!isset($this->response->original) || !$this->response->original instanceof View) {
  74. return PHPUnit::assertTrue(false, 'The response was not a view.');
  75. }
  76. PHPUnit::assertArrayNotHasKey($key, $this->response->original->getData());
  77. }
  78. /**
  79. * Assert whether the client was redirected to a given URI.
  80. *
  81. * @param string $uri
  82. * @param array $with
  83. * @return void
  84. */
  85. public function assertRedirectedTo($uri, $with = [])
  86. {
  87. PHPUnit::assertInstanceOf('Illuminate\Http\RedirectResponse', $this->response);
  88. PHPUnit::assertEquals($this->app['url']->to($uri), $this->response->headers->get('Location'));
  89. $this->assertSessionHasAll($with);
  90. }
  91. /**
  92. * Assert whether the client was redirected to a given route.
  93. *
  94. * @param string $name
  95. * @param array $parameters
  96. * @param array $with
  97. * @return void
  98. */
  99. public function assertRedirectedToRoute($name, $parameters = [], $with = [])
  100. {
  101. $this->assertRedirectedTo($this->app['url']->route($name, $parameters), $with);
  102. }
  103. /**
  104. * Assert whether the client was redirected to a given action.
  105. *
  106. * @param string $name
  107. * @param array $parameters
  108. * @param array $with
  109. * @return void
  110. */
  111. public function assertRedirectedToAction($name, $parameters = [], $with = [])
  112. {
  113. $this->assertRedirectedTo($this->app['url']->action($name, $parameters), $with);
  114. }
  115. /**
  116. * Assert that the session has a given list of values.
  117. *
  118. * @param string|array $key
  119. * @param mixed $value
  120. * @return void
  121. */
  122. public function assertSessionHas($key, $value = null)
  123. {
  124. if (is_array($key)) {
  125. return $this->assertSessionHasAll($key);
  126. }
  127. if (is_null($value)) {
  128. PHPUnit::assertTrue($this->app['session.store']->has($key), "Session missing key: $key");
  129. } else {
  130. PHPUnit::assertEquals($value, $this->app['session.store']->get($key));
  131. }
  132. }
  133. /**
  134. * Assert that the session has a given list of values.
  135. *
  136. * @param array $bindings
  137. * @return void
  138. */
  139. public function assertSessionHasAll(array $bindings)
  140. {
  141. foreach ($bindings as $key => $value) {
  142. if (is_int($key)) {
  143. $this->assertSessionHas($value);
  144. } else {
  145. $this->assertSessionHas($key, $value);
  146. }
  147. }
  148. }
  149. /**
  150. * Assert that the session has errors bound.
  151. *
  152. * @param string|array $bindings
  153. * @param mixed $format
  154. * @return void
  155. */
  156. public function assertSessionHasErrors($bindings = [], $format = null)
  157. {
  158. $this->assertSessionHas('errors');
  159. $bindings = (array) $bindings;
  160. $errors = $this->app['session.store']->get('errors');
  161. foreach ($bindings as $key => $value) {
  162. if (is_int($key)) {
  163. PHPUnit::assertTrue($errors->has($value), "Session missing error: $value");
  164. } else {
  165. PHPUnit::assertContains($value, $errors->get($key, $format));
  166. }
  167. }
  168. }
  169. /**
  170. * Assert that the session has old input.
  171. *
  172. * @return void
  173. */
  174. public function assertHasOldInput()
  175. {
  176. $this->assertSessionHas('_old_input');
  177. }
  178. }