FixtureInjector.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\TestSuite\Fixture;
  16. if (class_exists('PHPUnit_Runner_Version')) {
  17. if (version_compare(\PHPUnit_Runner_Version::id(), '5.7', '<')) {
  18. trigger_error(sprintf('Your PHPUnit Version must be at least 5.7.0 to use CakePHP Testsuite, found %s', \PHPUnit_Runner_Version::id()), E_USER_ERROR);
  19. }
  20. class_alias('PHPUnit_Framework_Test', 'PHPUnit\Framework\Test');
  21. class_alias('PHPUnit_Framework_Warning', 'PHPUnit\Framework\Warning');
  22. if (!class_exists('PHPUnit\Framework\TestSuite')) {
  23. class_alias('PHPUnit_Framework_TestSuite', 'PHPUnit\Framework\TestSuite');
  24. }
  25. if (class_exists('PHPUnit_Runner_Version') && !class_exists('PHPUnit\Framework\AssertionFailedError')) {
  26. class_alias('PHPUnit_Framework_AssertionFailedError', 'PHPUnit\Framework\AssertionFailedError');
  27. }
  28. }
  29. use Cake\TestSuite\TestCase;
  30. use Exception;
  31. use PHPUnit\Framework\AssertionFailedError;
  32. use PHPUnit\Framework\Test;
  33. use PHPUnit\Framework\TestListener;
  34. use PHPUnit\Framework\TestSuite;
  35. use PHPUnit\Framework\Warning;
  36. /**
  37. * Test listener used to inject a fixture manager in all tests that
  38. * are composed inside a Test Suite
  39. */
  40. class FixtureInjector implements TestListener
  41. {
  42. /**
  43. * The instance of the fixture manager to use
  44. *
  45. * @var \Cake\TestSuite\Fixture\FixtureManager
  46. */
  47. protected $_fixtureManager;
  48. /**
  49. * Holds a reference to the container test suite
  50. *
  51. * @var \PHPUnit\Framework\TestSuite
  52. */
  53. protected $_first;
  54. /**
  55. * Constructor. Save internally the reference to the passed fixture manager
  56. *
  57. * @param \Cake\TestSuite\Fixture\FixtureManager $manager The fixture manager
  58. */
  59. public function __construct(FixtureManager $manager)
  60. {
  61. if (isset($_SERVER['argv'])) {
  62. $manager->setDebug(in_array('--debug', $_SERVER['argv']));
  63. }
  64. $this->_fixtureManager = $manager;
  65. $this->_fixtureManager->shutDown();
  66. }
  67. /**
  68. * Iterates the tests inside a test suite and creates the required fixtures as
  69. * they were expressed inside each test case.
  70. *
  71. * @param \PHPUnit\Framework\TestSuite $suite The test suite
  72. * @return void
  73. */
  74. public function startTestSuite(TestSuite $suite)
  75. {
  76. if (empty($this->_first)) {
  77. $this->_first = $suite;
  78. }
  79. }
  80. /**
  81. * Destroys the fixtures created by the fixture manager at the end of the test
  82. * suite run
  83. *
  84. * @param \PHPUnit\Framework\TestSuite $suite The test suite
  85. * @return void
  86. */
  87. public function endTestSuite(TestSuite $suite)
  88. {
  89. if ($this->_first === $suite) {
  90. $this->_fixtureManager->shutDown();
  91. }
  92. }
  93. /**
  94. * Not Implemented
  95. *
  96. * @param \PHPUnit\Framework\Test $test The test to add errors from.
  97. * @param \Exception $e The exception
  98. * @param float $time current time
  99. * @return void
  100. */
  101. public function addError(Test $test, Exception $e, $time)
  102. {
  103. }
  104. /**
  105. * Not Implemented
  106. *
  107. * @param \PHPUnit\Framework\Test $test The test to add warnings from.
  108. * @param \PHPUnit\Framework\Warning $e The warning
  109. * @param float $time current time
  110. * @return void
  111. */
  112. public function addWarning(Test $test, Warning $e, $time)
  113. {
  114. }
  115. /**
  116. * Not Implemented
  117. *
  118. * @param \PHPUnit\Framework\Test $test The test case
  119. * @param \PHPUnit\Framework\AssertionFailedError $e The failed assertion
  120. * @param float $time current time
  121. * @return void
  122. */
  123. public function addFailure(Test $test, AssertionFailedError $e, $time)
  124. {
  125. }
  126. /**
  127. * Not Implemented
  128. *
  129. * @param \PHPUnit\Framework\Test $test The test case
  130. * @param \Exception $e The incomplete test error.
  131. * @param float $time current time
  132. * @return void
  133. */
  134. public function addIncompleteTest(Test $test, Exception $e, $time)
  135. {
  136. }
  137. /**
  138. * Not Implemented
  139. *
  140. * @param \PHPUnit\Framework\Test $test The test case
  141. * @param \Exception $e Skipped test exception
  142. * @param float $time current time
  143. * @return void
  144. */
  145. public function addSkippedTest(Test $test, Exception $e, $time)
  146. {
  147. }
  148. /**
  149. * Adds fixtures to a test case when it starts.
  150. *
  151. * @param \PHPUnit\Framework\Test $test The test case
  152. * @return void
  153. */
  154. public function startTest(Test $test)
  155. {
  156. $test->fixtureManager = $this->_fixtureManager;
  157. if ($test instanceof TestCase) {
  158. $this->_fixtureManager->fixturize($test);
  159. $this->_fixtureManager->load($test);
  160. }
  161. }
  162. /**
  163. * Unloads fixtures from the test case.
  164. *
  165. * @param \PHPUnit\Framework\Test $test The test case
  166. * @param float $time current time
  167. * @return void
  168. */
  169. public function endTest(Test $test, $time)
  170. {
  171. if ($test instanceof TestCase) {
  172. $this->_fixtureManager->unload($test);
  173. }
  174. }
  175. /**
  176. * Not Implemented
  177. *
  178. * @param \PHPUnit\Framework\Test $test The test case
  179. * @param \Exception $e The exception to track
  180. * @param float $time current time
  181. * @return void
  182. */
  183. public function addRiskyTest(Test $test, Exception $e, $time)
  184. {
  185. }
  186. }