AssertionsTrait.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\testing;
  12. use think\response\View;
  13. use PHPUnit_Framework_Assert as PHPUnit;
  14. use think\Session;
  15. use think\Url;
  16. trait AssertionsTrait
  17. {
  18. public function assertResponseOk()
  19. {
  20. $actual = $this->response->getCode();
  21. PHPUnit::assertTrue(200 == $actual, "Expected status code 200, got {$actual}.");
  22. }
  23. public function assertResponseStatus($code)
  24. {
  25. $actual = $this->response->getCode();
  26. PHPUnit::assertEquals($code, $actual, "Expected status code {$code}, got {$actual}.");
  27. }
  28. public function assertViewHas($key, $value = null)
  29. {
  30. if (is_array($key)) {
  31. $this->assertViewHasAll($key);
  32. } else {
  33. if (!$this->response instanceof View) {
  34. PHPUnit::assertTrue(false, 'The response was not a view.');
  35. } else {
  36. if (is_null($value)) {
  37. PHPUnit::assertArrayHasKey($key, $this->response->getVars());
  38. } else {
  39. PHPUnit::assertEquals($value, $this->response->getVars($key));
  40. }
  41. }
  42. }
  43. }
  44. public function assertViewHasAll(array $bindings)
  45. {
  46. foreach ($bindings as $key => $value) {
  47. if (is_int($key)) {
  48. $this->assertViewHas($value);
  49. } else {
  50. $this->assertViewHas($key, $value);
  51. }
  52. }
  53. }
  54. public function assertViewMissing($key)
  55. {
  56. if (!$this->response instanceof View) {
  57. PHPUnit::assertTrue(false, 'The response was not a view.');
  58. } else {
  59. PHPUnit::assertArrayNotHasKey($key, $this->response->getVars());
  60. }
  61. }
  62. public function assertRedirectedTo($uri, $params = [])
  63. {
  64. $this->assertInstanceOf('think\response\Redirect', $this->response);
  65. PHPUnit::assertEquals(Url::build($uri, $params), $this->response->getTargetUrl());
  66. }
  67. public function assertSessionHas($key, $value = null)
  68. {
  69. if (is_array($key)) {
  70. $this->assertSessionHasAll($key);
  71. } else {
  72. if (is_null($value)) {
  73. PHPUnit::assertTrue(Session::has($key), "Session missing key: $key");
  74. } else {
  75. PHPUnit::assertEquals($value, Session::get($key));
  76. }
  77. }
  78. }
  79. public function assertSessionHasAll(array $bindings)
  80. {
  81. foreach ($bindings as $key => $value) {
  82. if (is_int($key)) {
  83. $this->assertSessionHas($value);
  84. } else {
  85. $this->assertSessionHas($key, $value);
  86. }
  87. }
  88. }
  89. }