CakeTestSuiteCommand.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_once '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. /**
  27. * Class to customize loading of test suites from CLI
  28. *
  29. * @package Cake.TestSuite
  30. */
  31. class CakeTestSuiteCommand extends PHPUnit_TextUI_Command {
  32. /**
  33. * Construct method
  34. *
  35. * @param array $params list of options to be used for this run
  36. */
  37. public function __construct($loader, $params = array()) {
  38. if ($loader && !class_exists($loader)) {
  39. throw new MissingTestLoaderException(array('class' => $loader));
  40. }
  41. $this->arguments['loader'] = $loader;
  42. $this->arguments['test'] = $params['case'];
  43. $this->arguments['testFile'] = $params;
  44. $this->_params = $params;
  45. $this->longOptions['fixture='] = 'handleFixture';
  46. $this->longOptions['output='] = 'handleReporter';
  47. }
  48. /**
  49. * Ugly hack to get around PHPUnit having a hard coded classname for the Runner. :(
  50. *
  51. * @param array $argv
  52. * @param boolean $exit
  53. */
  54. public function run(array $argv, $exit = true) {
  55. $this->handleArguments($argv);
  56. $runner = $this->getRunner($this->arguments['loader']);
  57. if (is_object($this->arguments['test']) &&
  58. $this->arguments['test'] instanceof PHPUnit_Framework_Test) {
  59. $suite = $this->arguments['test'];
  60. } else {
  61. $suite = $runner->getTest(
  62. $this->arguments['test'],
  63. $this->arguments['testFile']
  64. );
  65. }
  66. if (count($suite) == 0) {
  67. $skeleton = new PHPUnit_Util_Skeleton_Test(
  68. $suite->getName(),
  69. $this->arguments['testFile']
  70. );
  71. $result = $skeleton->generate(true);
  72. if (!$result['incomplete']) {
  73. eval(str_replace(array('<?php', '?>'), '', $result['code']));
  74. $suite = new PHPUnit_Framework_TestSuite(
  75. $this->arguments['test'] . 'Test'
  76. );
  77. }
  78. }
  79. if ($this->arguments['listGroups']) {
  80. PHPUnit_TextUI_TestRunner::printVersionString();
  81. print "Available test group(s):\n";
  82. $groups = $suite->getGroups();
  83. sort($groups);
  84. foreach ($groups as $group) {
  85. print " - $group\n";
  86. }
  87. exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
  88. }
  89. unset($this->arguments['test']);
  90. unset($this->arguments['testFile']);
  91. try {
  92. $result = $runner->doRun($suite, $this->arguments);
  93. }
  94. catch (PHPUnit_Framework_Exception $e) {
  95. print $e->getMessage() . "\n";
  96. }
  97. if ($exit) {
  98. if (isset($result) && $result->wasSuccessful()) {
  99. exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
  100. }
  101. else if (!isset($result) || $result->errorCount() > 0) {
  102. exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT);
  103. }
  104. else {
  105. exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
  106. }
  107. }
  108. }
  109. /**
  110. * Create a runner for the command.
  111. *
  112. * @param $loader The loader to be used for the test run.
  113. * @return CakeTestRunner
  114. */
  115. public function getRunner($loader) {
  116. return new CakeTestRunner($loader, $this->_params);
  117. }
  118. /**
  119. * Handler for customizing the FixtureManager class/
  120. *
  121. * @param string $class Name of the class that will be the fixture manager
  122. * @return void
  123. */
  124. public function handleFixture($class) {
  125. $this->arguments['fixtureManager'] = $class;
  126. }
  127. /**
  128. * Handles output flag used to change printing on webrunner.
  129. *
  130. * @return void
  131. */
  132. public function handleReporter($reporter) {
  133. $object = null;
  134. $type = strtolower($reporter);
  135. $reporter = ucwords($reporter);
  136. $coreClass = 'Cake' . $reporter . 'Reporter';
  137. App::uses($coreClass, 'TestSuite/Reporter');
  138. $appClass = $reporter . 'Reporter';
  139. App::uses($appClass, 'TestSuite/Reporter');
  140. if (!class_exists($appClass)) {
  141. $object = new $coreClass(null, $this->_params);
  142. } else {
  143. $object = new $appClass(null, $this->_params);
  144. }
  145. return $this->arguments['printer'] = $object;
  146. }
  147. }