Last active
December 19, 2015 21:18
-
-
Save overnin/6018749 to your computer and use it in GitHub Desktop.
How a Test of a Component using redis should look like
This file contains hidden or 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
| <?php | |
| App::uses('Component', 'Controller'); | |
| class CreditManagerComponent extends Component{ | |
| public $Controller = null; | |
| public $redis = null; | |
| public $redisProgramPrefix = null; | |
| public function initialize(Controller $controller) { | |
| parent::startup($controller); | |
| $this->Controller = $controller; | |
| if (!isset($this->Controller->redis)) { | |
| throw new InternalErrorException("The CreditManager need a redis instance from his controller."); | |
| } | |
| $this->redis = $this->Controller->redis; | |
| $this->redisProgramPrefix = $this->Controller->redisProgramPrefix; | |
| } | |
| protected function getStatusKey($programDatabase) | |
| { | |
| return $this->redisProgramPrefix . ":" . $programDatabase . ":creditmanager:status"; | |
| } | |
| protected function getCountKey($programDatabase) | |
| { | |
| return $this->redisProgramPrefix . ":" . $programDatabase . ":creditmanager:count"; | |
| } | |
| public function getCount($programDatabase) | |
| { | |
| $countKey = $this->getCountKey($programDatabase); | |
| $count = $this->redis->get($countKey); | |
| if ($count == null || !isset($count)) { | |
| return null; | |
| } | |
| return (int)$count; | |
| } | |
| public function getStatus($programDatabase) | |
| { | |
| $statusKey = $this->getStatusKey($programDatabase); | |
| $statusRaw = $this->redis->get($statusKey); | |
| if ($statusRaw == null || !isset($statusRaw)) { | |
| return null; | |
| } | |
| return (array)json_decode($statusRaw); | |
| } | |
| } |
This file contains hidden or 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
| <?php | |
| App::uses('Controller', 'Controller'); | |
| App::uses('CakeRequest', 'Network'); | |
| App::uses('CakeResponse', 'Network'); | |
| App::uses('ComponentCollection', 'Controller'); | |
| App::uses('CreditManagerComponent', 'Controller/Component'); | |
| class TestCreditManagerController extends Controller { | |
| var $components = array('CreditManager'); | |
| function constructClasses() | |
| { | |
| $this->redis = new Redis(); | |
| $this->redis->connect('127.0.0.1'); | |
| $this->redisProgramPrefix = 'unittest'; | |
| } | |
| } | |
| class CreditManagerComponentTest extends CakeTestCase { | |
| public $CreditManagerComponent = null; | |
| public $Controller = null; | |
| public function setUp() | |
| { | |
| parent::setUp(); | |
| $Collection = new ComponentCollection(); | |
| $this->CreditManagerComponent = new CreditManagerComponent($Collection); | |
| $CakeRequest = new CakeRequest(); | |
| $CakeResponse = new CakeResponse(); | |
| $this->Controller = new TestCreditManagerController($CakeRequest, $CakeResponse); | |
| // Don't get why but me need to manually call the constructClasses function | |
| $this->Controller->constructClasses(); | |
| // We also need to call the callback | |
| $this->CreditManagerComponent->initialize($this->Controller); | |
| // Retrive the redis object from the controller | |
| $this->redis = $this->Controller->redis; | |
| } | |
| public function tearDown() | |
| { | |
| $keys = $this->redis->keys('unittest:*'); | |
| foreach ($keys as $key) { | |
| $this->redis->delete($key); | |
| } | |
| } | |
| public function mkStatus($status) { | |
| return array( | |
| 'object-type' => 'credit-status', | |
| 'model-version' => '1', | |
| 'since' => '2013-01-01T10:10:10', | |
| 'status' => $status); | |
| } | |
| public function testGetStatus() | |
| { | |
| $status = $this->mkStatus('ok'); | |
| $key = "unittest:programdatabase:creditmanager:status"; | |
| $this->redis->set($key, json_encode($status)); | |
| $this->assertEqual( | |
| $status, | |
| $this->CreditManagerComponent->getStatus('programdatabase')); | |
| } | |
| public function testGetStatus_fail() | |
| { | |
| $this->assertEqual( | |
| null, | |
| $this->CreditManagerComponent->getStatus('programdatabase')); | |
| } | |
| public function testGetCount() | |
| { | |
| $key = "unittest:programdatabase:creditmanager:count"; | |
| $this->redis->set($key, 10); | |
| $this->assertEqual( | |
| 10, | |
| $this->CreditManagerComponent->getCount('programdatabase')); | |
| } | |
| public function testGetCount_fail() | |
| { | |
| $this->assertEqual( | |
| null, | |
| $this->CreditManagerComponent->getCount('programdatabase')); | |
| } | |
| } |
MongoCursor::$timeout = -1;
$cursor = $this->dbReference->dbName->find($query);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MongoCursor::$timeout = -1;
$cursor = $this->dbReference->dbName->find($query);