CakeTestSuiteCommand.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * TestRunner for CakePHP Test suite.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.TestSuite
  17. * @since CakePHP(tm) v 2.0
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. require_once 'PHPUnit/TextUI/Command.php';
  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
  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
  55. * @param boolean $exit
  56. */
  57. public function run(array $argv, $exit = true) {
  58. $this->handleArguments($argv);
  59. $runner = $this->getRunner($this->arguments['loader']);
  60. if (is_object($this->arguments['test']) &&
  61. $this->arguments['test'] instanceof PHPUnit_Framework_Test) {
  62. $suite = $this->arguments['test'];
  63. } else {
  64. $suite = $runner->getTest(
  65. $this->arguments['test'],
  66. $this->arguments['testFile']
  67. );
  68. }
  69. if ($this->arguments['listGroups']) {
  70. PHPUnit_TextUI_TestRunner::printVersionString();
  71. print "Available test group(s):\n";
  72. $groups = $suite->getGroups();
  73. sort($groups);
  74. foreach ($groups as $group) {
  75. print " - $group\n";
  76. }
  77. exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
  78. }
  79. unset($this->arguments['test']);
  80. unset($this->arguments['testFile']);
  81. try {
  82. $result = $runner->doRun($suite, $this->arguments);
  83. } catch (PHPUnit_Framework_Exception $e) {
  84. print $e->getMessage() . "\n";
  85. }
  86. if ($exit) {
  87. if (isset($result) && $result->wasSuccessful()) {
  88. exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
  89. } elseif (!isset($result) || $result->errorCount() > 0) {
  90. exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
  91. }
  92. exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
  93. }
  94. }
  95. /**
  96. * Create a runner for the command.
  97. *
  98. * @param mixed $loader The loader to be used for the test run.
  99. * @return CakeTestRunner
  100. */
  101. public function getRunner($loader) {
  102. return new CakeTestRunner($loader, $this->_params);
  103. }
  104. /**
  105. * Handler for customizing the FixtureManager class/
  106. *
  107. * @param string $class Name of the class that will be the fixture manager
  108. * @return void
  109. */
  110. public function handleFixture($class) {
  111. $this->arguments['fixtureManager'] = $class;
  112. }
  113. /**
  114. * Handles output flag used to change printing on webrunner.
  115. *
  116. * @param string $reporter
  117. * @return void
  118. */
  119. public function handleReporter($reporter) {
  120. $object = null;
  121. $reporter = ucwords($reporter);
  122. $coreClass = 'Cake' . $reporter . 'Reporter';
  123. App::uses($coreClass, 'TestSuite/Reporter');
  124. $appClass = $reporter . 'Reporter';
  125. App::uses($appClass, 'TestSuite/Reporter');
  126. if (!class_exists($appClass)) {
  127. $object = new $coreClass(null, $this->_params);
  128. } else {
  129. $object = new $appClass(null, $this->_params);
  130. }
  131. return $this->arguments['printer'] = $object;
  132. }
  133. }