TestCase.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. /**
  4. * A version of the stock PHPUnit testcase that includes some extra helpers
  5. * and default settings
  6. *
  7. * @package KO7/UnitTest
  8. *
  9. *
  10. * @copyright (c) 2007-2012 Kohana Team
  11. * @copyright (c) 2016-2018 Koseven Team
  12. * @license https://koseven.dev/LICENSE
  13. */
  14. abstract class KO7_Unittest_TestCase extends TestCase {
  15. /**
  16. * A set of Unittest helpers that are shared between normal / database testcases
  17. * @var Kohana_Unittest_Helpers
  18. */
  19. protected $_helpers;
  20. //Workaround for test
  21. private $requestTimeFloat;
  22. private $requestTime;
  23. /**
  24. * A default set of environment to be applied before each test
  25. * @var array
  26. */
  27. protected $environmentDefault = [];
  28. /**
  29. * Creates a predefined environment using the default environment
  30. *
  31. * Extending classes that have their own setUp() should call
  32. * parent::setUp()
  33. *
  34. * @throws KO7_Exception
  35. * @throws ReflectionException
  36. */
  37. public function setUp() : void
  38. {
  39. $this->requestTimeFloat = $_SERVER['REQUEST_TIME_FLOAT'];
  40. $this->requestTime = $_SERVER['REQUEST_TIME'];
  41. $this->_helpers = new Unittest_Helpers;
  42. // Make sure PHPUnit does not backup globals
  43. $this->setBackupGlobals(FALSE);
  44. $this->setEnvironment($this->environmentDefault);
  45. parent::setUp();
  46. }
  47. /**
  48. * Restores the original environment overridden with setEnvironment()
  49. *
  50. * Extending classes that have their own tearDown()
  51. * should call parent::tearDown()
  52. *
  53. * @throws KO7_Exception
  54. * @throws ReflectionException
  55. */
  56. public function tearDown() : void
  57. {
  58. $this->_helpers->restore_environment();
  59. $_SERVER['REQUEST_TIME_FLOAT'] = $this->requestTimeFloat;
  60. $_SERVER['REQUEST_TIME'] = $this->requestTime;
  61. parent::tearDown();
  62. }
  63. /**
  64. * Removes all koseven related cache files in the cache directory
  65. */
  66. public function cleanCacheDir() : void
  67. {
  68. Unittest_Helpers::clean_cache_dir();
  69. }
  70. /**
  71. * Helper function that replaces all occurrences of '/' with
  72. * the OS-specific directory separator
  73. *
  74. * @param string $path The path to act on
  75. * @return string
  76. *
  77. * @codeCoverageIgnore Gets Tested with Helpers test
  78. */
  79. public function dirSeparator(string $path) : string
  80. {
  81. return Unittest_Helpers::dir_separator($path);
  82. }
  83. /**
  84. * Allows easy setting & backing up of Environment config
  85. *
  86. * Option types are checked in the following order:
  87. *
  88. * - Server Var
  89. * - Static Variable
  90. * - Config option
  91. *
  92. * @param array $environment List of environment to set
  93. *
  94. * @return bool
  95. * @throws KO7_Exception
  96. * @throws ReflectionException
  97. */
  98. public function setEnvironment(array $environment) : bool
  99. {
  100. return $this->_helpers->set_environment($environment);
  101. }
  102. /**
  103. * Check for internet connectivity
  104. *
  105. * @return boolean Whether an internet connection is available
  106. */
  107. public function hasInternet() : bool
  108. {
  109. return Unittest_Helpers::has_internet();
  110. }
  111. /**
  112. * Evaluate an HTML or XML string and assert its structure and/or contents.
  113. *
  114. * @param array $matcher
  115. * @param string $actual
  116. * @param string $message
  117. * @param bool $isHtml
  118. *
  119. * @deprecated since 4.0
  120. */
  121. public static function assertTag(array $matcher, string $actual, $message = NULL, $isHtml = NULL) : void
  122. {
  123. KO7::deprecated('4.0');
  124. $matched = static::tag_match($matcher, $actual, $isHtml ?? TRUE);
  125. static::assertTrue($matched, $message ?? '');
  126. }
  127. /**
  128. * Helper function to match HTML string tags against certain criteria
  129. *
  130. * @param array $matcher
  131. * @param string $actual
  132. * @param bool $isHtml
  133. *
  134. * @return bool TRUE if there is a match FALSE otherwise
  135. *
  136. * @deprecated since 4.0
  137. */
  138. protected static function tag_match(array $matcher, string $actual, $isHtml = NULL) : bool
  139. {
  140. KO7::deprecated('4.0');
  141. $tags = PHPUnit\Util\Xml::load($actual, $isHtml ?? TRUE)->getElementsByTagName($matcher['tag']);
  142. return count($tags) > 0 && $tags[0] instanceof DOMNode;
  143. }
  144. }