Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MergedTest.php
Go to the documentation of this file.
1 <?php
7 
11 use Psr\Log\LoggerInterface;
12 use Magento\Framework\View\Asset\Repository as AssetRepository;
16 
20 class MergedTest extends \PHPUnit\Framework\TestCase
21 {
25  private $logger;
26 
30  private $mergeStrategy;
31 
35  private $assetJsOne;
36 
40  private $assetJsTwo;
41 
45  private $assetRepo;
46 
50  private $versionStorage;
51 
52  protected function setUp()
53  {
54  $this->assetJsOne = $this->getMockForAbstractClass(MergeableInterface::class);
55  $this->assetJsOne->expects($this->any())
56  ->method('getContentType')
57  ->willReturn('js');
58  $this->assetJsOne->expects($this->any())
59  ->method('getPath')
60  ->willReturn('script_one.js');
61 
62  $this->assetJsTwo = $this->getMockForAbstractClass(MergeableInterface::class);
63  $this->assetJsTwo->expects($this->any())
64  ->method('getContentType')
65  ->willReturn('js');
66  $this->assetJsTwo->expects($this->any())
67  ->method('getPath')
68  ->willReturn('script_two.js');
69 
70  $this->logger = $this->createMock(LoggerInterface::class);
71  $this->mergeStrategy = $this->createMock(MergeStrategyInterface::class);
72  $this->assetRepo = $this->getMockBuilder(AssetRepository::class)
73  ->disableOriginalConstructor()
74  ->getMock();
75  $this->versionStorage = $this->createMock(StorageInterface::class);
76  }
77 
83  {
84  new \Magento\Framework\View\Asset\Merged(
85  $this->logger,
86  $this->mergeStrategy,
87  $this->assetRepo,
88  [],
89  $this->versionStorage
90  );
91  }
92 
98  {
99  $assetUrl = new \Magento\Framework\View\Asset\Remote('http://example.com/style.css', 'css');
100 
101  (new ObjectManager($this))->getObject(Merged::class, [
102  'logger' => $this->logger,
103  'mergeStrategy' => $this->mergeStrategy,
104  'assetRepo' => $this->assetRepo,
105  'assets' => [$this->assetJsOne, $assetUrl],
106  'versionStorage' => $this->versionStorage,
107  ]);
108  }
109 
115  {
116  $assetCss = $this->getMockForAbstractClass(MergeableInterface::class);
117  $assetCss->expects($this->any())
118  ->method('getContentType')
119  ->willReturn('css');
120 
121  (new ObjectManager($this))->getObject(Merged::class, [
122  'logger' => $this->logger,
123  'mergeStrategy' => $this->mergeStrategy,
124  'assetRepo' => $this->assetRepo,
125  'assets' => [$this->assetJsOne, $assetCss],
126  'versionStorage' => $this->versionStorage,
127  ]);
128  }
129 
130  public function testIteratorInterfaceMerge()
131  {
132  $assets = [$this->assetJsOne, $this->assetJsTwo];
133 
134  $this->logger->expects($this->never())->method('critical');
135 
137  $merged = (new ObjectManager($this))->getObject(Merged::class, [
138  'logger' => $this->logger,
139  'mergeStrategy' => $this->mergeStrategy,
140  'assetRepo' => $this->assetRepo,
141  'assets' => $assets,
142  'versionStorage' => $this->versionStorage,
143  ]);
144 
145  $mergedAsset = $this->createMock(\Magento\Framework\View\Asset\File::class);
146  $this->mergeStrategy
147  ->expects($this->once())
148  ->method('merge')
149  ->with($assets, $mergedAsset)
150  ->willReturn(null);
151  $this->assetRepo->expects($this->once())
152  ->method('createArbitrary')
153  ->willReturn($mergedAsset);
154  $expectedResult = [$mergedAsset];
155 
156  $this->assertIteratorEquals($expectedResult, $merged);
157  $this->assertIteratorEquals($expectedResult, $merged); // ensure merging happens only once
158  }
159 
160  public function testIteratorInterfaceMergeFailure()
161  {
162  $mergeError = new \Exception('File not found');
163  $assetBroken = $this->getMockForAbstractClass(MergeableInterface::class);
164  $assetBroken->expects($this->any())
165  ->method('getContentType')
166  ->willReturn('js');
167  $assetBroken->expects($this->any())
168  ->method('getPath')
169  ->willThrowException($mergeError);
170 
172  $merged = (new ObjectManager($this))->getObject(Merged::class, [
173  'logger' => $this->logger,
174  'mergeStrategy' => $this->mergeStrategy,
175  'assetRepo' => $this->assetRepo,
176  'assets' => [$this->assetJsOne, $this->assetJsTwo, $assetBroken],
177  'versionStorage' => $this->versionStorage,
178  ]);
179 
180  $this->logger->expects($this->once())->method('critical')->with($this->identicalTo($mergeError));
181 
182  $expectedResult = [$this->assetJsOne, $this->assetJsTwo, $assetBroken];
183  $this->assertIteratorEquals($expectedResult, $merged);
184  $this->assertIteratorEquals($expectedResult, $merged); // ensure merging attempt happens only once
185  }
186 
193  protected function assertIteratorEquals(array $expectedItems, \Iterator $actual)
194  {
195  $actualItems = [];
196  foreach ($actual as $actualItem) {
197  $actualItems[] = $actualItem;
198  }
199  $this->assertEquals($expectedItems, $actualItems);
200  }
201 }
assertIteratorEquals(array $expectedItems, \Iterator $actual)
Definition: MergedTest.php:193