PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/php/PHPUnit/Util/Configuration.php

https://github.com/yeco/lz77-kit
PHP | 592 lines | 300 code | 72 blank | 220 comment | 46 complexity | 22c1eb674214e808f53c928fafad8bcf MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2008, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category Testing
  38. * @package PHPUnit
  39. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @copyright 2002-2008 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @version SVN: $Id: Configuration.php 3165 2008-06-08 12:23:59Z sb $
  43. * @link http://www.phpunit.de/
  44. * @since File available since Release 3.2.0
  45. */
  46. require_once 'PHPUnit/Util/Filter.php';
  47. require_once 'PHPUnit/Runner/IncludePathTestCollector.php';
  48. require_once 'PHPUnit/Util/XML.php';
  49. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  50. /**
  51. * Wrapper for the PHPUnit XML configuration file.
  52. *
  53. * Example XML configuration file:
  54. * <code>
  55. * <?xml version="1.0" encoding="utf-8" ?>
  56. *
  57. * <phpunit convertErrorsToExceptions="true" convertNoticesToExceptions="true" stopOnFailure="false">
  58. * <testsuite name="My Test Suite">
  59. * <directory suffix="Test.php">/path/to/files</directory>
  60. * <file>/path/to/MyTest.php</file>
  61. * </testsuite>
  62. *
  63. * <groups>
  64. * <include>
  65. * <group>name</group>
  66. * </include>
  67. * <exclude>
  68. * <group>name</group>
  69. * </exclude>
  70. * </groups>
  71. *
  72. * <filter>
  73. * <blacklist>
  74. * <directory suffix=".php">/path/to/files</directory>
  75. * <file>/path/to/file</file>
  76. * <exclude>
  77. * <directory suffix=".php">/path/to/files</directory>
  78. * <file>/path/to/file</file>
  79. * </exclude>
  80. * </blacklist>
  81. * <whitelist addUncoveredFilesFromWhitelist="true">
  82. * <directory suffix=".php">/path/to/files</directory>
  83. * <file>/path/to/file</file>
  84. * <exclude>
  85. * <directory suffix=".php">/path/to/files</directory>
  86. * <file>/path/to/file</file>
  87. * </exclude>
  88. * </whitelist>
  89. * </filter>
  90. *
  91. * <logging>
  92. * <log type="coverage-html" target="/tmp/report" charset="UTF-8"
  93. * yui="true" highlight="false"
  94. * lowUpperBound="35" highLowerBound="70"/>
  95. * <log type="coverage-xml" target="/tmp/coverage.xml"/>
  96. * <log type="graphviz" target="/tmp/logfile.dot"/>
  97. * <log type="json" target="/tmp/logfile.json"/>
  98. * <log type="metrics-xml" target="/tmp/metrics.xml"/>
  99. * <log type="plain" target="/tmp/logfile.txt"/>
  100. * <log type="pmd-xml" target="/tmp/pmd.xml" cpdMinLines="5" cpdMinMatches="70"/>
  101. * <log type="tap" target="/tmp/logfile.tap"/>
  102. * <log type="test-xml" target="/tmp/logfile.xml" logIncompleteSkipped="false"/>
  103. * <log type="testdox-html" target="/tmp/testdox.html"/>
  104. * <log type="testdox-text" target="/tmp/testdox.txt"/>
  105. *
  106. * <pmd>
  107. * <rule class="PHPUnit_Util_Log_PMD_Rule_Project_CRAP"
  108. * threshold="5,30" priority="1"/>
  109. * <rule class="PHPUnit_Util_Log_PMD_Rule_Class_DepthOfInheritanceTree"
  110. * threshold="6" priority="1"/>
  111. * <rule class="PHPUnit_Util_Log_PMD_Rule_Class_EfferentCoupling"
  112. * threshold="20" priority="1"/>
  113. * <rule class="PHPUnit_Util_Log_PMD_Rule_Class_ExcessiveClassLength"
  114. * threshold="1000" priority="1"/>
  115. * <rule class="PHPUnit_Util_Log_PMD_Rule_Class_ExcessivePublicCount"
  116. * threshold="45" priority="1"/>
  117. * <rule class="PHPUnit_Util_Log_PMD_Rule_Class_TooManyFields"
  118. * threshold="15" priority="1"/>
  119. * <rule class="PHPUnit_Util_Log_PMD_Rule_Function_CodeCoverage"
  120. * threshold="35,70" priority="1"/>
  121. * <rule class="PHPUnit_Util_Log_PMD_Rule_Function_CRAP"
  122. * threshold="30" priority="1"/>
  123. * <rule class="PHPUnit_Util_Log_PMD_Rule_Function_CyclomaticComplexity"
  124. * threshold="20" priority="1"/>
  125. * <rule class="PHPUnit_Util_Log_PMD_Rule_Function_ExcessiveMethodLength"
  126. * threshold="100" priority="1"/>
  127. * <rule class="PHPUnit_Util_Log_PMD_Rule_Function_ExcessiveParameterList"
  128. * threshold="10" priority="1"/>
  129. * <rule class="PHPUnit_Util_Log_PMD_Rule_Function_NPathComplexity"
  130. * threshold="200" priority="1"/>
  131. * </pmd>
  132. * </logging>
  133. *
  134. * <php>
  135. * <ini name="foo" value="bar"/>
  136. * <var name="foo" value="bar"/>
  137. * </php>
  138. *
  139. * <selenium>
  140. * <browser name="" browser="" host="" port="" timeout="">
  141. * </selenium>
  142. * </phpunit>
  143. * </code>
  144. *
  145. * @category Testing
  146. * @package PHPUnit
  147. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  148. * @copyright 2002-2008 Sebastian Bergmann <sb@sebastian-bergmann.de>
  149. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  150. * @version Release: 3.2.21
  151. * @link http://www.phpunit.de/
  152. * @since Class available since Release 3.2.0
  153. */
  154. class PHPUnit_Util_Configuration
  155. {
  156. protected $document;
  157. protected $xpath;
  158. /**
  159. * Loads a PHPUnit configuration file.
  160. *
  161. * @param string $filename
  162. */
  163. public function __construct($filename)
  164. {
  165. $this->document = PHPUnit_Util_XML::load($filename);
  166. $this->xpath = new DOMXPath($this->document);
  167. }
  168. /**
  169. * Returns the configuration for SUT filtering.
  170. *
  171. * @return array
  172. * @since Method available since Release 3.2.1
  173. */
  174. public function getFilterConfiguration()
  175. {
  176. $addUncoveredFilesFromWhitelist = TRUE;
  177. $tmp = $this->xpath->query('filter/whitelist');
  178. if ($tmp->length == 1 &&
  179. $tmp->item(0)->hasAttribute('addUncoveredFilesFromWhitelist')) {
  180. $addUncoveredFilesFromWhitelist = $this->getBoolean(
  181. (string)$tmp->item(0)->getAttribute('addUncoveredFilesFromWhitelist'),
  182. TRUE
  183. );
  184. }
  185. return array(
  186. 'blacklist' => array(
  187. 'include' => array(
  188. 'directory' => $this->readFilterDirectories(
  189. 'filter/blacklist/directory'
  190. ),
  191. 'file' => $this->readFilterFiles(
  192. 'filter/blacklist/file'
  193. )
  194. ),
  195. 'exclude' => array(
  196. 'directory' => $this->readFilterDirectories(
  197. 'filter/blacklist/exclude/directory'
  198. ),
  199. 'file' => $this->readFilterFiles(
  200. 'filter/blacklist/exclude/file'
  201. )
  202. )
  203. ),
  204. 'whitelist' => array(
  205. 'addUncoveredFilesFromWhitelist' => $addUncoveredFilesFromWhitelist,
  206. 'include' => array(
  207. 'directory' => $this->readFilterDirectories(
  208. 'filter/whitelist/directory'
  209. ),
  210. 'file' => $this->readFilterFiles(
  211. 'filter/whitelist/file'
  212. )
  213. ),
  214. 'exclude' => array(
  215. 'directory' => $this->readFilterDirectories(
  216. 'filter/whitelist/exclude/directory'
  217. ),
  218. 'file' => $this->readFilterFiles(
  219. 'filter/whitelist/exclude/file'
  220. )
  221. )
  222. )
  223. );
  224. }
  225. /**
  226. * Returns the configuration for groups.
  227. *
  228. * @return array
  229. * @since Method available since Release 3.2.1
  230. */
  231. public function getGroupConfiguration()
  232. {
  233. $groups = array(
  234. 'include' => array(),
  235. 'exclude' => array()
  236. );
  237. foreach ($this->xpath->query('groups/include/group') as $group) {
  238. $groups['include'][] = (string)$group->nodeValue;
  239. }
  240. foreach ($this->xpath->query('groups/exclude/group') as $group) {
  241. $groups['exclude'][] = (string)$group->nodeValue;
  242. }
  243. return $groups;
  244. }
  245. /**
  246. * Returns the logging configuration.
  247. *
  248. * @return array
  249. */
  250. public function getLoggingConfiguration()
  251. {
  252. $result = array();
  253. foreach ($this->xpath->query('logging/log') as $log) {
  254. $type = (string)$log->getAttribute('type');
  255. $target = (string)$log->getAttribute('target');
  256. if ($type == 'coverage-html') {
  257. if ($log->hasAttribute('charset')) {
  258. $result['charset'] = (string)$log->getAttribute('charset');
  259. }
  260. if ($log->hasAttribute('lowUpperBound')) {
  261. $result['lowUpperBound'] = (string)$log->getAttribute('lowUpperBound');
  262. }
  263. if ($log->hasAttribute('highLowerBound')) {
  264. $result['highLowerBound'] = (string)$log->getAttribute('highLowerBound');
  265. }
  266. if ($log->hasAttribute('yui')) {
  267. $result['yui'] = $this->getBoolean(
  268. (string)$log->getAttribute('yui'),
  269. FALSE
  270. );
  271. }
  272. if ($log->hasAttribute('highlight')) {
  273. $result['highlight'] = $this->getBoolean(
  274. (string)$log->getAttribute('highlight'),
  275. FALSE
  276. );
  277. }
  278. }
  279. else if ($type == 'pmd-xml') {
  280. if ($log->hasAttribute('cpdMinLines')) {
  281. $result['cpdMinLines'] = (string)$log->getAttribute('cpdMinLines');
  282. }
  283. if ($log->hasAttribute('cpdMinMatches')) {
  284. $result['cpdMinMatches'] = (string)$log->getAttribute('cpdMinMatches');
  285. }
  286. }
  287. else if ($type == 'test-xml') {
  288. if ($log->hasAttribute('logIncompleteSkipped')) {
  289. $result['logIncompleteSkipped'] = $this->getBoolean(
  290. (string)$log->getAttribute('logIncompleteSkipped'),
  291. FALSE
  292. );
  293. }
  294. }
  295. $result[$type] = $target;
  296. }
  297. return $result;
  298. }
  299. /**
  300. * Returns the PHP configuration.
  301. *
  302. * @return array
  303. * @since Method available since Release 3.2.1
  304. */
  305. public function getPHPConfiguration()
  306. {
  307. $result = array(
  308. 'ini' => array(),
  309. 'var' => array()
  310. );
  311. foreach ($this->xpath->query('php/ini') as $ini) {
  312. $name = (string)$ini->getAttribute('name');
  313. $value = (string)$ini->getAttribute('value');
  314. $result['ini'][$name] = $value;
  315. }
  316. foreach ($this->xpath->query('php/var') as $var) {
  317. $name = (string)$var->getAttribute('name');
  318. $value = (string)$var->getAttribute('value');
  319. if (strtolower($value) == 'false') {
  320. $value = FALSE;
  321. }
  322. else if (strtolower($value) == 'true') {
  323. $value = TRUE;
  324. }
  325. $result['var'][$name] = $value;
  326. }
  327. return $result;
  328. }
  329. /**
  330. * Handles the PHP configuration.
  331. *
  332. * @since Method available since Release 3.2.20
  333. */
  334. public function handlePHPConfiguration()
  335. {
  336. $configuration = $this->getPHPConfiguration();
  337. foreach ($configuration['ini'] as $name => $value) {
  338. ini_set($name, $value);
  339. }
  340. foreach ($configuration['var'] as $name => $value) {
  341. $GLOBALS[$name] = $value;
  342. }
  343. }
  344. /**
  345. * Returns the PHPUnit configuration.
  346. *
  347. * @return array
  348. * @since Method available since Release 3.2.14
  349. */
  350. public function getPHPUnitConfiguration()
  351. {
  352. $result = array();
  353. if ($this->document->documentElement->hasAttribute('convertErrorsToExceptions')) {
  354. $result['convertErrorsToExceptions'] = $this->getBoolean(
  355. (string)$this->document->documentElement->getAttribute('convertErrorsToExceptions'),
  356. TRUE
  357. );
  358. }
  359. if ($this->document->documentElement->hasAttribute('convertNoticesToExceptions')) {
  360. $result['convertNoticesToExceptions'] = $this->getBoolean(
  361. (string)$this->document->documentElement->getAttribute('convertNoticesToExceptions'),
  362. TRUE
  363. );
  364. }
  365. if ($this->document->documentElement->hasAttribute('stopOnFailure')) {
  366. $result['stopOnFailure'] = $this->getBoolean(
  367. (string)$this->document->documentElement->getAttribute('stopOnFailure'),
  368. FALSE
  369. );
  370. }
  371. return $result;
  372. }
  373. /**
  374. * Returns the configuration for PMD rules.
  375. *
  376. * @return array
  377. */
  378. public function getPMDConfiguration()
  379. {
  380. $result = array();
  381. foreach ($this->xpath->query('logging/pmd/rule') as $rule) {
  382. $class = (string)$rule->getAttribute('class');
  383. $threshold = (string)$rule->getAttribute('threshold');
  384. $threshold = explode(',', $threshold);
  385. if (count($threshold) == 1) {
  386. $threshold = $threshold[0];
  387. }
  388. $priority = (int)$rule->getAttribute('priority');
  389. $result[$class] = array(
  390. 'threshold' => $threshold,
  391. 'priority' => $priority
  392. );
  393. }
  394. return $result;
  395. }
  396. /**
  397. * Returns the SeleniumTestCase browser configuration.
  398. *
  399. * @return array
  400. * @since Method available since Release 3.2.9
  401. */
  402. public function getSeleniumBrowserConfiguration()
  403. {
  404. $result = array();
  405. foreach ($this->xpath->query('selenium/browser') as $config) {
  406. $name = (string)$config->getAttribute('name');
  407. $browser = (string)$config->getAttribute('browser');
  408. if ($config->hasAttribute('host')) {
  409. $host = (string)$config->getAttribute('host');
  410. } else {
  411. $host = 'localhost';
  412. }
  413. if ($config->hasAttribute('port')) {
  414. $host = (int)$config->getAttribute('port');
  415. } else {
  416. $host = 4444;
  417. }
  418. if ($config->hasAttribute('timeout')) {
  419. $host = (int)$config->getAttribute('timeout');
  420. } else {
  421. $host = 30000;
  422. }
  423. $result[] = array(
  424. 'name' => $name,
  425. 'browser' => $browser,
  426. 'host' => $host,
  427. 'port' => $port,
  428. 'timeout' => $timeout
  429. );
  430. }
  431. return $result;
  432. }
  433. /**
  434. * Returns the test suite configuration.
  435. *
  436. * @return PHPUnit_Framework_TestSuite
  437. * @since Method available since Release 3.2.1
  438. */
  439. public function getTestSuiteConfiguration()
  440. {
  441. $testSuiteNode = $this->xpath->query('testsuite');
  442. if ($testSuiteNode->length > 0) {
  443. $testSuiteNode = $testSuiteNode->item(0);
  444. if ($testSuiteNode->hasAttribute('name')) {
  445. $suite = new PHPUnit_Framework_TestSuite(
  446. (string)$testSuiteNode->getAttribute('name')
  447. );
  448. } else {
  449. $suite = new PHPUnit_Framework_TestSuite;
  450. }
  451. foreach ($this->xpath->query('testsuite/directory') as $directoryNode) {
  452. if ($directoryNode->hasAttribute('suffix')) {
  453. $suffix = (string)$directoryNode->getAttribute('suffix');
  454. } else {
  455. $suffix = 'Test.php';
  456. }
  457. $testCollector = new PHPUnit_Runner_IncludePathTestCollector(
  458. array((string)$directoryNode->nodeValue),
  459. $suffix
  460. );
  461. $suite->addTestFiles($testCollector->collectTests());
  462. }
  463. foreach ($this->xpath->query('testsuite/file') as $fileNode) {
  464. $suite->addTestFile((string)$fileNode->nodeValue);
  465. }
  466. return $suite;
  467. }
  468. }
  469. /**
  470. * @param string $value
  471. * @param boolean $default
  472. * @return boolean
  473. * @since Method available since Release 3.2.3
  474. */
  475. protected function getBoolean($value, $default)
  476. {
  477. if (strtolower($value) == 'false') {
  478. return FALSE;
  479. }
  480. else if (strtolower($value) == 'true') {
  481. return TRUE;
  482. }
  483. return $default;
  484. }
  485. /**
  486. * @param string $query
  487. * @return array
  488. * @since Method available since Release 3.2.3
  489. */
  490. protected function readFilterDirectories($query)
  491. {
  492. $directories = array();
  493. foreach ($this->xpath->query($query) as $directory) {
  494. if ($directory->hasAttribute('suffix')) {
  495. $suffix = (string)$directory->getAttribute('suffix');
  496. } else {
  497. $suffix = '.php';
  498. }
  499. $directories[] = array(
  500. 'path' => (string)$directory->nodeValue,
  501. 'suffix' => $suffix
  502. );
  503. }
  504. return $directories;
  505. }
  506. /**
  507. * @param string $query
  508. * @return array
  509. * @since Method available since Release 3.2.3
  510. */
  511. protected function readFilterFiles($query)
  512. {
  513. $files = array();
  514. foreach ($this->xpath->query($query) as $file) {
  515. $files[] = (string)$file->nodeValue;
  516. }
  517. return $files;
  518. }
  519. }
  520. ?>