php-dba-cache /tests/CacheSerializerTest.php

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


class CacheSerializerTest extends PHPUnit_Framework_TestCase
{
  public function testCreatingNewSerializerObject()
  {
    $this->assertNotNull(new CacheSerializer());
  }

  public function objectsProvider()
  {
    $stdClass        = new stdClass();
    $stdClass->title = 'Zweiundvierz';
    $stdClass->from  = 'Joe';
    $stdClass->to    = 'Jane';
    $stdClass->body  = new Dummy2();

    return array(
      array( new Dummy2() ),
      array( $stdClass ),
      array( new ZipArchive() ),
      array( new XMLReader() ),
      array( 'i am a string' ),
      array( 123456789 ),
      array(
        array(
          'boo'=> 1,
          'foo'=> 2,
          'laa'=> 3
        )
      )
    );
  }

  /**
   * @depends CacheSerializerTest::testCreatingNewSerializerObject
   * @dataProvider objectsProvider
   */
  public function testSerializingSomeObjects($object)
  {
    $serializer = new CacheSerializer();
    $serializer->serialize($object);
  }

  /**
   * @depends CacheSerializerTest::testCreatingNewSerializerObject
   * @depends CacheSerializerTest::testSerializingSomeObjects
   * @dataProvider objectsProvider
   */
  public function testUnserializingSomeObjectsAndCompareEachother($object)
  {
    $unserializer = new CacheSerializer();

    $serialized = $unserializer->serialize($object);

    $userItem = $unserializer->unserialize($serialized);

    $this->assertEquals($object, $userItem->object);
  }

  public function testHandlingWithSimpleXMLElement()
  {
    $identifier = md5(uniqid());

    // make a xml-file of 1000 nodes.
    $string = "<?xml version='1.0'?>
        <document>";
    for ($i = 1; $i <= 1000; $i++) {
      $string .= "<item>
			 <title>Let us cache</title>
			 <from>Joe</from>
			 <to>Jane</to>
			 <body>Some content here</body>
                 </item>";
    }
    $string .= "</document>";

    $simplexml = simplexml_load_string(
      $string, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET
    );

    $path  = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache-with-simplexml.db4';

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

    $cache->put($identifier, $simplexml);
    $object_from_cache = $cache->get($identifier);
    $cache->closeDba();

    $this->assertEquals($simplexml->asXML(), $object_from_cache->asXML());

    unlink($path);
  }
}
Back to Top