Guest User

Untitled

a guest
Dec 10th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. <?php
  2. namespace Codeception\Module;
  3.  
  4. use Cake\TestSuite\Fixture\FixtureManager;
  5. use Codeception\Module;
  6. use Codeception\Test\Cest;
  7. use Codeception\TestInterface;
  8. use Exception;
  9. use stdClass;
  10.  
  11. /**
  12. * CakePHP Fixture Module
  13. *
  14. * @see Cake\TestSuite\Fixture\FixtureInjector
  15. */
  16. class CakeFixture extends Module
  17. {
  18.  
  19. /**
  20. * @var array
  21. */
  22. protected $config = [
  23. // pass to FixtureManager's debug option
  24. 'debug' => false,
  25. // default $autoFixtures property
  26. 'autoFixtures' => true,
  27. // default $dropTables property
  28. 'dropTables' => true,
  29. ];
  30.  
  31. /**
  32. * The instance of the fixture manager to use
  33. *
  34. * @var FixtureManager
  35. */
  36. protected $fixtureManager;
  37.  
  38. /**
  39. * Current TestCase
  40. *
  41. * @var stdClass
  42. */
  43. protected $testCase;
  44.  
  45. /**
  46. * Load FixtureManager
  47. */
  48. public function _initialize()
  49. {
  50. $manager = new FixtureManager();
  51. $manager->setDebug($this->_getConfig('debug'));
  52.  
  53. $this->fixtureManager = $manager;
  54. $this->fixtureManager->shutDown();
  55.  
  56. $this->debugSection('Fixture', 'Initialized FixtureManager, debug=' . (int)$this->_getConfig('debug'));
  57. }
  58.  
  59. /**
  60. * Destroys the fixtures created by the fixture manager at the end of
  61. * the test suite run
  62. */
  63. // @codingStandardsIgnoreStart
  64. public function _afterSuite()// @codingStandardsIgnoreEnd
  65. {
  66. $this->testCase = null;
  67. $this->fixtureManager->shutDown();
  68. $this->debugSection('Fixture', 'FixtureManager shutDown');
  69. }
  70.  
  71. /**
  72. * Adds fixtures to a test case when it starts.
  73. *
  74. * @param TestInterface $test The test case
  75. * @return void
  76. */
  77. // @codingStandardsIgnoreStart
  78. public function _before(TestInterface $test)// @codingStandardsIgnoreEnd
  79. {
  80. if ($this->hasFixtures($test)) {
  81. $this->debugSection('Fixture', 'Test class is: ' . get_class($test->getTestClass()));
  82. $this->testCase = $this->setRequireProperties($test->getTestClass());
  83. $this->fixtureManager->fixturize($this->testCase);
  84.  
  85. $this->debugSection('Fixture', 'Load fixtures: ' . implode(', ', $this->testCase->fixtures));
  86. $this->fixtureManager->load($this->testCase);
  87. }
  88. }
  89.  
  90. /**
  91. * Unloads fixtures from the test case.
  92. *
  93. * @param TestInterface $test The test case
  94. * @return void
  95. */
  96. // @codingStandardsIgnoreStart
  97. public function _after(TestInterface $test)// @codingStandardsIgnoreEnd
  98. {
  99. if ($this->hasFixtures($test)) {
  100. $this->debugSection('Fixture', 'Unload fixtures: ' . implode(', ', $test->getTestClass()->fixtures));
  101. $this->fixtureManager->unload($test->getTestClass());
  102. }
  103.  
  104. $this->testCase = null;
  105. }
  106.  
  107. /**
  108. * Chooses which fixtures to load for a given test
  109. *
  110. * Each parameter is a model name that corresponds to a fixture, i.e. 'Posts', 'Authors', etc.
  111. * Passing no parameters will cause all fixtures on the test case to load.
  112. *
  113. * @return void
  114. * @see Cake\TestSuite\TestCase::loadFixtures()
  115. * @throws Exception when no fixture manager is available.
  116. */
  117. public function loadFixtures()
  118. {
  119. $args = func_get_args();
  120. foreach ($args as $class) {
  121. $this->fixtureManager->loadSingle($class, null, $this->testCase->dropTables);
  122. }
  123.  
  124. if (empty($args)) {
  125. $autoFixtures = $this->testCase->autoFixtures;
  126. $this->testCase->autoFixtures = true;
  127. $this->fixtureManager->load($this);
  128. $this->testCase->autoFixtures = $autoFixtures;
  129. }
  130. }
  131.  
  132. /**
  133. * check test class has $fixtures
  134. *
  135. * @param Cest $test
  136. * @return bool
  137. */
  138. private function hasFixtures($test)
  139. {
  140. return $test instanceof Cest && property_exists($test->getTestClass(), 'fixtures');
  141. }
  142.  
  143. /**
  144. * set required properties to test class
  145. *
  146. * @param stdClass $testClass
  147. * @return stdClass
  148. */
  149. private function setRequireProperties($testClass)
  150. {
  151. if (!property_exists($testClass, 'autoFixtures')) {
  152. $testClass->autoFixtures = $this->_getConfig('autoFixtures');
  153. }
  154. if (!property_exists($testClass, 'dropTables')) {
  155. $testClass->dropTables = $this->_getConfig('dropTables');
  156. }
  157.  
  158. return $testClass;
  159. }
  160. }
Add Comment
Please, Sign In to add comment