PageCachePluginTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /***
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\PageCache\Test\Unit\Model\App;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\PageCache\Model\App\PageCachePlugin;
  9. use Magento\PageCache\Model\Cache\Type;
  10. class PageCachePluginTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /** @var PageCachePlugin */
  13. private $plugin;
  14. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\PageCache\Cache*/
  15. private $subjectMock;
  16. protected function setUp()
  17. {
  18. $this->plugin = (new ObjectManager($this))->getObject(\Magento\PageCache\Model\App\PageCachePlugin::class);
  19. $this->subjectMock = $this->getMockBuilder(\Magento\Framework\App\PageCache\Cache::class)
  20. ->disableOriginalConstructor()
  21. ->getMock();
  22. }
  23. public function testBeforeSaveAddTag()
  24. {
  25. $initTags = ['tag', 'otherTag'];
  26. $result = $this->plugin->beforeSave($this->subjectMock, 'data', 'identifier', $initTags);
  27. $tags = isset($result[2]) ? $result[2] : null;
  28. $expectedTags = array_merge($initTags, [Type::CACHE_TAG]);
  29. $this->assertNotNull($tags);
  30. foreach ($expectedTags as $expected) {
  31. $this->assertContains($expected, $tags);
  32. }
  33. }
  34. public function testBeforeSaveCompression()
  35. {
  36. $data = 'raw-data';
  37. $expected = PageCachePlugin::COMPRESSION_PREFIX . gzcompress($data);
  38. $result = $this->plugin->beforeSave($this->subjectMock, $data, 'id');
  39. $resultData = $result[0];
  40. $this->assertSame($resultData, $expected);
  41. }
  42. /**
  43. * @dataProvider afterSaveDataProvider
  44. * @param string $dataw
  45. * @param string $initResult
  46. */
  47. public function testAfterSaveDecompression($data, $initResult)
  48. {
  49. $this->assertSame($data, $this->plugin->afterLoad($this->subjectMock, $initResult));
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function afterSaveDataProvider()
  55. {
  56. return [
  57. 'Compressed cache' => ['raw-data', PageCachePlugin::COMPRESSION_PREFIX . gzcompress('raw-data')],
  58. 'Non-compressed cache' => ['raw-data', 'raw-data']
  59. ];
  60. }
  61. }