MyControllerTestCase.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. App::uses('ControllerTestCase', 'TestSuite');
  3. App::uses('Router', 'Routing');
  4. /**
  5. * MyControllerTestCase Test Case
  6. *
  7. */
  8. class MyControllerTestCase extends ControllerTestCase {
  9. /**
  10. * Constructor.
  11. *
  12. * Overwrite to default headers in case of non-cli (webtestrunner)
  13. *
  14. * @param string $base The base directory for the application. Writes `App.base` to Configure.
  15. */
  16. public function __construct($base = false) {
  17. if (php_sapi_name() !== 'cli') {
  18. $_SERVER['HTTP_REFERER'] = '';
  19. }
  20. parent::__construct($base);
  21. }
  22. /**
  23. * Overwrite to fix issue that it always defaults to POST.
  24. * That should be GET - which it now is.
  25. *
  26. * ### Options:
  27. *
  28. * - `data` Will be used as the request data. If the `method` is GET,
  29. * data will be used a GET params. If the `method` is POST, it will be used
  30. * as POST data. By setting `$options['data']` to a string, you can simulate XML or JSON
  31. * payloads to your controllers allowing you to test REST webservices.
  32. * - `method` POST or GET. Defaults to GET.
  33. * - `return` Specify the return type you want. Choose from:
  34. * - `vars` Get the set view variables.
  35. * - `view` Get the rendered view, without a layout.
  36. * - `contents` Get the rendered view including the layout.
  37. * - `result` Get the return value of the controller action. Useful
  38. * for testing requestAction methods.
  39. *
  40. * @param string $url The url to test
  41. * @param array $options See options
  42. * @return mixed
  43. */
  44. protected function _testAction($url = '', $options = []) {
  45. $options += [
  46. 'method' => 'GET',
  47. ];
  48. if (is_array($url)) {
  49. $url = Router::url($url);
  50. }
  51. return parent::_testAction($url, $options);
  52. }
  53. }