Helpers.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * @package Kohana/UnitTest
  4. * @author Kohana Team
  5. * @copyright (c) 2007-2012 Kohana Team
  6. * @copyright (c) 2016-2018 Koseven Team
  7. * @license https://koseven.ga/LICENSE.md
  8. */
  9. class Kohana_Unittest_Helpers {
  10. /**
  11. * Static variable used to work out whether we have an internet
  12. * connection
  13. * @see has_internet
  14. * @var boolean
  15. */
  16. static protected $_has_internet = NULL;
  17. /**
  18. * Check for internet connectivity
  19. *
  20. * @return boolean Whether an internet connection is available
  21. */
  22. public static function has_internet()
  23. {
  24. if ( ! isset(self::$_has_internet))
  25. {
  26. // The @ operator is used here to avoid DNS errors when there is no connection.
  27. $sock = @fsockopen("www.google.com", 80, $errno, $errstr, 1);
  28. self::$_has_internet = (bool) $sock ? TRUE : FALSE;
  29. }
  30. return self::$_has_internet;
  31. }
  32. /**
  33. * Helper function which replaces the "/" to OS-specific delimiter
  34. *
  35. * @param string $path
  36. * @return string
  37. */
  38. static public function dir_separator($path)
  39. {
  40. return str_replace('/', DIRECTORY_SEPARATOR, $path);
  41. }
  42. /**
  43. * Removes all cache files from the kohana cache dir
  44. *
  45. * @return void
  46. */
  47. static public function clean_cache_dir()
  48. {
  49. $cache_dir = opendir(Kohana::$cache_dir);
  50. while ($dir = readdir($cache_dir))
  51. {
  52. // Cache files are split into directories based on first two characters of hash
  53. if ($dir[0] !== '.' AND strlen($dir) === 2)
  54. {
  55. $dir = self::dir_separator(Kohana::$cache_dir.'/'.$dir.'/');
  56. $cache = opendir($dir);
  57. while ($file = readdir($cache))
  58. {
  59. if ($file[0] !== '.')
  60. {
  61. unlink($dir.$file);
  62. }
  63. }
  64. closedir($cache);
  65. rmdir($dir);
  66. }
  67. }
  68. closedir($cache_dir);
  69. }
  70. /**
  71. * Backup of the environment variables
  72. * @see set_environment
  73. * @var array
  74. */
  75. protected $_environment_backup = [];
  76. /**
  77. * Allows easy setting & backing up of enviroment config
  78. *
  79. * Option types are checked in the following order:
  80. *
  81. * * Server Var
  82. * * Static Variable
  83. * * Config option
  84. *
  85. * @codeCoverageIgnore
  86. * @param array $environment List of environment to set
  87. */
  88. public function set_environment(array $environment)
  89. {
  90. if ( ! count($environment))
  91. return FALSE;
  92. foreach ($environment as $option => $value)
  93. {
  94. $backup_needed = ! array_key_exists($option, $this->_environment_backup);
  95. // Handle changing superglobals
  96. if (in_array($option, ['_GET', '_POST', '_SERVER', '_FILES']))
  97. {
  98. // For some reason we need to do this in order to change the superglobals
  99. global $$option;
  100. if ($backup_needed)
  101. {
  102. $this->_environment_backup[$option] = $$option;
  103. }
  104. // PHPUnit makes a backup of superglobals automatically
  105. $$option = $value;
  106. }
  107. // If this is a static property i.e. Html::$windowed_urls
  108. elseif (strpos($option, '::$') !== FALSE)
  109. {
  110. list($class, $var) = explode('::$', $option, 2);
  111. $class = new ReflectionClass($class);
  112. if ($backup_needed)
  113. {
  114. $this->_environment_backup[$option] = $class->getStaticPropertyValue($var);
  115. }
  116. $class->setStaticPropertyValue($var, $value);
  117. }
  118. // If this is an environment variable
  119. elseif (preg_match('/^[A-Z_-]+$/', $option) OR isset($_SERVER[$option]))
  120. {
  121. if ($backup_needed)
  122. {
  123. $this->_environment_backup[$option] = isset($_SERVER[$option]) ? $_SERVER[$option] : '';
  124. }
  125. $_SERVER[$option] = $value;
  126. }
  127. // Else we assume this is a config option
  128. else
  129. {
  130. if ($backup_needed)
  131. {
  132. $this->_environment_backup[$option] = Kohana::$config->load($option);
  133. }
  134. list($group, $var) = explode('.', $option, 2);
  135. Kohana::$config->load($group)->set($var, $value);
  136. }
  137. }
  138. }
  139. /**
  140. * Restores the environment to the original state
  141. *
  142. * @codeCoverageIgnore
  143. * @chainable
  144. * @return Kohana_Unittest_Helpers $this
  145. */
  146. public function restore_environment()
  147. {
  148. $this->set_environment($this->_environment_backup);
  149. }
  150. }