CacheManagerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Interception\Config;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. class CacheManagerTest extends \PHPUnit\Framework\TestCase
  10. {
  11. const CACHE_ID = 'interceptiontest';
  12. /**
  13. * @var \Magento\Framework\ObjectManagerInterface
  14. */
  15. private $objectManager;
  16. /**
  17. * @var \Magento\Framework\Serialize\SerializerInterface
  18. */
  19. private $serializer;
  20. /**
  21. * @var \Magento\Framework\Cache\FrontendInterface
  22. */
  23. private $cache;
  24. /**
  25. * @var \Magento\Framework\App\ObjectManager\ConfigWriterInterface
  26. */
  27. private $configWriter;
  28. protected function setUp()
  29. {
  30. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  31. $this->serializer = $this->objectManager->get(\Magento\Framework\Serialize\SerializerInterface::class);
  32. $this->cache = $this->objectManager->get(\Magento\Framework\App\CacheInterface::class);
  33. $this->configWriter =
  34. $this->objectManager->get(\Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem::class);
  35. $this->initializeMetadataDirectory();
  36. }
  37. /**
  38. * Delete compiled file if it was created and clear cache data
  39. */
  40. protected function tearDown()
  41. {
  42. $compiledPath = \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::getFilePath(self::CACHE_ID);
  43. if (file_exists($compiledPath)) {
  44. unlink($compiledPath);
  45. }
  46. $this->cache->remove(self::CACHE_ID);
  47. }
  48. /**
  49. * Test load interception cache from generated/metadata
  50. * @dataProvider interceptionCompiledConfigDataProvider
  51. * @param array $testConfig
  52. */
  53. public function testInstantiateFromCompiled(array $testConfig)
  54. {
  55. $this->configWriter->write(self::CACHE_ID, $testConfig);
  56. $config = $this->getConfig();
  57. $this->assertEquals($testConfig, $config->load(self::CACHE_ID));
  58. }
  59. /**
  60. * Test load interception cache from backend cache
  61. * @dataProvider interceptionCacheConfigDataProvider
  62. * @param array $testConfig
  63. */
  64. public function testInstantiateFromCache(array $testConfig)
  65. {
  66. $this->cache->save($this->serializer->serialize($testConfig), self::CACHE_ID);
  67. $config = $this->getConfig();
  68. $this->assertEquals($testConfig, $config->load(self::CACHE_ID));
  69. }
  70. public function interceptionCompiledConfigDataProvider()
  71. {
  72. return [
  73. [['classA' => true, 'classB' => false]],
  74. [['classA' => false, 'classB' => true]],
  75. ];
  76. }
  77. public function interceptionCacheConfigDataProvider()
  78. {
  79. return [
  80. [['classC' => true, 'classD' => false]],
  81. [['classC' => false, 'classD' => true]],
  82. ];
  83. }
  84. /**
  85. * Ensure generated/metadata exists
  86. */
  87. private function initializeMetadataDirectory()
  88. {
  89. $diPath = DirectoryList::getDefaultConfig()[DirectoryList::GENERATED_METADATA][DirectoryList::PATH];
  90. $fullPath = BP . DIRECTORY_SEPARATOR . $diPath;
  91. if (!file_exists($fullPath)) {
  92. mkdir($fullPath);
  93. }
  94. }
  95. /**
  96. * Create instance of Config class with specific cacheId. This is done to prevent our test
  97. * from altering the interception config that may have been generated during application
  98. * installation. Inject a new instance of the compileLoaded to bypass it's caching.
  99. *
  100. * @return \Magento\Framework\Interception\Config\CacheManager
  101. */
  102. private function getConfig()
  103. {
  104. return $this->objectManager->create(
  105. \Magento\Framework\Interception\Config\CacheManager::class,
  106. [
  107. 'cacheId' => self::CACHE_ID,
  108. 'compiledLoader' => $this->objectManager->create(
  109. \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::class
  110. ),
  111. ]
  112. );
  113. }
  114. }