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