FixtureTrait.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. *
  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::className(),
  42. * // "users" fixture
  43. * 'users' => UserFixture::className(),
  44. * // "cache" fixture with configuration
  45. * 'cache' => [
  46. * 'class' => CacheFixture::className(),
  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[] $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. /* @var $fixture Fixture */
  84. foreach ($fixtures as $fixture) {
  85. $fixture->beforeLoad();
  86. }
  87. foreach ($fixtures as $fixture) {
  88. $fixture->load();
  89. }
  90. foreach (array_reverse($fixtures) as $fixture) {
  91. $fixture->afterLoad();
  92. }
  93. }
  94. /**
  95. * Unloads the specified fixtures.
  96. * This method will call [[Fixture::unload()]] for every fixture object.
  97. * @param Fixture[] $fixtures the fixtures to be loaded. If this parameter is not specified,
  98. * the return value of [[getFixtures()]] will be used.
  99. */
  100. public function unloadFixtures($fixtures = null)
  101. {
  102. if ($fixtures === null) {
  103. $fixtures = $this->getFixtures();
  104. }
  105. /* @var $fixture Fixture */
  106. foreach ($fixtures as $fixture) {
  107. $fixture->beforeUnload();
  108. }
  109. $fixtures = array_reverse($fixtures);
  110. foreach ($fixtures as $fixture) {
  111. $fixture->unload();
  112. }
  113. foreach ($fixtures as $fixture) {
  114. $fixture->afterUnload();
  115. }
  116. }
  117. /**
  118. * Initialize the fixtures.
  119. * @since 2.0.12
  120. */
  121. public function initFixtures()
  122. {
  123. $this->unloadFixtures();
  124. $this->loadFixtures();
  125. }
  126. /**
  127. * Returns the fixture objects as specified in [[globalFixtures()]] and [[fixtures()]].
  128. * @return Fixture[] the loaded fixtures for the current test case
  129. */
  130. public function getFixtures()
  131. {
  132. if ($this->_fixtures === null) {
  133. $this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
  134. }
  135. return $this->_fixtures;
  136. }
  137. /**
  138. * Returns the named fixture.
  139. * @param string $name the fixture name. This can be either the fixture alias name, or the class name if the alias is not used.
  140. * @return Fixture the fixture object, or null if the named fixture does not exist.
  141. */
  142. public function getFixture($name)
  143. {
  144. if ($this->_fixtures === null) {
  145. $this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
  146. }
  147. $name = ltrim($name, '\\');
  148. return isset($this->_fixtures[$name]) ? $this->_fixtures[$name] : null;
  149. }
  150. /**
  151. * Creates the specified fixture instances.
  152. * All dependent fixtures will also be created.
  153. * @param array $fixtures the fixtures to be created. You may provide fixture names or fixture configurations.
  154. * If this parameter is not provided, the fixtures specified in [[globalFixtures()]] and [[fixtures()]] will be created.
  155. * @return Fixture[] the created fixture instances
  156. * @throws InvalidConfigException if fixtures are not properly configured or if a circular dependency among
  157. * the fixtures is detected.
  158. */
  159. protected function createFixtures(array $fixtures)
  160. {
  161. // normalize fixture configurations
  162. $config = []; // configuration provided in test case
  163. $aliases = []; // class name => alias or class name
  164. foreach ($fixtures as $name => $fixture) {
  165. if (!is_array($fixture)) {
  166. $class = ltrim($fixture, '\\');
  167. $fixtures[$name] = ['class' => $class];
  168. $aliases[$class] = is_int($name) ? $class : $name;
  169. } elseif (isset($fixture['class'])) {
  170. $class = ltrim($fixture['class'], '\\');
  171. $config[$class] = $fixture;
  172. $aliases[$class] = $name;
  173. } else {
  174. throw new InvalidConfigException("You must specify 'class' for the fixture '$name'.");
  175. }
  176. }
  177. // create fixture instances
  178. $instances = [];
  179. $stack = array_reverse($fixtures);
  180. while (($fixture = array_pop($stack)) !== null) {
  181. if ($fixture instanceof Fixture) {
  182. $class = get_class($fixture);
  183. $name = isset($aliases[$class]) ? $aliases[$class] : $class;
  184. unset($instances[$name]); // unset so that the fixture is added to the last in the next line
  185. $instances[$name] = $fixture;
  186. } else {
  187. $class = ltrim($fixture['class'], '\\');
  188. $name = isset($aliases[$class]) ? $aliases[$class] : $class;
  189. if (!isset($instances[$name])) {
  190. $instances[$name] = false;
  191. $stack[] = $fixture = Yii::createObject($fixture);
  192. foreach ($fixture->depends as $dep) {
  193. // need to use the configuration provided in test case
  194. $stack[] = isset($config[$dep]) ? $config[$dep] : ['class' => $dep];
  195. }
  196. } elseif ($instances[$name] === false) {
  197. throw new InvalidConfigException("A circular dependency is detected for fixture '$class'.");
  198. }
  199. }
  200. }
  201. return $instances;
  202. }
  203. }