PageRenderTime 71ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/CacheSerializerTest.php

https://bitbucket.org/gjerokrsteski/php-dba-cache
PHP | 103 lines | 75 code | 18 blank | 10 comment | 1 complexity | a4161369bae513c998d552cf1189269d MD5 | raw file
  1. <?php
  2. require_once dirname(dirname(__FILE__)) . '/src/CacheDba.php';
  3. require_once dirname(dirname(__FILE__)) . '/src/CacheSerializer.php';
  4. require_once dirname(__FILE__) .'/DummyFixtures.php';
  5. class CacheSerializerTest extends PHPUnit_Framework_TestCase
  6. {
  7. public function testCreatingNewSerializerObject()
  8. {
  9. $this->assertNotNull(new CacheSerializer());
  10. }
  11. public function objectsProvider()
  12. {
  13. $stdClass = new stdClass();
  14. $stdClass->title = 'Zweiundvierz';
  15. $stdClass->from = 'Joe';
  16. $stdClass->to = 'Jane';
  17. $stdClass->body = new Dummy2();
  18. return array(
  19. array( new Dummy2() ),
  20. array( $stdClass ),
  21. array( new ZipArchive() ),
  22. array( new XMLReader() ),
  23. array( 'i am a string' ),
  24. array( 123456789 ),
  25. array(
  26. array(
  27. 'boo'=> 1,
  28. 'foo'=> 2,
  29. 'laa'=> 3
  30. )
  31. )
  32. );
  33. }
  34. /**
  35. * @depends CacheSerializerTest::testCreatingNewSerializerObject
  36. * @dataProvider objectsProvider
  37. */
  38. public function testSerializingSomeObjects($object)
  39. {
  40. $serializer = new CacheSerializer();
  41. $serializer->serialize($object);
  42. }
  43. /**
  44. * @depends CacheSerializerTest::testCreatingNewSerializerObject
  45. * @depends CacheSerializerTest::testSerializingSomeObjects
  46. * @dataProvider objectsProvider
  47. */
  48. public function testUnserializingSomeObjectsAndCompareEachother($object)
  49. {
  50. $unserializer = new CacheSerializer();
  51. $serialized = $unserializer->serialize($object);
  52. $userItem = $unserializer->unserialize($serialized);
  53. $this->assertEquals($object, $userItem->object);
  54. }
  55. public function testHandlingWithSimpleXMLElement()
  56. {
  57. $identifier = md5(uniqid());
  58. // make a xml-file of 1000 nodes.
  59. $string = "<?xml version='1.0'?>
  60. <document>";
  61. for ($i = 1; $i <= 1000; $i++) {
  62. $string .= "<item>
  63. <title>Let us cache</title>
  64. <from>Joe</from>
  65. <to>Jane</to>
  66. <body>Some content here</body>
  67. </item>";
  68. }
  69. $string .= "</document>";
  70. $simplexml = simplexml_load_string(
  71. $string, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET
  72. );
  73. $path = dirname(dirname(__FILE__)) . '/tests/_drafts/test-cache-with-simplexml.db4';
  74. try {
  75. $cache = new CacheDba($path, 'db4');
  76. } catch(RuntimeException $e) {
  77. $this->markTestSkipped($e->getMessage());
  78. }
  79. $cache->put($identifier, $simplexml);
  80. $object_from_cache = $cache->get($identifier);
  81. $cache->closeDba();
  82. $this->assertEquals($simplexml->asXML(), $object_from_cache->asXML());
  83. unlink($path);
  84. }
  85. }