FixtureTrait.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * @link https://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license https://www.yiiframework.com/license/
  6. */
  7. namespace yii\test;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. /**
  11. * FixtureTrait provides functionalities for loading, unloading and accessing fixtures for a test case.
  12. *
  13. * By using FixtureTrait, a test class will be able to specify which fixtures to load by overriding
  14. * the [[fixtures()]] method. It can then load and unload the fixtures using [[loadFixtures()]] and [[unloadFixtures()]].
  15. * Once a fixture is loaded, it can be accessed like an object property, thanks to the PHP `__get()` magic method.
  16. * Also, if the fixture is an instance of [[ActiveFixture]], you will be able to access AR models
  17. * through the syntax `$this->fixtureName('model name')`.
  18. *
  19. * For more details and usage information on FixtureTrait, see the [guide article on fixtures](guide:test-fixtures).
  20. *
  21. * @author Qiang Xue <qiang.xue@gmail.com>
  22. * @since 2.0
  23. */
  24. trait FixtureTrait
  25. {
  26. /**
  27. * @var array the list of fixture objects available for the current test.
  28. * The array keys are the corresponding fixture class names.
  29. * The fixtures are listed in their dependency order. That is, fixture A is listed before B
  30. * if B depends on A.
  31. */
  32. private $_fixtures;
  33. /**
  34. * Declares the fixtures that are needed by the current test case.
  35. *
  36. * The return value of this method must be an array of fixture configurations. For example,
  37. *
  38. * ```php
  39. * [
  40. * // anonymous fixture
  41. * PostFixture::class,
  42. * // "users" fixture
  43. * 'users' => UserFixture::class,
  44. * // "cache" fixture with configuration
  45. * 'cache' => [
  46. * 'class' => CacheFixture::class,
  47. * 'host' => 'xxx',
  48. * ],
  49. * ]
  50. * ```
  51. *
  52. * Note that the actual fixtures used for a test case will include both [[globalFixtures()]]
  53. * and [[fixtures()]].
  54. *
  55. * @return array the fixtures needed by the current test case
  56. */
  57. public function fixtures()
  58. {
  59. return [];
  60. }
  61. /**
  62. * Declares the fixtures shared required by different test cases.
  63. * The return value should be similar to that of [[fixtures()]].
  64. * You should usually override this method in a base class.
  65. * @return array the fixtures shared and required by different test cases.
  66. * @see fixtures()
  67. */
  68. public function globalFixtures()
  69. {
  70. return [];
  71. }
  72. /**
  73. * Loads the specified fixtures.
  74. * This method will call [[Fixture::load()]] for every fixture object.
  75. * @param Fixture[]|null $fixtures the fixtures to be loaded. If this parameter is not specified,
  76. * the return value of [[getFixtures()]] will be used.
  77. */
  78. public function loadFixtures($fixtures = null)
  79. {
  80. if ($fixtures === null) {
  81. $fixtures = $this->getFixtures();
  82. }
  83. foreach ($fixtures as $fixture) {
  84. $fixture->beforeLoad();
  85. }
  86. foreach ($fixtures as $fixture) {
  87. $fixture->load();
  88. }
  89. foreach (array_reverse($fixtures) as $fixture) {
  90. $fixture->afterLoad();
  91. }
  92. }
  93. /**
  94. * Unloads the specified fixtures.
  95. * This method will call [[Fixture::unload()]] for every fixture object.
  96. * @param Fixture[]|null $fixtures the fixtures to be loaded. If this parameter is not specified,
  97. * the return value of [[getFixtures()]] will be used.
  98. */
  99. public function unloadFixtures($fixtures = null)
  100. {
  101. if ($fixtures === null) {
  102. $fixtures = $this->getFixtures();
  103. }
  104. foreach ($fixtures as $fixture) {
  105. $fixture->beforeUnload();
  106. }
  107. $fixtures = array_reverse($fixtures);
  108. foreach ($fixtures as $fixture) {
  109. $fixture->unload();
  110. }
  111. foreach ($fixtures as $fixture) {
  112. $fixture->afterUnload();
  113. }
  114. }
  115. /**
  116. * Initialize the fixtures.
  117. * @since 2.0.12
  118. */
  119. public function initFixtures()
  120. {
  121. $this->unloadFixtures();
  122. $this->loadFixtures();
  123. }
  124. /**
  125. * Returns the fixture objects as specified in [[globalFixtures()]] and [[fixtures()]].
  126. * @return Fixture[] the loaded fixtures for the current test case
  127. */
  128. public function getFixtures()
  129. {
  130. if ($this->_fixtures === null) {
  131. $this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
  132. }
  133. return $this->_fixtures;
  134. }
  135. /**
  136. * Returns the named fixture.
  137. * @param string $name the fixture name. This can be either the fixture alias name, or the class name if the alias is not used.
  138. * @return Fixture|null the fixture object, or null if the named fixture does not exist.
  139. */
  140. public function getFixture($name)
  141. {
  142. if ($this->_fixtures === null) {
  143. $this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
  144. }
  145. $name = ltrim($name, '\\');
  146. return isset($this->_fixtures[$name]) ? $this->_fixtures[$name] : null;
  147. }
  148. /**
  149. * Creates the specified fixture instances.
  150. * All dependent fixtures will also be created. Duplicate fixtures and circular dependencies will only be created once.
  151. * @param array $fixtures the fixtures to be created. You may provide fixture names or fixture configurations.
  152. * If this parameter is not provided, the fixtures specified in [[globalFixtures()]] and [[fixtures()]] will be created.
  153. * @return Fixture[] the created fixture instances
  154. * @throws InvalidConfigException if fixtures are not properly configured
  155. */
  156. protected function createFixtures(array $fixtures)
  157. {
  158. // normalize fixture configurations
  159. $config = []; // configuration provided in test case
  160. $aliases = []; // class name => alias or class name
  161. foreach ($fixtures as $name => $fixture) {
  162. if (!is_array($fixture)) {
  163. $class = ltrim($fixture, '\\');
  164. $fixtures[$name] = ['class' => $class];
  165. $aliases[$class] = is_int($name) ? $class : $name;
  166. } elseif (isset($fixture['class'])) {
  167. $class = ltrim($fixture['class'], '\\');
  168. $config[$class] = $fixture;
  169. $aliases[$class] = $name;
  170. } else {
  171. throw new InvalidConfigException("You must specify 'class' for the fixture '$name'.");
  172. }
  173. }
  174. // create fixture instances
  175. $instances = [];
  176. $stack = array_reverse($fixtures);
  177. while (($fixture = array_pop($stack)) !== null) {
  178. if ($fixture instanceof Fixture) {
  179. $class = get_class($fixture);
  180. $name = isset($aliases[$class]) ? $aliases[$class] : $class;
  181. unset($instances[$name]); // unset so that the fixture is added to the last in the next line
  182. $instances[$name] = $fixture;
  183. } else {
  184. $class = ltrim($fixture['class'], '\\');
  185. $name = isset($aliases[$class]) ? $aliases[$class] : $class;
  186. if (!isset($instances[$name])) {
  187. $instances[$name] = false;
  188. $stack[] = $fixture = Yii::createObject($fixture);
  189. foreach ($fixture->depends as $dep) {
  190. // need to use the configuration provided in test case
  191. $stack[] = isset($config[$dep]) ? $config[$dep] : ['class' => $dep];
  192. }
  193. }
  194. // if the fixture is already loaded (ie. a circular dependency or if two fixtures depend on the same fixture) just skip it.
  195. }
  196. }
  197. return $instances;
  198. }
  199. }