CacheStoragePersistenceTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Test\Integration\TaxRegistry;
  7. use Magento\Framework\App\Cache\StateInterface;
  8. use Vertex\Tax\Model\Cache\Type as CacheType;
  9. use Vertex\Tax\Model\TaxRegistry\CacheStorage;
  10. use Vertex\Tax\Test\Integration\TestCase;
  11. /**
  12. * Ensure that cache storage persists data across requests.
  13. * @magentoAppArea frontend
  14. */
  15. class CacheStoragePersistenceTest extends TestCase
  16. {
  17. /** @var StateInterface */
  18. private $cacheState;
  19. /** @var CacheStorage */
  20. private $cacheStorage;
  21. /**
  22. * Perform test setup.
  23. */
  24. protected function setUp()
  25. {
  26. parent::setUp();
  27. $this->cacheState = $this->getObject(StateInterface::class);
  28. $this->cacheStorage = $this->getObject(CacheStorage::class);
  29. }
  30. /**
  31. * Test that cache storage can unset its data.
  32. */
  33. public function testSuccessfulCacheUnset()
  34. {
  35. $this->cacheState->setEnabled(CacheType::TYPE_IDENTIFIER, true);
  36. $this->assertTrue($this->cacheState->isEnabled(CacheType::TYPE_IDENTIFIER));
  37. $this->assertTrue($this->cacheStorage->set('key_to_unset', 'value_to_unset'));
  38. $this->assertTrue($this->cacheStorage->unsetData('key_to_unset'));
  39. $this->assertNull($this->cacheStorage->get('key_to_unset'));
  40. }
  41. /**
  42. * Test that cache storage succeeds when in fallback mode.
  43. */
  44. public function testGenericPersistenceUnderCacheDisablement()
  45. {
  46. $this->cacheState->setEnabled(CacheType::TYPE_IDENTIFIER, false);
  47. $this->assertFalse($this->cacheState->isEnabled(CacheType::TYPE_IDENTIFIER));
  48. $expectedResult = 'test_value';
  49. $this->cacheStorage->set('test_key', $expectedResult);
  50. $actualResult = $this->cacheStorage->get('test_key');
  51. $this->assertEquals($expectedResult, $actualResult);
  52. }
  53. /**
  54. * Test that cache storage succeeds when enabled.
  55. */
  56. public function testPersistenceUnderCacheEnablement()
  57. {
  58. $this->cacheState->setEnabled(CacheType::TYPE_IDENTIFIER, true);
  59. $this->assertTrue($this->cacheState->isEnabled(CacheType::TYPE_IDENTIFIER));
  60. $expectedResult = 'test_value2';
  61. $this->cacheStorage->set('test_key2', $expectedResult, 300);
  62. $actualResult = $this->cacheStorage->get('test_key2');
  63. $this->assertEquals($expectedResult, $actualResult);
  64. }
  65. }