TestCase.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace dcb9\redis\tests;
  3. use yii\di\Container;
  4. use yii\helpers\ArrayHelper;
  5. use Yii;
  6. use dcb9\redis\Connection;
  7. /**
  8. * This is the base class for all yii framework unit tests.
  9. */
  10. abstract class TestCase extends \PHPUnit_Framework_TestCase
  11. {
  12. public static $params;
  13. /**
  14. * Returns a test configuration param from config.php or config-local.php
  15. * @return array the value of the configuration param
  16. */
  17. public static function getParam()
  18. {
  19. if (static::$params === null) {
  20. if (file_exists(__DIR__ . '/config-local.php')) {
  21. static::$params = include(__DIR__ . '/config-local.php');
  22. } else {
  23. static::$params = include(__DIR__ . '/config.php');
  24. }
  25. }
  26. return static::$params;
  27. }
  28. /**
  29. * Clean up after test.
  30. * By default the application created with [[mockApplication]] will be destroyed.
  31. */
  32. protected function tearDown()
  33. {
  34. parent::tearDown();
  35. $this->destroyApplication();
  36. }
  37. /**
  38. * Populates Yii::$app with a new application
  39. * The application will be destroyed on tearDown() automatically.
  40. * @param array $config The application configuration, if needed
  41. * @param string $appClass name of the application class to create
  42. */
  43. protected function mockApplication($config = [], $appClass = '\yii\console\Application')
  44. {
  45. new $appClass(ArrayHelper::merge([
  46. 'id' => 'testapp',
  47. 'basePath' => __DIR__,
  48. 'vendorPath' => dirname(__DIR__) . '/vendor',
  49. ], $config));
  50. }
  51. protected function mockWebApplication($config = [], $appClass = '\yii\web\Application')
  52. {
  53. new $appClass(ArrayHelper::merge([
  54. 'id' => 'testapp',
  55. 'basePath' => __DIR__,
  56. 'vendorPath' => dirname(__DIR__) . '/vendor',
  57. 'components' => [
  58. 'request' => [
  59. 'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
  60. 'scriptFile' => __DIR__ . '/index.php',
  61. 'scriptUrl' => '/index.php',
  62. ],
  63. ]
  64. ], $config));
  65. }
  66. /**
  67. * Destroys application in Yii::$app by setting it to null.
  68. */
  69. protected function destroyApplication()
  70. {
  71. Yii::$app = null;
  72. Yii::$container = new Container();
  73. }
  74. protected function setUp()
  75. {
  76. $params = self::getParam();
  77. if ($params === null) {
  78. $this->markTestSkipped('No redis server connection configured.');
  79. }
  80. $connection = new Connection($params);
  81. $this->mockApplication(['components' => ['redis' => $connection]]);
  82. parent::setUp();
  83. }
  84. /**
  85. * @param boolean $reset whether to clean up the test database
  86. * @return Connection
  87. */
  88. public function getConnection($reset = true)
  89. {
  90. $params = self::getParam();
  91. $db = new Connection($params);
  92. if ($reset) {
  93. $db->open();
  94. $db->flushdb();
  95. }
  96. return $db;
  97. }
  98. }