-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
何延龙
committed
May 22, 2015
1 parent
7bc5337
commit 2252c50
Showing
6 changed files
with
282 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<phpunit bootstrap="./tests/bootstrap.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,111 @@ | ||
<?php | ||
|
||
/** | ||
* Created by PhpStorm. | ||
* User: heyanlong | ||
* Date: 2015/5/21 | ||
* Time: 13:59 | ||
*/ | ||
class TestCase extends \PHPUnit_Framework_TestCase | ||
namespace heyanlong\redis\tests; | ||
|
||
use Yii; | ||
use yii\di\Container; | ||
use heyanlong\redis\Connection; | ||
use yii\helpers\ArrayHelper; | ||
|
||
abstract class TestCase extends \PHPUnit_Framework_TestCase | ||
{ | ||
public static $params; | ||
|
||
|
||
/** | ||
* Returns a test configuration param from /data/config.php | ||
* @param string $name params name | ||
* @param mixed $default default value to use when param is not set. | ||
* @return mixed the value of the configuration param | ||
*/ | ||
public static function getParam($name, $default = null) | ||
{ | ||
if (static::$params === null) { | ||
static::$params = require(__DIR__ . '/data/config.php'); | ||
} | ||
|
||
return isset(static::$params[$name]) ? static::$params[$name] : $default; | ||
} | ||
|
||
/** | ||
* Clean up after test. | ||
* By default the application created with [[mockApplication]] will be destroyed. | ||
*/ | ||
protected function tearDown() | ||
{ | ||
parent::tearDown(); | ||
$this->destroyApplication(); | ||
} | ||
|
||
/** | ||
* Populates Yii::$app with a new application | ||
* The application will be destroyed on tearDown() automatically. | ||
* @param array $config The application configuration, if needed | ||
* @param string $appClass name of the application class to create | ||
*/ | ||
protected function mockApplication($config = [], $appClass = '\yii\console\Application') | ||
{ | ||
new $appClass(ArrayHelper::merge([ | ||
'id' => 'testapp', | ||
'basePath' => __DIR__, | ||
'vendorPath' => dirname(__DIR__) . '/vendor', | ||
], $config)); | ||
} | ||
|
||
protected function mockWebApplication($config = [], $appClass = '\yii\web\Application') | ||
{ | ||
new $appClass(ArrayHelper::merge([ | ||
'id' => 'testapp', | ||
'basePath' => __DIR__, | ||
'vendorPath' => dirname(__DIR__) . '/vendor', | ||
'components' => [ | ||
'request' => [ | ||
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq', | ||
'scriptFile' => __DIR__ . '/index.php', | ||
'scriptUrl' => '/index.php', | ||
], | ||
] | ||
], $config)); | ||
} | ||
|
||
/** | ||
* Destroys application in Yii::$app by setting it to null. | ||
*/ | ||
protected function destroyApplication() | ||
{ | ||
Yii::$app = null; | ||
Yii::$container = new Container(); | ||
} | ||
|
||
protected function setUp() | ||
{ | ||
$databases = self::getParam('databases'); | ||
$params = isset($databases['redis']) ? $databases['redis'] : null; | ||
if ($params === null) { | ||
$this->markTestSkipped('No redis server connection configured.'); | ||
} | ||
|
||
$connection = new Connection($params); | ||
|
||
$this->mockApplication(['components' => ['redis' => $connection]]); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* @param boolean $reset whether to clean up the test database | ||
* @return Connection | ||
*/ | ||
public function getConnection($reset = true) | ||
{ | ||
$databases = self::getParam('databases'); | ||
$params = isset($databases['redis']) ? $databases['redis'] : []; | ||
$db = new Connection($params); | ||
if ($reset) { | ||
$db->open(); | ||
$db->flushdb(); | ||
} | ||
|
||
return $db; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
error_reporting(-1); | ||
|
||
define('YII_ENABLE_ERROR_HANDLER', false); | ||
define('YII_DEBUG', true); | ||
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__; | ||
$_SERVER['SCRIPT_FILENAME'] = __FILE__; | ||
|
||
require_once(__DIR__ . '/../../vendor/autoload.php'); | ||
require_once(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php'); | ||
require_once(__DIR__ . '/TestCase.php'); | ||
require_once(__DIR__ . '/../Connection.php'); | ||
|
||
|
||
Yii::setAlias('@yiiunit/extensions/redis', __DIR__); | ||
Yii::setAlias('@yii/redis', dirname(__DIR__)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/** | ||
* This is the configuration file for the Yii2 unit tests. | ||
* You can override configuration values by creating a `config.local.php` file | ||
* and manipulate the `$config` variable. | ||
* For example to change MySQL username and password your `config.local.php` should | ||
* contain the following: | ||
* | ||
*/ | ||
|
||
$config = [ | ||
'databases' => [ | ||
'redis' => [ | ||
'master' => [ | ||
// '10.155.20.167:6391', | ||
// '10.155.20.168:6379', | ||
// '10.155.20.167:6380', | ||
// '10.155.20.169:6379', | ||
'localhost:6379', | ||
], | ||
'slave' => [ | ||
// '10.155.20.167:6379', | ||
// '10.155.20.168:6380', | ||
// '10.155.20.168:6391', | ||
// '10.155.20.169:6380', | ||
'localhost:6379', | ||
], | ||
'database' => 0, | ||
'password' => null, | ||
], | ||
], | ||
]; | ||
|
||
return $config; |