TestCase.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 yiiunit\faker;
  8. use yii\di\Container;
  9. use yii\helpers\ArrayHelper;
  10. use Yii;
  11. /**
  12. * This is the base class for all yii framework unit tests.
  13. */
  14. abstract class TestCase extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * Clean up after test.
  18. * By default the application created with [[mockApplication]] will be destroyed.
  19. */
  20. protected function tearDown()
  21. {
  22. parent::tearDown();
  23. $this->destroyApplication();
  24. }
  25. /**
  26. * Populates Yii::$app with a new application
  27. * The application will be destroyed on tearDown() automatically.
  28. * @param array $config The application configuration, if needed
  29. * @param string $appClass name of the application class to create
  30. */
  31. protected function mockApplication($config = [], $appClass = '\yii\console\Application')
  32. {
  33. new $appClass(ArrayHelper::merge([
  34. 'id' => 'testapp',
  35. 'basePath' => __DIR__,
  36. 'vendorPath' => dirname(__DIR__) . '/vendor',
  37. ], $config));
  38. }
  39. protected function mockWebApplication($config = [], $appClass = '\yii\web\Application')
  40. {
  41. new $appClass(ArrayHelper::merge([
  42. 'id' => 'testapp',
  43. 'basePath' => __DIR__,
  44. 'vendorPath' => dirname(__DIR__) . '/vendor',
  45. 'components' => [
  46. 'request' => [
  47. 'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
  48. 'scriptFile' => __DIR__ .'/index.php',
  49. 'scriptUrl' => '/index.php',
  50. ],
  51. ]
  52. ], $config));
  53. }
  54. /**
  55. * Destroys application in Yii::$app by setting it to null.
  56. */
  57. protected function destroyApplication()
  58. {
  59. Yii::$app = null;
  60. Yii::$container = new Container();
  61. }
  62. }