CakeTestSuiteCommand.php 4.5 KB

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