ToolsTestTrait.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Tools\TestSuite;
  3. /**
  4. * Utility methods for easier testing in CakePHP & PHPUnit
  5. */
  6. trait ToolsTestTrait {
  7. /**
  8. * OsFix method
  9. *
  10. * @param string $string
  11. * @return string
  12. */
  13. protected static function osFix($string) {
  14. return str_replace(["\r\n", "\r"], "\n", $string);
  15. }
  16. /**
  17. * Checks if debug flag is set.
  18. *
  19. * Flag is set via `--debug`.
  20. * Allows additional stuff like non-mocking when enabling debug.
  21. *
  22. * @return bool Success
  23. */
  24. protected static function isDebug() {
  25. return !empty($_SERVER['argv']) && in_array('--debug', $_SERVER['argv'], true);
  26. }
  27. /**
  28. * Checks if verbose flag is set.
  29. *
  30. * Flags are `-v` and `-vv`.
  31. * Allows additional stuff like non-mocking when enabling debug.
  32. *
  33. * @param bool $onlyVeryVerbose If only -vv should be counted.
  34. * @return bool Success
  35. */
  36. protected static function isVerbose($onlyVeryVerbose = false) {
  37. if (empty($_SERVER['argv'])) {
  38. return false;
  39. }
  40. if (!$onlyVeryVerbose && in_array('-v', $_SERVER['argv'], true)) {
  41. return true;
  42. }
  43. if (in_array('-vv', $_SERVER['argv'], true)) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. /**
  49. * Outputs debug information during a test run.
  50. * This is a convenience output handler since debug() itself is not desired
  51. * for tests in general.
  52. *
  53. * Forces flushing the output if -v or -vv is set.
  54. *
  55. * @param mixed $data
  56. * @return void
  57. */
  58. protected static function debug($data) {
  59. if (!static::isVerbose()) {
  60. return;
  61. }
  62. $showFrom = static::isVerbose(true);
  63. debug($data, null, $showFrom);
  64. }
  65. }