Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PageCachePluginTest.php
Go to the documentation of this file.
1 <?php
2 /***
3  * Copyright © Magento, Inc. All rights reserved.
4  * See COPYING.txt for license details.
5  */
6 
8 
12 
13 class PageCachePluginTest extends \PHPUnit\Framework\TestCase
14 {
16  private $plugin;
17 
19  private $subjectMock;
20 
21  protected function setUp()
22  {
23  $this->plugin = (new ObjectManager($this))->getObject(\Magento\PageCache\Model\App\PageCachePlugin::class);
24  $this->subjectMock = $this->getMockBuilder(\Magento\Framework\App\PageCache\Cache::class)
25  ->disableOriginalConstructor()
26  ->getMock();
27  }
28 
29  public function testBeforeSaveAddTag()
30  {
31  $initTags = ['tag', 'otherTag'];
32  $result = $this->plugin->beforeSave($this->subjectMock, 'data', 'identifier', $initTags);
33  $tags = isset($result[2]) ? $result[2] : null;
34  $expectedTags = array_merge($initTags, [Type::CACHE_TAG]);
35  $this->assertNotNull($tags);
36  foreach ($expectedTags as $expected) {
37  $this->assertContains($expected, $tags);
38  }
39  }
40 
41  public function testBeforeSaveCompression()
42  {
43  $data = 'raw-data';
44  $expected = PageCachePlugin::COMPRESSION_PREFIX . gzcompress($data);
45  $result = $this->plugin->beforeSave($this->subjectMock, $data, 'id');
46  $resultData = $result[0];
47  $this->assertSame($resultData, $expected);
48  }
49 
55  public function testAfterSaveDecompression($data, $initResult)
56  {
57  $this->assertSame($data, $this->plugin->afterLoad($this->subjectMock, $initResult));
58  }
59 
63  public function afterSaveDataProvider()
64  {
65  return [
66  'Compressed cache' => ['raw-data', PageCachePlugin::COMPRESSION_PREFIX . gzcompress('raw-data')],
67  'Non-compressed cache' => ['raw-data', 'raw-data']
68  ];
69  }
70 }