FixtureTrait.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. * Returns the fixture objects as specified in [[globalFixtures()]] and [[fixtures()]].
  118. * @return Fixture[] the loaded fixtures for the current test case
  119. */
  120. public function getFixtures()
  121. {
  122. if ($this->_fixtures === null) {
  123. $this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
  124. }
  125. return $this->_fixtures;
  126. }
  127. /**
  128. * Returns the named fixture.
  129. * @param string $name the fixture name. This can be either the fixture alias name, or the class name if the alias is not used.
  130. * @return Fixture the fixture object, or null if the named fixture does not exist.
  131. */
  132. public function getFixture($name)
  133. {
  134. if ($this->_fixtures === null) {
  135. $this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
  136. }
  137. $name = ltrim($name, '\\');
  138. return isset($this->_fixtures[$name]) ? $this->_fixtures[$name] : null;
  139. }
  140. /**
  141. * Creates the specified fixture instances.
  142. * All dependent fixtures will also be created.
  143. * @param array $fixtures the fixtures to be created. You may provide fixture names or fixture configurations.
  144. * If this parameter is not provided, the fixtures specified in [[globalFixtures()]] and [[fixtures()]] will be created.
  145. * @return Fixture[] the created fixture instances
  146. * @throws InvalidConfigException if fixtures are not properly configured or if a circular dependency among
  147. * the fixtures is detected.
  148. */
  149. protected function createFixtures(array $fixtures)
  150. {
  151. // normalize fixture configurations
  152. $config = []; // configuration provided in test case
  153. $aliases = []; // class name => alias or class name
  154. foreach ($fixtures as $name => $fixture) {
  155. if (!is_array($fixture)) {
  156. $class = ltrim($fixture, '\\');
  157. $fixtures[$name] = ['class' => $class];
  158. $aliases[$class] = is_int($name) ? $class : $name;
  159. } elseif (isset($fixture['class'])) {
  160. $class = ltrim($fixture['class'], '\\');
  161. $config[$class] = $fixture;
  162. $aliases[$class] = $name;
  163. } else {
  164. throw new InvalidConfigException("You must specify 'class' for the fixture '$name'.");
  165. }
  166. }
  167. // create fixture instances
  168. $instances = [];
  169. $stack = array_reverse($fixtures);
  170. while (($fixture = array_pop($stack)) !== null) {
  171. if ($fixture instanceof Fixture) {
  172. $class = get_class($fixture);
  173. $name = isset($aliases[$class]) ? $aliases[$class] : $class;
  174. unset($instances[$name]); // unset so that the fixture is added to the last in the next line
  175. $instances[$name] = $fixture;
  176. } else {
  177. $class = ltrim($fixture['class'], '\\');
  178. $name = isset($aliases[$class]) ? $aliases[$class] : $class;
  179. if (!isset($instances[$name])) {
  180. $instances[$name] = false;
  181. $stack[] = $fixture = Yii::createObject($fixture);
  182. foreach ($fixture->depends as $dep) {
  183. // need to use the configuration provided in test case
  184. $stack[] = isset($config[$dep]) ? $config[$dep] : ['class' => $dep];
  185. }
  186. } elseif ($instances[$name] === false) {
  187. throw new InvalidConfigException("A circular dependency is detected for fixture '$class'.");
  188. }
  189. }
  190. }
  191. return $instances;
  192. }
  193. }