CakeTestSuiteCommand.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.TestSuite
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. require 'PHPUnit/TextUI/Command.php';
  20. App::uses('CakeTestRunner', 'TestSuite');
  21. App::uses('CakeTestLoader', 'TestSuite');
  22. App::uses('CakeTestSuite', 'TestSuite');
  23. App::uses('CakeTestCase', 'TestSuite');
  24. App::uses('ControllerTestCase', 'TestSuite');
  25. App::uses('CakeTestModel', 'TestSuite/Fixture');
  26. PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT');
  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 array $params list of options to be used for this run
  37. */
  38. public function __construct($loader, $params = array()) {
  39. if ($loader && !class_exists($loader)) {
  40. throw new MissingTestLoaderException(array('class' => $loader));
  41. }
  42. $this->arguments['loader'] = $loader;
  43. $this->arguments['test'] = $params['case'];
  44. $this->arguments['testFile'] = $params;
  45. $this->_params = $params;
  46. $this->longOptions['fixture='] = 'handleFixture';
  47. $this->longOptions['output='] = 'handleReporter';
  48. }
  49. /**
  50. * Ugly hack to get around PHPUnit having a hard coded classname for the Runner. :(
  51. *
  52. * @param array $argv
  53. * @param boolean $exit
  54. */
  55. public function run(array $argv, $exit = true) {
  56. $this->handleArguments($argv);
  57. $runner = $this->getRunner($this->arguments['loader']);
  58. if (is_object($this->arguments['test']) &&
  59. $this->arguments['test'] instanceof PHPUnit_Framework_Test) {
  60. $suite = $this->arguments['test'];
  61. } else {
  62. $suite = $runner->getTest(
  63. $this->arguments['test'],
  64. $this->arguments['testFile'],
  65. $this->arguments['syntaxCheck']
  66. );
  67. }
  68. if (count($suite) == 0) {
  69. $skeleton = new PHPUnit_Util_Skeleton_Test(
  70. $suite->getName(),
  71. $this->arguments['testFile']
  72. );
  73. $result = $skeleton->generate(true);
  74. if (!$result['incomplete']) {
  75. eval(str_replace(array('<?php', '?>'), '', $result['code']));
  76. $suite = new PHPUnit_Framework_TestSuite(
  77. $this->arguments['test'] . 'Test'
  78. );
  79. }
  80. }
  81. if ($this->arguments['listGroups']) {
  82. PHPUnit_TextUI_TestRunner::printVersionString();
  83. print "Available test group(s):\n";
  84. $groups = $suite->getGroups();
  85. sort($groups);
  86. foreach ($groups as $group) {
  87. print " - $group\n";
  88. }
  89. exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
  90. }
  91. unset($this->arguments['test']);
  92. unset($this->arguments['testFile']);
  93. try {
  94. $result = $runner->doRun($suite, $this->arguments);
  95. }
  96. catch (PHPUnit_Framework_Exception $e) {
  97. print $e->getMessage() . "\n";
  98. }
  99. if ($exit) {
  100. if (isset($result) && $result->wasSuccessful()) {
  101. exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
  102. }
  103. else if (!isset($result) || $result->errorCount() > 0) {
  104. exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
  105. }
  106. else {
  107. exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
  108. }
  109. }
  110. }
  111. /**
  112. * Create a runner for the command.
  113. *
  114. * @param $loader The loader to be used for the test run.
  115. * @return CakeTestRunner
  116. */
  117. public function getRunner($loader) {
  118. return new CakeTestRunner($loader, $this->_params);
  119. }
  120. /**
  121. * Handler for customizing the FixtureManager class/
  122. *
  123. * @param string $class Name of the class that will be the fixture manager
  124. * @return void
  125. */
  126. public function handleFixture($class) {
  127. $this->arguments['fixtureManager'] = $class;
  128. }
  129. /**
  130. * Handles output flag used to change printing on webrunner.
  131. *
  132. * @return void
  133. */
  134. public function handleReporter($reporter) {
  135. $object = null;
  136. $type = strtolower($reporter);
  137. $reporter = ucwords($reporter);
  138. $coreClass = 'Cake' . $reporter . 'Reporter';
  139. App::uses($coreClass, 'TestSuite/Reporter');
  140. $appClass = $reporter . 'Reporter';
  141. App::uses($appClass, 'TestSuite/Reporter');
  142. if (!class_exists($appClass)) {
  143. $object = new $coreClass(null, $this->_params);
  144. } else {
  145. $object = new $appClass(null, $this->_params);
  146. }
  147. return $this->arguments['printer'] = $object;
  148. }
  149. }