|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
-
-
- namespace yii\codeception;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use Codeception\TestCase\Test;
- use yii\base\UnknownMethodException;
- use yii\base\UnknownPropertyException;
- use yii\di\Container;
- use yii\test\ActiveFixture;
- use yii\test\BaseActiveFixture;
- use yii\test\FixtureTrait;
-
-
- class TestCase extends Test
- {
- use FixtureTrait;
-
-
-
- public $appConfig = '@tests/codeception/config/unit.php';
-
-
-
-
- public function __get($name)
- {
- $fixture = $this->getFixture($name);
- if ($fixture !== null) {
- return $fixture;
- } else {
- throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
- }
- }
-
-
-
- public function __call($name, $params)
- {
- $fixture = $this->getFixture($name);
- if ($fixture instanceof BaseActiveFixture) {
- return $fixture->getModel(reset($params));
- } else {
- throw new UnknownMethodException('Unknown method: ' . get_class($this) . "::$name()");
- }
- }
-
-
-
- protected function setUp()
- {
- parent::setUp();
- $this->mockApplication();
- $this->unloadFixtures();
- $this->loadFixtures();
- }
-
-
-
- protected function tearDown()
- {
- $this->destroyApplication();
- parent::tearDown();
- }
-
-
-
- protected function mockApplication($config = null)
- {
- if (isset(Yii::$app)) {
- return;
- }
- Yii::$container = new Container();
- $config = $config === null ? $this->appConfig : $config;
- if (is_string($config)) {
- $configFile = Yii::getAlias($config);
- if (!is_file($configFile)) {
- throw new InvalidConfigException("The application configuration file does not exist: $config");
- }
- $config = require($configFile);
- }
- if (is_array($config)) {
- if (!isset($config['class'])) {
- $config['class'] = 'yii\web\Application';
- }
- return Yii::createObject($config);
- } else {
- throw new InvalidConfigException('Please provide a configuration array to mock up an application.');
- }
- }
-
-
-
- protected function destroyApplication()
- {
- if (\Yii::$app) {
- if (\Yii::$app->has('session', true)) {
- \Yii::$app->session->close();
- }
- if (\Yii::$app->has('db', true)) {
- Yii::$app->db->close();
- }
- }
- Yii::$app = null;
- Yii::$container = new Container();
- }
- }
|