|
- <?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();
- }
- }
|