CacheSqliteTestCase.php 4.29 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
2 3
require_once("UnitTestCase.php");

doctrine's avatar
doctrine committed
4 5 6 7
class Doctrine_Cache_SqliteTestCase extends Doctrine_UnitTestCase {
    public function setUp() {
        parent::setUp();
        $this->manager->setAttribute(Doctrine::ATTR_CACHE,Doctrine::CACHE_NONE);
zYne's avatar
zYne committed
8
        $dir = $this->connection->getAttribute(Doctrine::ATTR_CACHE_DIR);
doctrine's avatar
doctrine committed
9 10 11 12 13 14 15

        if(file_exists($dir.DIRECTORY_SEPARATOR."stats.cache"))
            unlink($dir.DIRECTORY_SEPARATOR."stats.cache");

        $this->cache = new Doctrine_Cache_Sqlite($this->objTable);
        $this->cache->deleteAll();
    }
doctrine's avatar
doctrine committed
16

doctrine's avatar
doctrine committed
17 18 19 20 21 22 23 24 25 26 27 28
    public function testStore() {
        // does not store proxy objects
        $this->assertFalse($this->cache->store($this->objTable->getProxy(4)));
        
        $this->assertTrue($this->cache->store($this->objTable->find(4)));

        $record = $this->cache->fetch(4);
        $this->assertTrue($record instanceof Doctrine_Record);

        foreach($this->old as $name => $value) {
            $this->assertEqual($record->get($name), $value);
        }
29
        $this->assertEqual($record->obtainIdentifier(), $this->old->obtainIdentifier());
doctrine's avatar
doctrine committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    }
    public function testFetchMultiple() {
        $this->assertFalse($this->cache->fetchMultiple(array(5,6)));
        $this->cache->store($this->objTable->find(5));

        $array = $this->cache->fetchMultiple(array(5,6));
        $this->assertEqual(gettype($array), "array");
        $this->assertEqual(count($array), 1);
        $this->assertTrue($array[0] instanceof Doctrine_Record);
    }
    public function testDeleteMultiple() {
        $this->assertEqual($this->cache->deleteMultiple(array()),0);
        $this->cache->store($this->objTable->find(5));
        $this->cache->store($this->objTable->find(6));

        $count = $this->cache->deleteMultiple(array(5,6));

        $this->assertEqual($count,2);
        $this->cache->store($this->objTable->find(6));
        $count = $this->cache->deleteMultiple(array(5,6));
        $this->assertEqual($count,1);
    }
    public function testDelete() {
        $this->cache->store($this->objTable->find(5));
        $this->assertTrue($this->cache->fetch(5) instanceof Doctrine_Record);

        $this->assertEqual($this->cache->delete(5),true);
        $this->assertFalse($this->cache->fetch(5));

        $this->assertFalse($this->cache->delete(0));
    }

    public function testFetch() {
        $this->assertFalse($this->cache->fetch(3));

    }
    public function testCount() {
        $this->assertEqual($this->cache->count(), 0);
        $this->cache->store($this->objTable->find(5));
        $this->assertEqual($this->cache->count(), 1);
    }
    public function testSaveStats() {
        $this->assertFalse($this->cache->saveStats());
        $this->cache->store($this->objTable->find(5));
        $this->cache->store($this->objTable->find(6));
        $this->cache->store($this->objTable->find(7));
        $this->cache->fetchMultiple(array(5,6,7));

        $this->assertTrue($this->cache->saveStats());
        $this->assertTrue(gettype($this->cache->getStats()), "array");
        $this->assertEqual($this->cache->getStats(),array(5 => 1, 6 => 1, 7 => 1));

        $this->cache->fetchMultiple(array(5,6,7));
        $this->cache->fetch(5);
        $this->cache->fetch(7);
        $this->assertTrue($this->cache->saveStats());
        $this->assertEqual($this->cache->getStats(),array(5 => 3, 6 => 2, 7 => 3));
    }
    public function testClean() {
        $this->cache->store($this->objTable->find(4));
        $this->cache->store($this->objTable->find(5));
        $this->cache->store($this->objTable->find(6));
        $this->cache->store($this->objTable->find(7));
        $this->cache->store($this->objTable->find(8));
        $this->cache->store($this->objTable->find(9));
        $this->assertEqual($this->cache->count(), 6);
        $this->cache->fetch(5);
        $this->cache->fetch(7);
        $this->cache->fetchMultiple(array(5,6,7));
        $this->cache->fetchMultiple(array(5,6,7));
        $this->cache->fetchMultiple(array(5,6,7));
        $this->cache->fetchMultiple(array(4,5,6,7,8,9));
        $this->assertTrue($this->cache->saveStats());
        
        $this->manager->setAttribute(Doctrine::ATTR_CACHE_SIZE, 3);
        $this->assertEqual($this->cache->clean(), 3);

    }
}