TestCase.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 Kohana/UnitTest
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2012 Kohana Team
  10. * @copyright (c) 2016-2018 Koseven Team
  11. * @license https://koseven.ga/LICENSE.md
  12. */
  13. abstract class Kohana_Unittest_TestCase extends TestCase {
  14. /**
  15. * Make sure PHPUnit backs up globals
  16. * @var boolean
  17. */
  18. protected $backupGlobals = FALSE;
  19. /**
  20. * A set of unittest helpers that are shared between normal / database
  21. * testcases
  22. * @var Kohana_Unittest_Helpers
  23. */
  24. protected $_helpers = NULL;
  25. /**
  26. * A default set of environment to be applied before each test
  27. * @var array
  28. */
  29. protected $environmentDefault = [];
  30. /**
  31. * Creates a predefined environment using the default environment
  32. *
  33. * Extending classes that have their own setUp() should call
  34. * parent::setUp()
  35. *
  36. * @codeCoverageIgnore
  37. */
  38. public function setUp()
  39. {
  40. $this->_helpers = new Unittest_Helpers;
  41. $this->setEnvironment($this->environmentDefault);
  42. }
  43. /**
  44. * Restores the original environment overriden with setEnvironment()
  45. *
  46. * Extending classes that have their own tearDown()
  47. * should call parent::tearDown()
  48. *
  49. * @codeCoverageIgnore
  50. */
  51. public function tearDown()
  52. {
  53. $this->_helpers->restore_environment();
  54. }
  55. /**
  56. * Removes all kohana related cache files in the cache directory
  57. */
  58. public function cleanCacheDir()
  59. {
  60. return Unittest_Helpers::clean_cache_dir();
  61. }
  62. /**
  63. * Helper function that replaces all occurences of '/' with
  64. * the OS-specific directory separator
  65. *
  66. * @param string $path The path to act on
  67. * @return string
  68. */
  69. public function dirSeparator($path)
  70. {
  71. return Unittest_Helpers::dir_separator($path);
  72. }
  73. /**
  74. * Allows easy setting & backing up of enviroment config
  75. *
  76. * Option types are checked in the following order:
  77. *
  78. * * Server Var
  79. * * Static Variable
  80. * * Config option
  81. *
  82. * @codeCoverageIgnore
  83. * @param array $environment List of environment to set
  84. */
  85. public function setEnvironment(array $environment)
  86. {
  87. return $this->_helpers->set_environment($environment);
  88. }
  89. /**
  90. * Check for internet connectivity
  91. *
  92. * @return boolean Whether an internet connection is available
  93. */
  94. public function hasInternet()
  95. {
  96. return Unittest_Helpers::has_internet();
  97. }
  98. /**
  99. * Evaluate an HTML or XML string and assert its structure and/or contents.
  100. *
  101. * NOTE:
  102. * Overriding this method to remove the deprecation error
  103. * when tested with PHPUnit 4.2.0+
  104. *
  105. * TODO:
  106. * this should be removed when phpunit-dom-assertions gets released
  107. * https://github.com/phpunit/phpunit-dom-assertions
  108. *
  109. * @param array $matcher
  110. * @param string $actual
  111. * @param string $message
  112. * @param bool $isHtml
  113. * @uses Unittest_TestCase::tag_match
  114. */
  115. public static function assertTag($matcher, $actual, $message = '', $isHtml = true)
  116. {
  117. //trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
  118. $matched = static::tag_match($matcher, $actual, $message, $isHtml);
  119. static::assertTrue($matched, $message);
  120. }
  121. /**
  122. * This assertion is the exact opposite of assertTag
  123. *
  124. * Rather than asserting that $matcher results in a match, it asserts that
  125. * $matcher does not match
  126. *
  127. * NOTE:
  128. * Overriding this method to remove the deprecation error
  129. * when tested with PHPUnit 4.2.0+
  130. *
  131. * TODO:
  132. * this should be removed when phpunit-dom-assertions gets released
  133. * https://github.com/phpunit/phpunit-dom-assertions
  134. *
  135. * @param array $matcher
  136. * @param string $actual
  137. * @param string $message
  138. * @param bool $isHtml
  139. * @uses Unittest_TestCase::tag_match
  140. */
  141. public static function assertNotTag($matcher, $actual, $message = '', $isHtml = true)
  142. {
  143. //trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
  144. $matched = static::tag_match($matcher, $actual, $message, $isHtml);
  145. static::assertTrue($matched, $message);
  146. }
  147. /**
  148. * Helper function to match HTML string tags against certain criteria
  149. *
  150. * TODO:
  151. * this should be removed when phpunit-dom-assertions gets released
  152. * https://github.com/phpunit/phpunit-dom-assertions
  153. *
  154. * @param array $matcher
  155. * @param string $actual
  156. * @param string $message
  157. * @param bool $isHtml
  158. * @return bool TRUE if there is a match FALSE otherwise
  159. */
  160. protected static function tag_match($matcher, $actual, $message = '', $isHtml = true)
  161. {
  162. $dom = PHPUnit\Util\Xml::load($actual, $isHtml);
  163. $tags = $dom->getElementsByTagName($matcher['tag']);
  164. return count($tags) > 0 && $tags[0] instanceof DOMNode;
  165. }
  166. }