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