ToolsTestTrait.php 968 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace Dereuromark\Tools\TestSuite\Traits;
  3. use Cake\Controller\Controller;
  4. use Cake\Network\Response;
  5. use Cake\Routing\Router;
  6. /**
  7. * Utility methods for easier testing in CakePHP & PHPUnit
  8. */
  9. trait ToolsTestTrait {
  10. /**
  11. * Assert a redirect happened
  12. *
  13. * `$actual` can be a string, Controller or Response instance
  14. *
  15. * @param string $expected
  16. * @param mixed $actual
  17. * @return void
  18. */
  19. public function assertRedirect($expected, $actual = null) {
  20. if ($actual === null) {
  21. $actual = $this->controller;
  22. }
  23. if ($actual instanceof Controller) {
  24. $actual = $actual->response->location();
  25. }
  26. if ($actual instanceof Response) {
  27. $actual = $actual->location();
  28. }
  29. if (empty($actual)) {
  30. throw new \Exception('assertRedirect: Expected "actual" to be a non-empty string');
  31. }
  32. if (is_array($expected)) {
  33. $expected = Router::url($expected);
  34. }
  35. $this->assertEquals($expected, $actual, 'Was not redirected to ' . $expected);
  36. }
  37. }