Permalink
Browse files

Added flushCache() and tests.

  • Loading branch information...
1 parent e4fcee4 commit d302686d61d5bcab391a8e5797120fbd1d88fbe5 @gabordemooij committed Nov 21, 2012
Showing with 100 additions and 3 deletions.
  1. +1 −1 RedBean/Finder.php
  2. +8 −1 RedBean/QueryWriter/AQueryWriter.php
  3. +91 −1 testing/RedUNIT/Base/Writecache.php
View
@@ -53,7 +53,7 @@ public function __construct(RedBean_ToolBox $toolbox) {
*
* @return array $beans beans
*/
- public function find( $type, $sql=null, $values=array() ) {
+ public function find( $type, $sql=null, $values=array() ) {
if ($sql instanceof RedBean_SQLHelper) list($sql,$values) = $sql->getQuery();
if (!is_array($values)) throw new InvalidArgumentException('Expected array, ' . gettype($values) . ' given.');
return $this->redbean->find($type,array(),array($sql,$values));
@@ -523,7 +523,14 @@ public function getValue(){
* @param boolean $yesNo
*/
public function setUseCache($yesNo) {
+ $this->flushCache();
$this->flagUseCache = (boolean) $yesNo;
}
-
+
+ /**
+ * Flushes the Query Writer Cache.
+ */
+ public function flushCache() {
+ $this->cache = array();
+ }
}
@@ -16,25 +16,115 @@
class RedUNIT_Base_Writecache extends RedUNIT_Base {
/**
+ * What drivers should be loaded for this test pack?
+ */
+ public function getTargetDrivers() {
+ return array('mysql','pgsql','sqlite','CUBRID');
+ }
+
+ /**
* Begin testing.
* This method runs the actual test pack.
*
* @return void
*/
public function run(){
+
+ testpack('Testing WriteCache Query Writer Cache');
R::nuke();
$logger = RedBean_Plugin_QueryLogger::getInstanceAndAttach(R::$adapter);
$book = R::dispense('book')->setAttr('title','ABC');
+ $book->ownPage[] = R::dispense('page');
$id = R::store($book);
+
+ //test load cache -- without
$logger->clear();
$book = R::load('book',$id);
$book = R::load('book',$id);
asrt(count($logger->grep('SELECT')),2);
- R::$writer->setUseCache(true);
+ R::$writer->setUseCache(true); //with cache
$logger->clear();
$book = R::load('book',$id);
$book = R::load('book',$id);
asrt(count($logger->grep('SELECT')),1);
R::$writer->setUseCache(false);
+
+ //test find cache
+ $logger->clear();
+ $book = R::find('book');
+ $book = R::find('book');
+ asrt(count($logger->grep('SELECT')),2);
+ R::$writer->setUseCache(true); //with cache
+ $logger->clear();
+ $book = R::find('book');
+ $book = R::find('book');
+ asrt(count($logger->grep('SELECT')),1);
+ R::$writer->setUseCache(false);
+
+ //test combinations
+ $logger->clear();
+ $book = R::findOne('book',' id = ? ', array($id));
+ $book->ownPage;
+ R::batch('book',array($id));
+ $book = R::findOne('book',' id = ? ', array($id));
+ $book->ownPage;
+ R::batch('book',array($id));
+ asrt(count($logger->grep('SELECT')),6);
+ R::$writer->setUseCache(true); //with cache
+ $logger->clear();
+ $book = R::findOne('book',' id = ? ', array($id));
+ $book->ownPage;
+ R::batch('book',array($id));
+ $book = R::findOne('book',' id = ? ', array($id));
+ $book->ownPage;
+ R::batch('book',array($id));
+ asrt(count($logger->grep('SELECT')),3);
+ R::$writer->setUseCache(false);
+
+ //test auto flush
+ $logger->clear();
+ $book = R::findOne('book');
+ $book->name = 'X';
+ R::store($book);
+ $book = R::findOne('book');
+ asrt(count($logger->grep('SELECT *')),2);
+ R::$writer->setUseCache(true); //with cache
+ $logger->clear();
+ $book = R::findOne('book');
+ $book->name = 'Y';
+ R::store($book); //will flush
+ $book = R::findOne('book');
+ asrt(count($logger->grep('SELECT *')),2); // now the same, auto flushed
+ R::$writer->setUseCache(false);
+
+ //test whether delete flushes as well (because uses selectRecord - might be a gotcha!)
+ R::store(R::dispense('garbage'));
+ $garbage = R::findOne('garbage');
+ $logger->clear();
+ $book = R::findOne('book');
+ R::trash($garbage);
+ $book = R::findOne('book');
+ asrt(count($logger->grep('SELECT *')),2);
+
+ R::store(R::dispense('garbage'));
+ $garbage = R::findOne('garbage');
+ R::$writer->setUseCache(true); //with cache
+ $logger->clear();
+ $book = R::findOne('book');
+ R::trash($garbage);
+ $book = R::findOne('book');
+ asrt(count($logger->grep('SELECT *')),2); // now the same, auto flushed
+ R::$writer->setUseCache(false);
+
+ R::store(R::dispense('garbage'));
+ $garbage = R::findOne('garbage');
+ R::$writer->setUseCache(true); //with cache
+ $logger->clear();
+ $book = R::findOne('book');
+ R::$writer->selectRecord('garbage',array('id'=>array($garbage->id)),null,true);
+ $book = R::findOne('book');
+ asrt(count($logger->grep('SELECT *')),2); // now the same, auto flushed
+ R::$writer->setUseCache(false);
+
}
}

0 comments on commit d302686

Please sign in to comment.