CacheTestCase.php 5.8 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */

/**
 * Doctrine_Cache_TestCase
 *
 * @package     Doctrine
 * @subpackage  Doctrine_Cache
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
34
class Doctrine_Cache_TestCase extends Doctrine_UnitTestCase
zYne's avatar
zYne committed
35
{
36
    protected $cache;
zYne's avatar
zYne committed
37

38 39 40 41
    public function prepareTables()
    { }
    public function prepareData()
    { }
zYne's avatar
zYne committed
42
    /**
43 44 45 46 47 48
    public function testAdapterQueryAddsQueriesToCacheStack()
    {
        $this->dbh->query('SELECT * FROM user');

        $this->assertEqual($this->cache->getAll(), array('main' => array('SELECT * FROM user')));
    }
zYne's avatar
zYne committed
49 50 51 52 53 54 55
    */
    public function testAdapterQueryChecksCache()
    {
    	$query = 'SELECT * FROM user';

        $resultSet = array(array('name' => 'John'), array('name' => 'Arnold'));

zYne's avatar
zYne committed
56
    	$this->cache->getDriver()->save(md5(serialize($query)), $resultSet);
zYne's avatar
zYne committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

        $count = $this->dbh->getAdapter()->count();

        $stmt = $this->dbh->query($query);
        $data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);

        $this->assertEqual($data, $resultSet);
        $this->assertEqual($this->dbh->getAdapter()->count(), $count);
    }
    public function testAdapterStatementExecuteChecksCache()
    {
    	$query  = 'SELECT * FROM user WHERE id = ?';
        $params = array(1);
        $resultSet = array(array('name' => 'John'), array('name' => 'Arnold'));

    	$this->cache->getDriver()->save(md5(serialize(array($query, $params))), $resultSet);

        $count = $this->dbh->getAdapter()->count();

        $stmt = $this->dbh->prepare($query);
        $stmt->execute($params);
        $data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);

        $this->assertEqual($data, $resultSet);
        $this->assertEqual($this->dbh->getAdapter()->count(), $count);
    }
zYne's avatar
zYne committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    public function testFetchAdvancesCacheDataPointer()
    {
        $query  = 'SELECT * FROM user WHERE id = ?';
        $count = $this->dbh->getAdapter()->count();
        $params = array(1);
        $stmt = $this->dbh->prepare($query);
        $stmt->execute($params);

        $row1 = $stmt->fetch();
        $row2 = $stmt->fetch();

        $this->assertEqual($row1, array('name' => 'John'));
        $this->assertEqual($row2, array('name' => 'Arnold'));

        $this->assertEqual($this->dbh->getAdapter()->count(), $count);
    }

100 101 102 103 104 105 106 107
    public function testAdapterStatementExecuteAddsQueriesToCacheStack()
    {
        $stmt = $this->dbh->prepare('SELECT * FROM user');

        $stmt->execute();

        $this->assertEqual($this->cache->getAll(), array('main' => array('SELECT * FROM user')));
    }
zYne's avatar
zYne committed
108 109 110 111 112 113 114 115
    public function testAdapterStatementFetchCallsCacheFetch()
    {
        $stmt = $this->dbh->prepare('SELECT * FROM user');

        $stmt->execute();

        $a = $stmt->fetchAll();
    }
zYne's avatar
zYne committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    public function testAdapterStatementExecuteAddsQueriesToCache()
    {
    	$this->cache->setOption('savePropability', 1);

    	$driver = $this->cache->getDriver();

    	$driver->deleteAll();

    	$this->assertEqual($driver->count(), 0);

        $stmt = $this->dbh->prepare('SELECT * FROM user WHERE id = ?');

        $stmt->execute(array(1));

        $this->assertEqual($driver->count(), 1);
    }
    public function testAppendStatsWritesQueriesToStatsFile()
    {
    	$this->cache->setOption('addStatsPropability', 1);
    	
    	$data = array(1,2,3);

        $this->cache->add('SELECT * FROM user');
        $this->cache->add(array('SELECT * FROM user WHERE id = ?', array(1)));

        $this->cache->appendStats();
        
        $stats = $this->cache->readStats();

        $this->assertEqual($stats[0], 'SELECT * FROM user');
        $this->assertEqual($stats[1], array('SELECT * FROM user WHERE id = ?', array(1)));
    }
    public function testCleanRemovesDriver()
    {
    	$this->cache->setOption('cleanPropability', 1);

        $this->cache->add('SELECT * FROM user');
        $this->cache->add(array('SELECT * FROM user WHERE id = ?', array(1)));

        $this->cache->appendStats();
        
        $stats = $this->cache->readStats();

        $this->assertEqual($stats[0], 'SELECT * FROM user');
        $this->assertEqual($stats[1], array('SELECT * FROM user WHERE id = ?', array(1)));
    }
zYne's avatar
zYne committed
162

163 164 165 166 167 168
    public function setUp()
    {
        parent::setUp();

    	if ( ! isset($this->cache)) {
            $this->cache = new Doctrine_Cache('Array');
zYne's avatar
zYne committed
169
            $this->cache->setOption('cacheFile', false);
zYne's avatar
zYne committed
170 171
            $this->cache->setOption('savePropability', 0);

172 173 174 175 176 177
            $this->dbh->setAdapter(new Doctrine_Adapter_Mock());
            $this->dbh->addListener($this->cache);
        }

        $this->cache->reset();
    }
zYne's avatar
zYne committed
178
}