Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CompressionTest.php
Go to the documentation of this file.
1 <?php
11 
12 class CompressionTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $_decorator;
18 
22  protected $_testString = 'Any string';
23 
27  protected static $_cacheStorage = [];
28 
29  protected function setUp()
30  {
31  $options = [
32  'concrete_backend' => $this->createMock(\Zend_Cache_Backend_File::class),
33  'compression_threshold' => strlen($this->_testString),
34  ];
35  $this->_decorator = new \Magento\Framework\Cache\Backend\Decorator\Compression($options);
36  }
37 
38  protected function tearDown()
39  {
40  unset($this->_decorator);
41  self::$_cacheStorage = [];
42  }
43 
44  public function testCompressData()
45  {
46  $method = new \ReflectionMethod(
47  \Magento\Framework\Cache\Backend\Decorator\Compression::class,
48  '_compressData'
49  );
50  $method->setAccessible(true);
51 
52  $this->assertStringStartsWith('CACHE_COMPRESSION', $method->invoke($this->_decorator, $this->_testString));
53  }
54 
55  public function testDecompressData()
56  {
57  $methodCompress = new \ReflectionMethod(
58  \Magento\Framework\Cache\Backend\Decorator\Compression::class,
59  '_compressData'
60  );
61  $methodCompress->setAccessible(true);
62 
63  $methodDecompress = new \ReflectionMethod(
64  \Magento\Framework\Cache\Backend\Decorator\Compression::class,
65  '_decompressData'
66  );
67  $methodDecompress->setAccessible(true);
68 
69  $this->assertEquals(
70  $this->_testString,
71  $methodDecompress->invoke(
72  $this->_decorator,
73  $methodCompress->invoke($this->_decorator, $this->_testString)
74  )
75  );
76  }
77 
78  public function testIsCompressionNeeded()
79  {
80  $method = new \ReflectionMethod(
81  \Magento\Framework\Cache\Backend\Decorator\Compression::class,
82  '_isCompressionNeeded'
83  );
84  $method->setAccessible(true);
85 
86  $this->assertFalse($method->invoke($this->_decorator, $this->_testString));
87  $this->assertFalse($method->invoke($this->_decorator, substr($this->_testString, 0, -1)));
88  $this->assertTrue($method->invoke($this->_decorator, $this->_testString . 's'));
89  }
90 
91  public function testIsDecompressionNeeded()
92  {
93  $prefix = 'CACHE_COMPRESSION';
94 
95  $method = new \ReflectionMethod(
96  \Magento\Framework\Cache\Backend\Decorator\Compression::class,
97  '_isDecompressionNeeded'
98  );
99  $method->setAccessible(true);
100 
101  $this->assertFalse($method->invoke($this->_decorator, $this->_testString));
102  $this->assertFalse($method->invoke($this->_decorator, 's' . $prefix . $this->_testString));
103  $this->assertTrue($method->invoke($this->_decorator, $prefix . $this->_testString));
104  }
105 
106  public function testSaveLoad()
107  {
108  $cacheId = 'cacheId' . rand(1, 100);
109 
110  $backend = $this->createPartialMock(\Zend_Cache_Backend_File::class, ['save', 'load']);
111  $backend->expects($this->once())->method('save')->will($this->returnCallback([__CLASS__, 'mockSave']));
112 
113  $backend->expects($this->once())->method('load')->will($this->returnCallback([__CLASS__, 'mockLoad']));
114 
115  $options = ['concrete_backend' => $backend, 'compression_threshold' => strlen($this->_testString)];
116 
117  $decorator = new \Magento\Framework\Cache\Backend\Decorator\Compression($options);
118 
119  $decorator->setOption('write_control', false);
120  $decorator->setOption('automatic_cleaning_factor', 0);
121 
122  $decorator->save($this->_testString, $cacheId);
123 
124  $this->assertArrayHasKey($cacheId, self::$_cacheStorage);
125  $this->assertInternalType('string', self::$_cacheStorage[$cacheId]);
126 
127  $loadedValue = $decorator->load($cacheId);
128 
129  $this->assertEquals($this->_testString, $loadedValue);
130  }
131 
137  public static function mockSave($data, $cacheId)
138  {
139  self::$_cacheStorage[$cacheId] = $data;
140  return true;
141  }
142 
147  public static function mockLoad($cacheId)
148  {
149  return array_key_exists($cacheId, self::$_cacheStorage) ? self::$_cacheStorage[$cacheId] : false;
150  }
151 }
$prefix
Definition: name.phtml:25
$method
Definition: info.phtml:13