Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

136 Zeilen
4.2KB

  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. $config = $config === null ? $this->appConfig : $config;
  98. if (is_string($config)) {
  99. $configFile = Yii::getAlias($config);
  100. if (!is_file($configFile)) {
  101. throw new InvalidConfigException("The application configuration file does not exist: $config");
  102. }
  103. $config = require($configFile);
  104. }
  105. if (is_array($config)) {
  106. if (!isset($config['class'])) {
  107. $config['class'] = 'yii\web\Application';
  108. }
  109. Yii::$container = new Container();
  110. return Yii::createObject($config);
  111. } else {
  112. throw new InvalidConfigException('Please provide a configuration array to mock up an application.');
  113. }
  114. }
  115. /**
  116. * Destroys the application instance created by [[mockApplication]].
  117. */
  118. protected function destroyApplication()
  119. {
  120. Yii::$app = null;
  121. Yii::$container = new Container();
  122. }
  123. }