您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\codeception;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use Codeception\TestCase\Test;
  11. use yii\base\UnknownMethodException;
  12. use yii\base\UnknownPropertyException;
  13. use yii\di\Container;
  14. use yii\test\ActiveFixture;
  15. use yii\test\BaseActiveFixture;
  16. use yii\test\FixtureTrait;
  17. /**
  18. * TestCase is the base class for all codeception unit tests
  19. *
  20. * @author Mark Jebri <mark.github@yandex.ru>
  21. * @since 2.0
  22. */
  23. class TestCase extends Test
  24. {
  25. use FixtureTrait;
  26. /**
  27. * @var array|string the application configuration that will be used for creating an application instance for each test.
  28. * You can use a string to represent the file path or path alias of a configuration file.
  29. * The application configuration array may contain an optional `class` element which specifies the class
  30. * name of the application instance to be created. By default, a [[\yii\web\Application]] instance will be created.
  31. */
  32. public $appConfig = '@tests/codeception/config/unit.php';
  33. /**
  34. * Returns the value of an object property.
  35. *
  36. * Do not call this method directly as it is a PHP magic method that
  37. * will be implicitly called when executing `$value = $object->property;`.
  38. * @param string $name the property name
  39. * @return mixed the property value
  40. * @throws UnknownPropertyException if the property is not defined
  41. */
  42. public function __get($name)
  43. {
  44. $fixture = $this->getFixture($name);
  45. if ($fixture !== null) {
  46. return $fixture;
  47. } else {
  48. throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
  49. }
  50. }
  51. /**
  52. * Calls the named method which is not a class method.
  53. *
  54. * Do not call this method directly as it is a PHP magic method that
  55. * will be implicitly called when an unknown method is being invoked.
  56. * @param string $name the method name
  57. * @param array $params method parameters
  58. * @throws UnknownMethodException when calling unknown method
  59. * @return mixed the method return value
  60. */
  61. public function __call($name, $params)
  62. {
  63. $fixture = $this->getFixture($name);
  64. if ($fixture instanceof BaseActiveFixture) {
  65. return $fixture->getModel(reset($params));
  66. } else {
  67. throw new UnknownMethodException('Unknown method: ' . get_class($this) . "::$name()");
  68. }
  69. }
  70. /**
  71. * @inheritdoc
  72. */
  73. protected function setUp()
  74. {
  75. parent::setUp();
  76. $this->mockApplication();
  77. $this->unloadFixtures();
  78. $this->loadFixtures();
  79. }
  80. /**
  81. * @inheritdoc
  82. */
  83. protected function tearDown()
  84. {
  85. $this->destroyApplication();
  86. parent::tearDown();
  87. }
  88. /**
  89. * Mocks up the application instance.
  90. * @param array $config the configuration that should be used to generate the application instance.
  91. * If null, [[appConfig]] will be used.
  92. * @return \yii\web\Application|\yii\console\Application the application instance
  93. * @throws InvalidConfigException if the application configuration is invalid
  94. */
  95. protected function mockApplication($config = null)
  96. {
  97. if (isset(Yii::$app)) {
  98. return;
  99. }
  100. Yii::$container = new Container();
  101. $config = $config === null ? $this->appConfig : $config;
  102. if (is_string($config)) {
  103. $configFile = Yii::getAlias($config);
  104. if (!is_file($configFile)) {
  105. throw new InvalidConfigException("The application configuration file does not exist: $config");
  106. }
  107. $config = require($configFile);
  108. }
  109. if (is_array($config)) {
  110. if (!isset($config['class'])) {
  111. $config['class'] = 'yii\web\Application';
  112. }
  113. return Yii::createObject($config);
  114. } else {
  115. throw new InvalidConfigException('Please provide a configuration array to mock up an application.');
  116. }
  117. }
  118. /**
  119. * Destroys the application instance created by [[mockApplication]].
  120. */
  121. protected function destroyApplication()
  122. {
  123. if (\Yii::$app) {
  124. if (\Yii::$app->has('session', true)) {
  125. \Yii::$app->session->close();
  126. }
  127. if (\Yii::$app->has('db', true)) {
  128. Yii::$app->db->close();
  129. }
  130. }
  131. Yii::$app = null;
  132. Yii::$container = new Container();
  133. }
  134. }