php-dba-cache /tests/CacheDbaTest.php

Language PHP Lines 183
MD5 Hash c1a6046f9d52c1a5c37998339018593c Estimated Cost $3,044 (why?)
Repository https://bitbucket.org/gjerokrsteski/php-dba-cache.git View Raw File View Project SPDX
  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
 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
110
111
112
113
114
115
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
require_once dirname(dirname(__FILE__)) . '/src/CacheDba.php';
require_once dirname(dirname(__FILE__)) . '/src/CacheSerializer.php';
require_once dirname(__FILE__) .'/DummyFixtures.php';


class CacheDbaTest extends PHPUnit_Framework_TestCase
{
  /**
   * @var CacheDba
   */
  private $_cache;

  /**
   * Prepares the environment before running a test.
   */
  protected function setUp()
  {
    parent::setUp();

    try {
      $this->_cache = new CacheDba(dirname(dirname(__FILE__)) . '/tests/_drafts/cache.flat');
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }
  }

  /**
   * Cleans up the environment after running a test.
   */
  protected function tearDown()
  {
    unset($this->_cache);

    parent::tearDown();
  }

  public function objectsProvider()
  {
    $stdClass        = new stdClass();
    $stdClass->title = 'Zweiundvierz';
    $stdClass->from  = 'Joe';
    $stdClass->to    = 'Jane';
    $stdClass->body  = 'Ich kenne die Antwort -- aber was ist die Frage?';

    return array(
      array(
        md5('stdClass'),
        $stdClass
      ),
      array(
        md5('ZipArchive'),
        new ZipArchive()
      ),
      array(
        md5('XMLReader'),
        new XMLReader()
      ),
      array(
        md5('Dummy'),
        new Dummy()
      )
    );
  }

  /**
   * @depends CacheDbaTest::testCreateNewCacheObjectNoException
   * @dataProvider objectsProvider
   */
  public function testPutSomeObjectsIntoTheCache($identifier, $object)
  {
    try {
      $this->_cache->put($identifier, $object);
    } catch (Exception $e) {
      $this->fail(
        $e->getMessage()
      );
    }
  }

  /**
   * @depends CacheDbaTest::testPutSomeObjectsIntoTheCache
   * @dataProvider objectsProvider
   */
  public function testGetSomeObjectsFromTheCacheAndCompareEachother($identifier, $expectedObject)
  {
    try {
      $this->assertTrue($this->_cache->has($identifier));

      $getObject = $this->_cache->get($identifier);

      $this->assertEquals($expectedObject, $getObject);
    } catch (Exception $e) {
      $this->fail(
        $e->getMessage()
      );
    }
  }

  public function testReadAllIdsInCache()
  {
    $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache-get-all-ids.flat';

    try {
      $cache = new CacheDba(
        $path, 'flatfile', 'c', false
      );
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

    $cache->put('array-1', array( 1 ));
    $cache->put('string-2', 'some big string');
    $cache->put('float-3', 1234.87987698);

    $ids = $cache->getIds();

    $this->assertInstanceOf('ArrayObject', $ids);
    $this->assertEquals($ids[0], 'array-1');
    $this->assertEquals($ids[1], 'string-2');
    $this->assertEquals($ids[2], 'float-3');

    unlink($path);
  }

  public function testPutTheSameIdentifierTwiceToFlatfileHandler()
  {
    $path  = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache-insert.flat';

    try {
      $cache = new CacheDba($path, 'flatfile', 'c', false);
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

    // first insert.
    $this->assertTrue($cache->put('key', 'data'));

    // replace instead of insert.
    $this->assertTrue($cache->put('key', 'data-2'));

    //check if data replaced.
    $this->assertEquals('data-2', $cache->get('key'));
  }

  public function testPutTheSameIdentifierTwiceToDb4Handler()
  {
    $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache.db4';

    try {
      $cache = new CacheDba($path, 'db4');
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

    // first insert.
    $this->assertTrue($cache->put('key', 'data'));

    // replace instead of insert.
    $this->assertTrue($cache->put('key', 'data-2'));

    //check if data replaced.
    $this->assertEquals('data-2', $cache->get('key'));
  }

  public function testLoadingMetadata()
  {
    $path  = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache.db4';

    try {
      $cache = new CacheDba($path, 'db4');
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

    // first insert.
    $this->assertTrue($cache->put('key', 'data'));

    $this->assertInternalType('array', $cache->getMetaData('key'));
    $this->assertNotEmpty($cache->getMetaData('key'));
  }
}
Back to Top