php-dba-cache /tests/CacheDbaHandlersTest.php

Language PHP Lines 165
MD5 Hash d2c88d391f431284c1cd32e32a865c5f Estimated Cost $2,383 (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
<?php
require_once dirname(dirname(__FILE__)) . '/src/CacheDba.php';
require_once dirname(dirname(__FILE__)) . '/src/CacheSerializer.php';
require_once dirname(__FILE__) .'/DummyFixtures.php';


class CacheDbaHandlersTest extends PHPUnit_Framework_TestCase
{
  /**
   * @var stdClass
   */
  protected $_object;

  /**
   * @var string
   */
  protected $_identifier;

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

    $stdClass        = new stdClass();
    $stdClass->title = 'Zweiundvierz';
    $stdClass->from  = 'Joe';
    $stdClass->to    = 'Jane';
    $stdClass->body  = new Dummy();

    $this->_object     = $stdClass;
    $this->_identifier = md5('stdClass' . time());
  }

  /**
   * Cleans up the environment after running a test.
   */
  protected function tearDown()
  {
    unset($this->_object, $this->_identifier);
    parent::tearDown();
  }

  /**
   * Tests support for Oracle Berkeley DB 4.
   */
  public function testOracleBerkeleyDb4HandlerSupportWithoutPersistantConnection()
  {
    try {
      $cache = new CacheDba(
        dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache.db4', 'db4', 'c', false
      );
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

    $this->assertInstanceOf('CacheDba', $cache);

    $cache->put($this->_identifier, $this->_object);

    $this->assertInstanceOf('stdClass', $cache->get($this->_identifier));
  }

  /**
   * @depends CacheDbaHandlersTest::testOracleBerkeleyDb4HandlerSupportWithoutPersistantConnection
   */
  public function testOracleBerkeleyDb4HandlerBeSupportedWithPersistantConnection()
  {
    try {
      $cache = new CacheDba(dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache.db4', 'db4');
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

    $this->assertInstanceOf('CacheDba', $cache);

    $cache->put($this->_identifier, $this->_object);

    $this->assertInstanceOf('stdClass', $cache->get($this->_identifier));
  }

  /**
   * Tests support for CDB - Tiny Constant Database.
   */
  public function testCanCdbHandlerOnlyNewAndReadBeSupportedWithPersistantConnection()
  {
    $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache-cdb2.cdb';

    // create handler to write.
    try {
      $cacheMake = new CacheDba($path, 'cdb_make', 'n');
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

    $this->assertInstanceOf('CacheDba', $cacheMake);

    $this->assertTrue($cacheMake->put(md5('test123'), $this->_object));

    // for read we close the handler.
    $cacheMake->closeDba();

    // create handler to read.
    try {
      $cacheRead = new CacheDba($path, 'cdb', 'r');
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

    $this->assertTrue($cacheRead->has(md5('test123')));

    $this->assertInstanceOf('stdClass', $cacheRead->get(md5('test123')));

    $cacheRead->closeDba();
  }

  public function testCreateAnCdbHandler()
  {
  $path = dirname(dirname(__FILE__)) . '/tests/_drafts/simple-xml-test-cache-on-cdb.db';

    try {
      new CacheDba(
        $path, "cdb", "n"
      );
      unlink($path);
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }
  }

  public function testPutTheSameIdentifierTwiceToCdbHandler()
  {
    $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache-cdb2.cdb';

    // CREATE HANDLER TO WRITE.
    try {
      $cacheMake = new CacheDba($path, 'cdb_make', 'n');
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

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

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

    // for read we close the handler.
    $cacheMake->closeDba();

    // CREATE HANDLER TO READ.
    try {
      $cacheRead = new CacheDba($path, 'cdb', 'r');
    } catch(RuntimeException $e) {
     $this->markTestSkipped($e->getMessage());
    }

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

    $cacheRead->closeDba();
  }
}
Back to Top