FixtureTrait.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://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. * The return value of this method must be an array of fixture configurations. For example,
  36. *
  37. * ```php
  38. * [
  39. * // anonymous fixture
  40. * PostFixture::className(),
  41. * // "users" fixture
  42. * 'users' => UserFixture::className(),
  43. * // "cache" fixture with configuration
  44. * 'cache' => [
  45. * 'class' => CacheFixture::className(),
  46. * 'host' => 'xxx',
  47. * ],
  48. * ]
  49. * ```
  50. *
  51. * Note that the actual fixtures used for a test case will include both [[globalFixtures()]]
  52. * and [[fixtures()]].
  53. *
  54. * @return array the fixtures needed by the current test case
  55. */
  56. public function fixtures()
  57. {
  58. return [];
  59. }
  60. /**
  61. * Declares the fixtures shared required by different test cases.
  62. * The return value should be similar to that of [[fixtures()]].
  63. * You should usually override this method in a base class.
  64. * @return array the fixtures shared and required by different test cases.
  65. * @see fixtures()
  66. */
  67. public function globalFixtures()
  68. {
  69. return [];
  70. }
  71. /**
  72. * Loads the specified fixtures.
  73. * This method will call [[Fixture::load()]] for every fixture object.
  74. * @param Fixture[] $fixtures the fixtures to be loaded. If this parameter is not specified,
  75. * the return value of [[getFixtures()]] will be used.
  76. */
  77. public function loadFixtures($fixtures = null)
  78. {
  79. if ($fixtures === null) {
  80. $fixtures = $this->getFixtures();
  81. }
  82. /* @var $fixture Fixture */
  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[] $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. /* @var $fixture Fixture */
  105. foreach ($fixtures as $fixture) {
  106. $fixture->beforeUnload();
  107. }
  108. $fixtures = array_reverse($fixtures);
  109. foreach ($fixtures as $fixture) {
  110. $fixture->unload();
  111. }
  112. foreach ($fixtures as $fixture) {
  113. $fixture->afterUnload();
  114. }
  115. }
  116. /**
  117. * Initialize the fixtures
  118. * @since 2.0.12
  119. */
  120. public function initFixtures()
  121. {
  122. $this->unloadFixtures();
  123. $this->loadFixtures();
  124. }
  125. /**
  126. * Returns the fixture objects as specified in [[globalFixtures()]] and [[fixtures()]].
  127. * @return Fixture[] the loaded fixtures for the current test case
  128. */
  129. public function getFixtures()
  130. {
  131. if ($this->_fixtures === null) {
  132. $this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
  133. }
  134. return $this->_fixtures;
  135. }
  136. /**
  137. * Returns the named fixture.
  138. * @param string $name the fixture name. This can be either the fixture alias name, or the class name if the alias is not used.
  139. * @return Fixture the fixture object, or null if the named fixture does not exist.
  140. */
  141. public function getFixture($name)
  142. {
  143. if ($this->_fixtures === null) {
  144. $this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
  145. }
  146. $name = ltrim($name, '\\');
  147. return isset($this->_fixtures[$name]) ? $this->_fixtures[$name] : null;
  148. }
  149. /**
  150. * Creates the specified fixture instances.
  151. * All dependent fixtures will also be created.
  152. * @param array $fixtures the fixtures to be created. You may provide fixture names or fixture configurations.
  153. * If this parameter is not provided, the fixtures specified in [[globalFixtures()]] and [[fixtures()]] will be created.
  154. * @return Fixture[] the created fixture instances
  155. * @throws InvalidConfigException if fixtures are not properly configured or if a circular dependency among
  156. * the fixtures is detected.
  157. */
  158. protected function createFixtures(array $fixtures)
  159. {
  160. // normalize fixture configurations
  161. $config = []; // configuration provided in test case
  162. $aliases = []; // class name => alias or class name
  163. foreach ($fixtures as $name => $fixture) {
  164. if (!is_array($fixture)) {
  165. $class = ltrim($fixture, '\\');
  166. $fixtures[$name] = ['class' => $class];
  167. $aliases[$class] = is_int($name) ? $class : $name;
  168. } elseif (isset($fixture['class'])) {
  169. $class = ltrim($fixture['class'], '\\');
  170. $config[$class] = $fixture;
  171. $aliases[$class] = $name;
  172. } else {
  173. throw new InvalidConfigException("You must specify 'class' for the fixture '$name'.");
  174. }
  175. }
  176. // create fixture instances
  177. $instances = [];
  178. $stack = array_reverse($fixtures);
  179. while (($fixture = array_pop($stack)) !== null) {
  180. if ($fixture instanceof Fixture) {
  181. $class = get_class($fixture);
  182. $name = isset($aliases[$class]) ? $aliases[$class] : $class;
  183. unset($instances[$name]); // unset so that the fixture is added to the last in the next line
  184. $instances[$name] = $fixture;
  185. } else {
  186. $class = ltrim($fixture['class'], '\\');
  187. $name = isset($aliases[$class]) ? $aliases[$class] : $class;
  188. if (!isset($instances[$name])) {
  189. $instances[$name] = false;
  190. $stack[] = $fixture = Yii::createObject($fixture);
  191. foreach ($fixture->depends as $dep) {
  192. // need to use the configuration provided in test case
  193. $stack[] = isset($config[$dep]) ? $config[$dep] : ['class' => $dep];
  194. }
  195. } elseif ($instances[$name] === false) {
  196. throw new InvalidConfigException("A circular dependency is detected for fixture '$class'.");
  197. }
  198. }
  199. }
  200. return $instances;
  201. }
  202. }