TestCase.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace yiiunit\extensions\swiftmailer;
  3. use yii\helpers\ArrayHelper;
  4. abstract class TestCase extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * Clean up after test.
  8. * By default the application created with [[mockApplication]] will be destroyed.
  9. */
  10. protected function tearDown()
  11. {
  12. parent::tearDown();
  13. $this->destroyApplication();
  14. }
  15. /**
  16. * Populates Yii::$app with a new application
  17. * The application will be destroyed on tearDown() automatically.
  18. * @param array $config The application configuration, if needed
  19. * @param string $appClass name of the application class to create
  20. */
  21. protected function mockApplication($config = [], $appClass = '\yii\console\Application')
  22. {
  23. new $appClass(ArrayHelper::merge([
  24. 'id' => 'testapp',
  25. 'basePath' => __DIR__,
  26. 'vendorPath' => $this->getVendorPath(),
  27. ], $config));
  28. }
  29. protected function getVendorPath()
  30. {
  31. $vendor = dirname(dirname(__DIR__)) . '/vendor';
  32. if (!is_dir($vendor)) {
  33. $vendor = dirname(dirname(dirname(dirname(__DIR__))));
  34. }
  35. return $vendor;
  36. }
  37. /**
  38. * Destroys application in Yii::$app by setting it to null.
  39. */
  40. protected function destroyApplication()
  41. {
  42. \Yii::$app = null;
  43. }
  44. }