Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ChecksumTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\View\Asset\MergeStrategy\Checksum;
9 
12 
13 class ChecksumTest extends \PHPUnit\Framework\TestCase
14 {
18  private $mergerMock;
19 
23  private $sourceDir;
24 
28  private $targetDir;
29 
33  private $resultAsset;
34 
38  private $assetSource;
39 
43  private $checksum;
44 
45  protected function setUp()
46  {
47  $this->mergerMock = $this->getMockForAbstractClass(\Magento\Framework\View\Asset\MergeStrategyInterface::class);
48  $this->sourceDir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
49  $this->targetDir = $this->getMockForAbstractClass(
50  \Magento\Framework\Filesystem\Directory\WriteInterface::class
51  );
52  $filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
53  $filesystem->expects($this->once())
54  ->method('getDirectoryRead')
55  ->with(DirectoryList::ROOT)
56  ->will($this->returnValue($this->sourceDir));
57  $filesystem->expects($this->any())
58  ->method('getDirectoryWrite')
60  ->will($this->returnValue($this->targetDir));
61  $this->checksum = new Checksum($this->mergerMock, $filesystem);
62  $this->assetSource = $this->getMockBuilder(Source::class)
63  ->disableOriginalConstructor()
64  ->getMock();
65 
66  $reflection = new \ReflectionClass(Checksum::class);
67  $reflectionProperty = $reflection->getProperty('assetSource');
68  $reflectionProperty->setAccessible(true);
69  $reflectionProperty->setValue($this->checksum, $this->assetSource);
70 
71  $this->resultAsset = $this->createMock(\Magento\Framework\View\Asset\File::class);
72  }
73 
74  public function testMergeNoAssets()
75  {
76  $this->mergerMock->expects($this->never())->method('merge');
77  $this->checksum->merge([], $this->resultAsset);
78  }
79 
80  public function testMergeNoDatFile()
81  {
82  $this->targetDir->expects($this->once())
83  ->method('isExist')
84  ->with('merged/result.txt.dat')
85  ->will($this->returnValue(false));
86  $assets = $this->getAssetsToMerge();
87  $this->mergerMock->expects($this->once())->method('merge')->with($assets, $this->resultAsset);
88  $this->targetDir->expects($this->once())->method('writeFile')->with('merged/result.txt.dat', '11');
89  $this->checksum->merge($assets, $this->resultAsset);
90  }
91 
92  public function testMergeMtimeChanged()
93  {
94  $this->targetDir->expects($this->once())
95  ->method('isExist')
96  ->with('merged/result.txt.dat')
97  ->will($this->returnValue(true));
98  $this->targetDir->expects($this->once())
99  ->method('readFile')
100  ->with('merged/result.txt.dat')
101  ->will($this->returnValue('10'));
102  $assets = $this->getAssetsToMerge();
103  $this->mergerMock->expects($this->once())->method('merge')->with($assets, $this->resultAsset);
104  $this->targetDir->expects($this->once())->method('writeFile')->with('merged/result.txt.dat', '11');
105  $this->checksum->merge($assets, $this->resultAsset);
106  }
107 
108  public function testMergeMtimeUnchanged()
109  {
110  $this->targetDir->expects($this->once())
111  ->method('isExist')
112  ->with('merged/result.txt.dat')
113  ->will($this->returnValue(true));
114  $this->targetDir->expects($this->once())
115  ->method('readFile')
116  ->with('merged/result.txt.dat')
117  ->will($this->returnValue('11'));
118  $assets = $this->getAssetsToMerge();
119  $this->mergerMock->expects($this->never())->method('merge');
120  $this->targetDir->expects($this->never())->method('writeFile');
121  $this->checksum->merge($assets, $this->resultAsset);
122  }
123 
129  private function getAssetsToMerge()
130  {
131  $one = $this->createMock(\Magento\Framework\View\Asset\File::class);
132  $two = $this->createMock(\Magento\Framework\View\Asset\File::class);
133  $one->expects($this->never())
134  ->method('getSourceFile');
135  $two->expects($this->never())
136  ->method('getSourceFile');
137 
138  $this->assetSource->expects($this->exactly(2))
139  ->method('findSource')
140  ->withConsecutive([$one], [$two])
141  ->willReturnOnConsecutiveCalls('/dir/file/one.txt', '/dir/file/two.txt');
142 
143  $this->sourceDir->expects($this->exactly(2))
144  ->method('getRelativePath')
145  ->will($this->onConsecutiveCalls('file/one.txt', 'file/two.txt'));
146  $this->sourceDir->expects($this->exactly(2))->method('stat')->will($this->returnValue(['mtime' => '1']));
147  $this->resultAsset->expects($this->once())
148  ->method('getPath')
149  ->will($this->returnValue('merged/result.txt'));
150  return [$one, $two];
151  }
152 }
$filesystem