Helpers.php 3.7 KB

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