Base for a static organization website

CakeTestSuiteCommand.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * TestRunner for CakePHP Test suite.
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.TestSuite
  15. * @since CakePHP(tm) v 2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. if (!defined('__PHPUNIT_PHAR__')) {
  19. require_once 'PHPUnit/TextUI/Command.php';
  20. }
  21. App::uses('CakeTestRunner', 'TestSuite');
  22. App::uses('CakeTestLoader', 'TestSuite');
  23. App::uses('CakeTestSuite', 'TestSuite');
  24. App::uses('CakeTestCase', 'TestSuite');
  25. App::uses('ControllerTestCase', 'TestSuite');
  26. App::uses('CakeTestModel', 'TestSuite/Fixture');
  27. /**
  28. * Class to customize loading of test suites from CLI
  29. *
  30. * @package Cake.TestSuite
  31. */
  32. class CakeTestSuiteCommand extends PHPUnit_TextUI_Command {
  33. /**
  34. * Construct method
  35. *
  36. * @param mixed $loader The loader instance to use.
  37. * @param array $params list of options to be used for this run
  38. * @throws MissingTestLoaderException When a loader class could not be found.
  39. */
  40. public function __construct($loader, $params = array()) {
  41. if ($loader && !class_exists($loader)) {
  42. throw new MissingTestLoaderException(array('class' => $loader));
  43. }
  44. $this->arguments['loader'] = $loader;
  45. $this->arguments['test'] = $params['case'];
  46. $this->arguments['testFile'] = $params;
  47. $this->_params = $params;
  48. $this->longOptions['fixture='] = 'handleFixture';
  49. $this->longOptions['output='] = 'handleReporter';
  50. }
  51. /**
  52. * Ugly hack to get around PHPUnit having a hard coded class name for the Runner. :(
  53. *
  54. * @param array $argv The command arguments
  55. * @param bool $exit The exit mode.
  56. * @return void
  57. */
  58. public function run(array $argv, $exit = true) {
  59. $this->handleArguments($argv);
  60. $runner = $this->getRunner($this->arguments['loader']);
  61. if (is_object($this->arguments['test']) &&
  62. $this->arguments['test'] instanceof PHPUnit_Framework_Test) {
  63. $suite = $this->arguments['test'];
  64. } else {
  65. $suite = $runner->getTest(
  66. $this->arguments['test'],
  67. $this->arguments['testFile']
  68. );
  69. }
  70. if ($this->arguments['listGroups']) {
  71. PHPUnit_TextUI_TestRunner::printVersionString();
  72. print "Available test group(s):\n";
  73. $groups = $suite->getGroups();
  74. sort($groups);
  75. foreach ($groups as $group) {
  76. print " - $group\n";
  77. }
  78. exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
  79. }
  80. unset($this->arguments['test']);
  81. unset($this->arguments['testFile']);
  82. try {
  83. $result = $runner->doRun($suite, $this->arguments);
  84. } catch (PHPUnit_Framework_Exception $e) {
  85. print $e->getMessage() . "\n";
  86. }
  87. if ($exit) {
  88. if (isset($result) && $result->wasSuccessful()) {
  89. exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
  90. } elseif (!isset($result) || $result->errorCount() > 0) {
  91. exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
  92. }
  93. exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
  94. }
  95. }
  96. /**
  97. * Create a runner for the command.
  98. *
  99. * @param mixed $loader The loader to be used for the test run.
  100. * @return CakeTestRunner
  101. */
  102. public function getRunner($loader) {
  103. return new CakeTestRunner($loader, $this->_params);
  104. }
  105. /**
  106. * Handler for customizing the FixtureManager class/
  107. *
  108. * @param string $class Name of the class that will be the fixture manager
  109. * @return void
  110. */
  111. public function handleFixture($class) {
  112. $this->arguments['fixtureManager'] = $class;
  113. }
  114. /**
  115. * Handles output flag used to change printing on webrunner.
  116. *
  117. * @param string $reporter The reporter class to use.
  118. * @return void
  119. */
  120. public function handleReporter($reporter) {
  121. $object = null;
  122. $reporter = ucwords($reporter);
  123. $coreClass = 'Cake' . $reporter . 'Reporter';
  124. App::uses($coreClass, 'TestSuite/Reporter');
  125. $appClass = $reporter . 'Reporter';
  126. App::uses($appClass, 'TestSuite/Reporter');
  127. if (!class_exists($appClass)) {
  128. $object = new $coreClass(null, $this->_params);
  129. } else {
  130. $object = new $appClass(null, $this->_params);
  131. }
  132. return $this->arguments['printer'] = $object;
  133. }
  134. }