CacheTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DataObject\Test\Unit;
  7. class CacheTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\DataObject\Cache
  11. */
  12. protected $cache;
  13. protected function setUp()
  14. {
  15. $this->cache = new \Magento\Framework\DataObject\Cache();
  16. }
  17. public function testSaveWhenArgumentIsNotObject()
  18. {
  19. $this->assertEquals(false, $this->cache->save('string'));
  20. }
  21. /**
  22. * @expectedException \Magento\Framework\Exception\LocalizedException
  23. * @expectedExceptionMessage Object already exists in registry (#1). Old object class: stdClass
  24. */
  25. public function testSaveWhenObjectAlreadyExistsInRegistry()
  26. {
  27. $object = new \stdClass();
  28. $hash = spl_object_hash($object);
  29. $newIdx = 'idx' . $hash;
  30. $this->assertEquals($newIdx, $this->cache->save($object, 'idx{hash}', ['tags_array']));
  31. $this->assertEquals([$newIdx => $object], $this->cache->findByClass('stdClass'));
  32. $this->assertEquals([$newIdx => $object], $this->cache->getAllObjects());
  33. $this->assertEquals($newIdx, $this->cache->find($object));
  34. $this->assertEquals([$newIdx => $object], $this->cache->findByIds([$newIdx]));
  35. $objectTwo = new \stdClass();
  36. $this->assertEquals('#1', $this->cache->save($objectTwo, null, 'tags_string'));
  37. $objectThree = new \stdClass();
  38. $this->cache->save($objectThree, '#1');
  39. }
  40. public function testSaveAndDeleteWhenHashAlreadyExist()
  41. {
  42. $object = new \stdClass();
  43. $hash = spl_object_hash($object);
  44. $this->assertEquals('idx' . $hash, $this->cache->save($object, 'idx{hash}'));
  45. $this->assertEquals('idx' . $hash, $this->cache->save($object));
  46. $this->assertTrue($this->cache->delete('idx' . $hash));
  47. $this->assertFalse($this->cache->delete('idx' . $hash));
  48. }
  49. /**
  50. * @expectedException \Magento\Framework\Exception\LocalizedException
  51. * @expectedExceptionMessage The reference already exists: refName. New index: idx, old index: idx
  52. */
  53. public function testReferenceWhenReferenceAlreadyExist()
  54. {
  55. $refName = ['refName', 'refName'];
  56. $this->cache->reference($refName, 'idx');
  57. }
  58. public function testReferenceWhenReferenceEmpty()
  59. {
  60. $this->assertEquals(null, $this->cache->reference([], 'idx'));
  61. }
  62. public function testLoadWhenReferenceAndObjectAlreadyExists()
  63. {
  64. $idx = 'idx';
  65. $this->cache->reference('refName', $idx);
  66. $object = new \stdClass();
  67. $hash = spl_object_hash($object);
  68. $this->assertEquals(null, $this->cache->findByHash($hash));
  69. $this->cache->save($object, $idx);
  70. $this->assertEquals($object, $this->cache->load($idx));
  71. $this->assertEquals(true, $this->cache->has($idx));
  72. $this->assertEquals($object, $this->cache->findByHash($hash));
  73. $this->assertEquals(['refName' => 'idx'], $this->cache->getAllReferences());
  74. }
  75. public function testLoad()
  76. {
  77. $this->assertEquals('default', $this->cache->load('idx', 'default'));
  78. }
  79. public function testDeleteWhenIdxIsObject()
  80. {
  81. $object = new \stdClass();
  82. $this->cache->save($object, 'idx{hash}');
  83. $this->assertFalse($this->cache->delete($object));
  84. $this->cache->save($object, false);
  85. $this->assertFalse($this->cache->delete($object));
  86. }
  87. public function testDeleteIfReferencesExists()
  88. {
  89. $this->cache->reference('refName', 'idx');
  90. $object = new \stdClass();
  91. $this->cache->save($object, 'idx');
  92. $this->assertTrue($this->cache->delete('idx'));
  93. }
  94. public function testDeleteByClass()
  95. {
  96. $object = new \stdClass();
  97. $this->cache->save($object, 'idx');
  98. $this->cache->deleteByClass('stdClass');
  99. $this->assertFalse($this->cache->find($object));
  100. }
  101. public function testDebug()
  102. {
  103. $object = new \stdClass();
  104. $hash = spl_object_hash($object);
  105. $newIdx = 'idx' . $hash;
  106. $this->assertEquals($newIdx, $this->cache->save($object, 'idx{hash}'));
  107. $this->cache->debug($newIdx);
  108. $this->assertTrue(array_key_exists($newIdx, $this->cache->debugByIds($newIdx)));
  109. }
  110. public function testGetAndDeleteTags()
  111. {
  112. $object = new \stdClass();
  113. $hash = spl_object_hash($object);
  114. $newIdx = 'idx' . $hash;
  115. $tags = ['tags_array' => [$newIdx => true]];
  116. $tagsByObj = [$newIdx => ['tags_array' => true]];
  117. $this->assertEquals($newIdx, $this->cache->save($object, 'idx{hash}', ['tags_array']));
  118. $this->assertEquals($tags, $this->cache->getAllTags());
  119. $this->assertEquals([$newIdx => $object], $this->cache->findByTags('tags_array'));
  120. $this->assertEquals($tagsByObj, $this->cache->getAllTagsByObject());
  121. $this->assertTrue($this->cache->deleteByTags('tags_array'));
  122. }
  123. public function testSinglton()
  124. {
  125. $this->assertEquals($this->cache, $this->cache->singleton());
  126. }
  127. }