PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/codeception/codeception/tests/data/claypit/c3.php

https://gitlab.com/itlboy/yii2-starter-installed
PHP | 234 lines | 175 code | 41 blank | 18 comment | 26 complexity | 0f5459ac69451bd34006983f5bb6b5ec MD5 | raw file
  1. <?php
  2. // @codingStandardsIgnoreFile
  3. // @codeCoverageIgnoreStart
  4. /**
  5. * C3 - Codeception Code Coverage
  6. *
  7. * @author tiger
  8. */
  9. // $_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_DEBUG'] = 1;
  10. if (isset($_COOKIE['CODECEPTION_CODECOVERAGE'])) {
  11. $cookie = json_decode($_COOKIE['CODECEPTION_CODECOVERAGE'], true);
  12. if ($cookie) {
  13. foreach ($cookie as $key => $value) {
  14. $_SERVER["HTTP_X_CODECEPTION_".strtoupper($key)] = $value;
  15. }
  16. }
  17. }
  18. if (!array_key_exists('HTTP_X_CODECEPTION_CODECOVERAGE', $_SERVER)) {
  19. return;
  20. }
  21. // Autoload Codeception classes
  22. if (!class_exists('\\Codeception\\Codecept')) {
  23. if (stream_resolve_include_path(__DIR__ . '/vendor/autoload.php')) {
  24. require_once __DIR__ . '/vendor/autoload.php';
  25. } elseif (file_exists(__DIR__ . '/codecept.phar')) {
  26. require_once 'phar://'.__DIR__ . '/codecept.phar/autoload.php';
  27. } elseif (stream_resolve_include_path('Codeception/autoload.php')) {
  28. require_once 'Codeception/autoload.php';
  29. } else {
  30. __c3_error('Codeception is not loaded. Please check that either PHAR or Composer or PEAR package can be used');
  31. }
  32. }
  33. // Load Codeception Config
  34. $config_file = realpath(__DIR__) . DIRECTORY_SEPARATOR . 'codeception.yml';
  35. if (array_key_exists('HTTP_X_CODECEPTION_CODECOVERAGE_CONFIG', $_SERVER)) {
  36. $config_file = realpath(__DIR__) . DIRECTORY_SEPARATOR . $_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_CONFIG'];
  37. }
  38. if (!file_exists($config_file)) {
  39. __c3_error(sprintf("Codeception config file '%s' not found", $config_file));
  40. }
  41. try {
  42. \Codeception\Configuration::config($config_file);
  43. } catch (\Exception $e) {
  44. __c3_error($e->getMessage());
  45. }
  46. if (!defined('C3_CODECOVERAGE_MEDIATE_STORAGE')) {
  47. // workaround for 'zend_mm_heap corrupted' problem
  48. gc_disable();
  49. if ((integer)ini_get('memory_limit') < 384) {
  50. ini_set('memory_limit', '384M');
  51. }
  52. define('C3_CODECOVERAGE_MEDIATE_STORAGE', Codeception\Configuration::logDir() . 'c3tmp');
  53. define('C3_CODECOVERAGE_PROJECT_ROOT', Codeception\Configuration::projectDir());
  54. define('C3_CODECOVERAGE_TESTNAME', $_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE']);
  55. function __c3_build_html_report(PHP_CodeCoverage $codeCoverage, $path)
  56. {
  57. $writer = new PHP_CodeCoverage_Report_HTML();
  58. $writer->process($codeCoverage, $path . 'html');
  59. if (file_exists($path . '.tar')) {
  60. unlink($path . '.tar');
  61. }
  62. $phar = new PharData($path . '.tar');
  63. $phar->setSignatureAlgorithm(Phar::SHA1);
  64. $files = $phar->buildFromDirectory($path . 'html');
  65. array_map('unlink', $files);
  66. if (in_array('GZ', Phar::getSupportedCompression())) {
  67. if (file_exists($path . '.tar.gz')) {
  68. unlink($path . '.tar.gz');
  69. }
  70. $phar->compress(\Phar::GZ);
  71. // close the file so that we can rename it
  72. unset($phar);
  73. unlink($path . '.tar');
  74. rename($path . '.tar.gz', $path . '.tar');
  75. }
  76. return $path . '.tar';
  77. }
  78. function __c3_build_clover_report(PHP_CodeCoverage $codeCoverage, $path)
  79. {
  80. $writer = new PHP_CodeCoverage_Report_Clover();
  81. $writer->process($codeCoverage, $path . '.clover.xml');
  82. return $path . '.clover.xml';
  83. }
  84. function __c3_send_file($filename)
  85. {
  86. if (!headers_sent()) {
  87. readfile($filename);
  88. }
  89. return __c3_exit();
  90. }
  91. /**
  92. * @param $filename
  93. * @return null|PHP_CodeCoverage
  94. */
  95. function __c3_factory($filename)
  96. {
  97. $phpCoverage = is_readable($filename)
  98. ? unserialize(file_get_contents($filename))
  99. : new PHP_CodeCoverage();
  100. if (isset($_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_SUITE'])) {
  101. $suite = $_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_SUITE'];
  102. try {
  103. $settings = \Codeception\Configuration::suiteSettings($suite, \Codeception\Configuration::config());
  104. } catch (Exception $e) {
  105. __c3_error($e->getMessage());
  106. }
  107. } else {
  108. $settings = \Codeception\Configuration::config();
  109. }
  110. try {
  111. \Codeception\Coverage\Filter::setup($phpCoverage)
  112. ->whiteList($settings)
  113. ->blackList($settings);
  114. } catch (Exception $e) {
  115. __c3_error($e->getMessage());
  116. }
  117. return $phpCoverage;
  118. }
  119. function __c3_exit()
  120. {
  121. if (!isset($_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_DEBUG'])) {
  122. exit;
  123. }
  124. return null;
  125. }
  126. function __c3_error($message)
  127. {
  128. file_put_contents(C3_CODECOVERAGE_MEDIATE_STORAGE . DIRECTORY_SEPARATOR . 'error.txt', $message);
  129. if (!headers_sent()) {
  130. header('X-Codeception-CodeCoverage-Error: ' . str_replace("\n", ' ', $message), true, 500);
  131. }
  132. setcookie('CODECEPTION_CODECOVERAGE_ERROR', $message);
  133. __c3_exit();
  134. }
  135. function __c3_clear()
  136. {
  137. \Codeception\Util\FileSystem::doEmptyDir(C3_CODECOVERAGE_MEDIATE_STORAGE);
  138. }
  139. }
  140. if (!is_dir(C3_CODECOVERAGE_MEDIATE_STORAGE)) {
  141. if (mkdir(C3_CODECOVERAGE_MEDIATE_STORAGE, 0777, true) === false) {
  142. __c3_error('Failed to create directory "' . C3_CODECOVERAGE_MEDIATE_STORAGE . '"');
  143. }
  144. }
  145. // evaluate base path for c3-related files
  146. $path = realpath(C3_CODECOVERAGE_MEDIATE_STORAGE) . DIRECTORY_SEPARATOR . 'codecoverage';
  147. $requested_c3_report = (strpos($_SERVER['REQUEST_URI'], 'c3/report') !== false);
  148. $complete_report = $current_report = $path . '.serialized';
  149. if ($requested_c3_report) {
  150. set_time_limit(0);
  151. $route = ltrim(strrchr($_SERVER['REQUEST_URI'], '/'), '/');
  152. if ($route == 'clear') {
  153. __c3_clear();
  154. return __c3_exit();
  155. }
  156. $codeCoverage = __c3_factory($complete_report);
  157. switch ($route) {
  158. case 'html':
  159. try {
  160. __c3_send_file(__c3_build_html_report($codeCoverage, $path));
  161. } catch (Exception $e) {
  162. __c3_error($e->getMessage());
  163. }
  164. return __c3_exit();
  165. case 'clover':
  166. try {
  167. __c3_send_file(__c3_build_clover_report($codeCoverage, $path));
  168. } catch (Exception $e) {
  169. __c3_error($e->getMessage());
  170. }
  171. return __c3_exit();
  172. case 'serialized':
  173. try {
  174. __c3_send_file($complete_report);
  175. } catch (Exception $e) {
  176. __c3_error($e->getMessage());
  177. }
  178. return __c3_exit();
  179. }
  180. } else {
  181. $codeCoverage = __c3_factory($current_report);
  182. $codeCoverage->start(C3_CODECOVERAGE_TESTNAME);
  183. register_shutdown_function(
  184. function () use ($codeCoverage, $current_report) {
  185. $codeCoverage->stop();
  186. if (!file_exists(dirname($current_report))) {
  187. mkdir(dirname($current_report), 0777, true);
  188. }
  189. file_put_contents($current_report, serialize($codeCoverage));
  190. }
  191. );
  192. }
  193. // @codeCoverageIgnoreEnd