PageRenderTime 60ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/data/claypit/c3.php

http://github.com/Codeception/Codeception
PHP | 388 lines | 291 code | 58 blank | 39 comment | 56 complexity | 8482c6b6694c7f210f9c46b1d6234e38 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. use SebastianBergmann\CodeCoverage\Driver\Driver;
  11. use SebastianBergmann\CodeCoverage\Filter as CodeCoverageFilter;
  12. if (isset($_COOKIE['CODECEPTION_CODECOVERAGE'])) {
  13. $cookie = json_decode($_COOKIE['CODECEPTION_CODECOVERAGE'], true);
  14. // fix for improperly encoded JSON in Code Coverage cookie with WebDriver.
  15. // @see https://github.com/Codeception/Codeception/issues/874
  16. if (!is_array($cookie)) {
  17. $cookie = json_decode($cookie, true);
  18. }
  19. if ($cookie) {
  20. foreach ($cookie as $key => $value) {
  21. $_SERVER["HTTP_X_CODECEPTION_" . strtoupper($key)] = $value;
  22. }
  23. }
  24. }
  25. if (!array_key_exists('HTTP_X_CODECEPTION_CODECOVERAGE', $_SERVER)) {
  26. return;
  27. }
  28. if (!function_exists('__c3_error')) {
  29. function __c3_error($message)
  30. {
  31. $errorLogFile = defined('C3_CODECOVERAGE_ERROR_LOG_FILE') ?
  32. C3_CODECOVERAGE_ERROR_LOG_FILE :
  33. C3_CODECOVERAGE_MEDIATE_STORAGE . DIRECTORY_SEPARATOR . 'error.txt';
  34. if (is_writable($errorLogFile)) {
  35. file_put_contents($errorLogFile, $message);
  36. } else {
  37. $message = "Could not write error to log file ($errorLogFile), original message: $message";
  38. }
  39. if (!headers_sent()) {
  40. header('X-Codeception-CodeCoverage-Error: ' . str_replace("\n", ' ', $message), true, 500);
  41. }
  42. setcookie('CODECEPTION_CODECOVERAGE_ERROR', $message);
  43. }
  44. }
  45. // phpunit codecoverage shimming
  46. if (!class_exists('PHP_CodeCoverage') and class_exists('SebastianBergmann\CodeCoverage\CodeCoverage')) {
  47. class_alias('SebastianBergmann\CodeCoverage\CodeCoverage', 'PHP_CodeCoverage');
  48. class_alias('SebastianBergmann\CodeCoverage\Report\Text', 'PHP_CodeCoverage_Report_Text');
  49. class_alias('SebastianBergmann\CodeCoverage\Report\PHP', 'PHP_CodeCoverage_Report_PHP');
  50. class_alias('SebastianBergmann\CodeCoverage\Report\Clover', 'PHP_CodeCoverage_Report_Clover');
  51. class_alias('SebastianBergmann\CodeCoverage\Report\Crap4j', 'PHP_CodeCoverage_Report_Crap4j');
  52. class_alias('SebastianBergmann\CodeCoverage\Report\Html\Facade', 'PHP_CodeCoverage_Report_HTML');
  53. class_alias('SebastianBergmann\CodeCoverage\Report\Xml\Facade', 'PHP_CodeCoverage_Report_XML');
  54. class_alias('SebastianBergmann\CodeCoverage\Exception', 'PHP_CodeCoverage_Exception');
  55. }
  56. // phpunit version
  57. if (!class_exists('PHPUnit_Runner_Version') && class_exists('PHPUnit\Runner\Version')) {
  58. class_alias('PHPUnit\Runner\Version', 'PHPUnit_Runner_Version');
  59. }
  60. // Autoload Codeception classes
  61. if (!class_exists('\\Codeception\\Codecept')) {
  62. if (file_exists(__DIR__ . '/codecept.phar')) {
  63. require_once 'phar://' . __DIR__ . '/codecept.phar/autoload.php';
  64. } elseif (stream_resolve_include_path(__DIR__ . '/vendor/autoload.php')) {
  65. require_once __DIR__ . '/vendor/autoload.php';
  66. // Required to load some methods only available at codeception/autoload.php
  67. if (stream_resolve_include_path(__DIR__ . '/vendor/codeception/codeception/autoload.php')) {
  68. require_once __DIR__ . '/vendor/codeception/codeception/autoload.php';
  69. }
  70. } elseif (stream_resolve_include_path('Codeception/autoload.php')) {
  71. require_once 'Codeception/autoload.php';
  72. } else {
  73. __c3_error('Codeception is not loaded. Please check that either PHAR or Composer package can be used');
  74. }
  75. }
  76. // Load Codeception Config
  77. $config_dist_file = realpath(__DIR__) . DIRECTORY_SEPARATOR . 'codeception.dist.yml';
  78. $config_file = realpath(__DIR__) . DIRECTORY_SEPARATOR . 'codeception.yml';
  79. if (isset($_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_CONFIG'])) {
  80. $config_file = realpath(__DIR__) . DIRECTORY_SEPARATOR . $_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_CONFIG'];
  81. }
  82. if (file_exists($config_file)) {
  83. // Use codeception.yml for configuration.
  84. } elseif (file_exists($config_dist_file)) {
  85. // Use codeception.dist.yml for configuration.
  86. $config_file = $config_dist_file;
  87. } else {
  88. __c3_error(sprintf("Codeception config file '%s' not found", $config_file));
  89. }
  90. try {
  91. \Codeception\Configuration::config($config_file);
  92. } catch (\Exception $e) {
  93. __c3_error($e->getMessage());
  94. }
  95. if (!defined('C3_CODECOVERAGE_MEDIATE_STORAGE')) {
  96. // workaround for 'zend_mm_heap corrupted' problem
  97. gc_disable();
  98. $memoryLimit = ini_get('memory_limit');
  99. $requiredMemory = '384M';
  100. if ((substr($memoryLimit, -1) === 'M' && (int)$memoryLimit < (int)$requiredMemory)
  101. || (substr($memoryLimit, -1) === 'K' && (int)$memoryLimit < (int)$requiredMemory * 1024)
  102. || (ctype_digit($memoryLimit) && (int)$memoryLimit < (int)$requiredMemory * 1024 * 1024)
  103. ) {
  104. ini_set('memory_limit', $requiredMemory);
  105. }
  106. define('C3_CODECOVERAGE_MEDIATE_STORAGE', Codeception\Configuration::logDir() . 'c3tmp');
  107. define('C3_CODECOVERAGE_PROJECT_ROOT', Codeception\Configuration::projectDir());
  108. define('C3_CODECOVERAGE_TESTNAME', $_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE']);
  109. function __c3_build_html_report(PHP_CodeCoverage $codeCoverage, $path)
  110. {
  111. $writer = new PHP_CodeCoverage_Report_HTML();
  112. $writer->process($codeCoverage, $path . 'html');
  113. if (file_exists($path . '.tar')) {
  114. unlink($path . '.tar');
  115. }
  116. $phar = new PharData($path . '.tar');
  117. $phar->setSignatureAlgorithm(Phar::SHA1);
  118. $files = $phar->buildFromDirectory($path . 'html');
  119. array_map('unlink', $files);
  120. if (in_array('GZ', Phar::getSupportedCompression())) {
  121. if (file_exists($path . '.tar.gz')) {
  122. unlink($path . '.tar.gz');
  123. }
  124. $phar->compress(\Phar::GZ);
  125. // close the file so that we can rename it
  126. unset($phar);
  127. unlink($path . '.tar');
  128. rename($path . '.tar.gz', $path . '.tar');
  129. }
  130. return $path . '.tar';
  131. }
  132. function __c3_build_clover_report(PHP_CodeCoverage $codeCoverage, $path)
  133. {
  134. $writer = new PHP_CodeCoverage_Report_Clover();
  135. $writer->process($codeCoverage, $path . '.clover.xml');
  136. return $path . '.clover.xml';
  137. }
  138. function __c3_build_crap4j_report(PHP_CodeCoverage $codeCoverage, $path)
  139. {
  140. $writer = new PHP_CodeCoverage_Report_Crap4j();
  141. $writer->process($codeCoverage, $path . '.crap4j.xml');
  142. return $path . '.crap4j.xml';
  143. }
  144. function __c3_build_phpunit_report(PHP_CodeCoverage $codeCoverage, $path)
  145. {
  146. $writer = new PHP_CodeCoverage_Report_XML(\PHPUnit_Runner_Version::id());
  147. $writer->process($codeCoverage, $path . 'phpunit');
  148. if (file_exists($path . '.tar')) {
  149. unlink($path . '.tar');
  150. }
  151. $phar = new PharData($path . '.tar');
  152. $phar->setSignatureAlgorithm(Phar::SHA1);
  153. $files = $phar->buildFromDirectory($path . 'phpunit');
  154. array_map('unlink', $files);
  155. if (in_array('GZ', Phar::getSupportedCompression())) {
  156. if (file_exists($path . '.tar.gz')) {
  157. unlink($path . '.tar.gz');
  158. }
  159. $phar->compress(\Phar::GZ);
  160. // close the file so that we can rename it
  161. unset($phar);
  162. unlink($path . '.tar');
  163. rename($path . '.tar.gz', $path . '.tar');
  164. }
  165. return $path . '.tar';
  166. }
  167. function __c3_send_file($filename)
  168. {
  169. if (!headers_sent()) {
  170. readfile($filename);
  171. }
  172. return __c3_exit();
  173. }
  174. /**
  175. * @param $filename
  176. * @param bool $lock Lock the file for writing?
  177. * @return [null|PHP_CodeCoverage|\SebastianBergmann\CodeCoverage\CodeCoverage, resource]
  178. */
  179. function __c3_factory($filename, $lock=false)
  180. {
  181. $file = null;
  182. if ($filename !== null && is_readable($filename)) {
  183. if ($lock) {
  184. $file = fopen($filename, 'r+');
  185. if (flock($file, LOCK_EX)) {
  186. $phpCoverage = unserialize(stream_get_contents($file));
  187. } else {
  188. __c3_error("Failed to acquire write-lock for $filename");
  189. }
  190. } else {
  191. $phpCoverage = unserialize(file_get_contents($filename));
  192. }
  193. return array($phpCoverage, $file);
  194. } else {
  195. if (method_exists(Driver::class, 'forLineCoverage')) {
  196. //php-code-coverage 9+
  197. $filter = new CodeCoverageFilter();
  198. $driver = Driver::forLineCoverage($filter);
  199. $phpCoverage = new PHP_CodeCoverage($driver, $filter);
  200. } else {
  201. //php-code-coverage 8 or older
  202. $phpCoverage = new PHP_CodeCoverage();
  203. }
  204. }
  205. if (isset($_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_SUITE'])) {
  206. $suite = $_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_SUITE'];
  207. try {
  208. $settings = \Codeception\Configuration::suiteSettings($suite, \Codeception\Configuration::config());
  209. } catch (Exception $e) {
  210. __c3_error($e->getMessage());
  211. }
  212. } else {
  213. $settings = \Codeception\Configuration::config();
  214. }
  215. try {
  216. \Codeception\Coverage\Filter::setup($phpCoverage)
  217. ->whiteList($settings)
  218. ->blackList($settings);
  219. } catch (Exception $e) {
  220. __c3_error($e->getMessage());
  221. }
  222. return array($phpCoverage, $file);
  223. }
  224. function __c3_exit()
  225. {
  226. if (!isset($_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_DEBUG'])) {
  227. exit;
  228. }
  229. return null;
  230. }
  231. function __c3_clear()
  232. {
  233. \Codeception\Util\FileSystem::doEmptyDir(C3_CODECOVERAGE_MEDIATE_STORAGE);
  234. }
  235. }
  236. if (!is_dir(C3_CODECOVERAGE_MEDIATE_STORAGE)) {
  237. if (mkdir(C3_CODECOVERAGE_MEDIATE_STORAGE, 0777, true) === false) {
  238. __c3_error('Failed to create directory "' . C3_CODECOVERAGE_MEDIATE_STORAGE . '"');
  239. }
  240. }
  241. // evaluate base path for c3-related files
  242. $path = realpath(C3_CODECOVERAGE_MEDIATE_STORAGE) . DIRECTORY_SEPARATOR . 'codecoverage';
  243. $requested_c3_report = (strpos($_SERVER['REQUEST_URI'], 'c3/report') !== false);
  244. $complete_report = $current_report = $path . '.serialized';
  245. if ($requested_c3_report) {
  246. set_time_limit(0);
  247. $route = ltrim(strrchr($_SERVER['REQUEST_URI'], '/'), '/');
  248. if ($route === 'clear') {
  249. __c3_clear();
  250. return __c3_exit();
  251. }
  252. list($codeCoverage, ) = __c3_factory($complete_report);
  253. switch ($route) {
  254. case 'html':
  255. try {
  256. __c3_send_file(__c3_build_html_report($codeCoverage, $path));
  257. } catch (Exception $e) {
  258. __c3_error($e->getMessage());
  259. }
  260. return __c3_exit();
  261. case 'clover':
  262. try {
  263. __c3_send_file(__c3_build_clover_report($codeCoverage, $path));
  264. } catch (Exception $e) {
  265. __c3_error($e->getMessage());
  266. }
  267. return __c3_exit();
  268. case 'crap4j':
  269. try {
  270. __c3_send_file(__c3_build_crap4j_report($codeCoverage, $path));
  271. } catch (Exception $e) {
  272. __c3_error($e->getMessage());
  273. }
  274. return __c3_exit();
  275. case 'serialized':
  276. try {
  277. __c3_send_file($complete_report);
  278. } catch (Exception $e) {
  279. __c3_error($e->getMessage());
  280. }
  281. return __c3_exit();
  282. case 'phpunit':
  283. try {
  284. __c3_send_file(__c3_build_phpunit_report($codeCoverage, $path));
  285. } catch (Exception $e) {
  286. __c3_error($e->getMessage());
  287. }
  288. return __c3_exit();
  289. }
  290. } else {
  291. list($codeCoverage, ) = __c3_factory(null);
  292. $codeCoverage->start(C3_CODECOVERAGE_TESTNAME);
  293. if (!array_key_exists('HTTP_X_CODECEPTION_CODECOVERAGE_DEBUG', $_SERVER)) {
  294. register_shutdown_function(
  295. function () use ($codeCoverage, $current_report) {
  296. $codeCoverage->stop();
  297. if (!file_exists(dirname($current_report))) { // verify directory exists
  298. if (!mkdir(dirname($current_report), 0777, true)) {
  299. __c3_error("Can't write CodeCoverage report into $current_report");
  300. }
  301. }
  302. // This will either lock the existing report for writing and return it along with a file pointer,
  303. // or return a fresh PHP_CodeCoverage object without a file pointer. We'll merge the current request
  304. // into that coverage object, write it to disk, and release the lock. By doing this in the end of
  305. // the request, we avoid this scenario, where Request 2 overwrites the changes from Request 1:
  306. //
  307. // Time ->
  308. // Request 1 [ <read> <write> ]
  309. // Request 2 [ <read> <write> ]
  310. //
  311. // In addition, by locking the file for exclusive writing, we make sure no other request try to
  312. // read/write to the file at the same time as this request (leading to a corrupt file). flock() is a
  313. // blocking call, so it waits until an exclusive lock can be acquired before continuing.
  314. list($existingCodeCoverage, $file) = __c3_factory($current_report, true);
  315. $existingCodeCoverage->merge($codeCoverage);
  316. if ($file === null) {
  317. file_put_contents($current_report, serialize($existingCodeCoverage), LOCK_EX);
  318. } else {
  319. fseek($file, 0);
  320. fwrite($file, serialize($existingCodeCoverage));
  321. fflush($file);
  322. flock($file, LOCK_UN);
  323. fclose($file);
  324. }
  325. }
  326. );
  327. }
  328. }
  329. // @codeCoverageIgnoreEnd