FileTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Test\Unit\Asset;
  7. use Magento\Framework\View\Asset\File;
  8. class FileTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\View\Asset\Source|\PHPUnit_Framework_MockObject_MockObject
  12. */
  13. private $source;
  14. /**
  15. * @var \Magento\Framework\View\Asset\ContextInterface|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $context;
  18. /**
  19. * @var \Magento\Framework\View\Asset\Minification|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $minificationMock;
  22. /**
  23. * @var File
  24. */
  25. private $object;
  26. protected function setUp()
  27. {
  28. $this->source = $this->createMock(\Magento\Framework\View\Asset\Source::class);
  29. $this->context = $this->getMockForAbstractClass(\Magento\Framework\View\Asset\ContextInterface::class);
  30. $this->minificationMock = $this->getMockBuilder(\Magento\Framework\View\Asset\Minification::class)
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $this->minificationMock
  34. ->expects($this->any())
  35. ->method('addMinifiedSign')
  36. ->willReturnArgument(0);
  37. $this->object = new File(
  38. $this->source,
  39. $this->context,
  40. 'dir/file.css',
  41. 'Magento_Module',
  42. 'css',
  43. $this->minificationMock
  44. );
  45. }
  46. public function testGetUrl()
  47. {
  48. $this->context->expects($this->once())->method('getBaseUrl')->will($this->returnValue('http://example.com/'));
  49. $this->context->expects($this->once())->method('getPath')->will($this->returnValue('static'));
  50. $this->assertEquals('http://example.com/static/Magento_Module/dir/file.css', $this->object->getUrl());
  51. }
  52. public function testGetContentType()
  53. {
  54. $this->assertEquals('css', $this->object->getContentType());
  55. $object = new File($this->source, $this->context, '', '', 'type', $this->minificationMock);
  56. $this->assertEquals('type', $object->getContentType());
  57. }
  58. /**
  59. * @param string $contextPath
  60. * @param string $module
  61. * @param string $filePath
  62. * @param string $expected
  63. * @dataProvider getPathDataProvider
  64. */
  65. public function testGetPath($contextPath, $module, $filePath, $expected)
  66. {
  67. $this->context->expects($this->once())->method('getPath')->will($this->returnValue($contextPath));
  68. $object = new File($this->source, $this->context, $filePath, $module, '', $this->minificationMock);
  69. $this->assertEquals($expected, $object->getPath());
  70. }
  71. /**
  72. * @return array
  73. */
  74. public function getPathDataProvider()
  75. {
  76. return [
  77. ['', '', '', ''],
  78. ['', '', 'c/d', 'c/d'],
  79. ['', 'b', '', 'b'],
  80. ['', 'b', 'c/d', 'b/c/d'],
  81. ['a', '', '', 'a'],
  82. ['a', '', 'c/d', 'a/c/d'],
  83. ['a', 'b', '', 'a/b'],
  84. ['a', 'b', 'c/d', 'a/b/c/d'],
  85. ];
  86. }
  87. public function testGetSourceFile()
  88. {
  89. $this->source->expects($this->once())
  90. ->method('getFile')
  91. ->with($this->object)
  92. ->will($this->returnValue('result'));
  93. $this->assertEquals('result', $this->object->getSourceFile());
  94. $this->assertEquals('result', $this->object->getSourceFile()); // second time to assert in-memory caching
  95. }
  96. /**
  97. * @expectedException \LogicException
  98. * @expectedExceptionMessage Unable to resolve the source file for 'context/Magento_Module/dir/file.css'
  99. */
  100. public function testGetSourceFileMissing()
  101. {
  102. $this->context->expects($this->once())->method('getPath')->will($this->returnValue('context'));
  103. $this->source->expects($this->once())->method('getFile')->will($this->returnValue(false));
  104. $this->object->getSourceFile();
  105. }
  106. /**
  107. * @param string $content
  108. *
  109. * @dataProvider getContentDataProvider
  110. */
  111. public function testGetContent($content)
  112. {
  113. $this->source->expects($this->exactly(2))
  114. ->method('getContent')
  115. ->with($this->object)
  116. ->will($this->returnValue($content));
  117. $this->assertEquals($content, $this->object->getContent());
  118. $this->assertEquals($content, $this->object->getContent()); // no in-memory caching for content
  119. }
  120. /**
  121. * @return array
  122. */
  123. public function getContentDataProvider()
  124. {
  125. return [
  126. 'normal content' => ['content'],
  127. 'empty content' => [''],
  128. ];
  129. }
  130. /**
  131. * @expectedException \Magento\Framework\View\Asset\File\NotFoundException
  132. * @expectedExceptionMessage Unable to get content for 'Magento_Module/dir/file.css'
  133. */
  134. public function testGetContentNotFound()
  135. {
  136. $this->source->expects($this->once())
  137. ->method('getContent')
  138. ->with($this->object)
  139. ->will($this->returnValue(false));
  140. $this->object->getContent();
  141. }
  142. public function testSimpleGetters()
  143. {
  144. $this->assertEquals('dir/file.css', $this->object->getFilePath());
  145. $this->assertSame($this->context, $this->object->getContext());
  146. $this->assertEquals('Magento_Module', $this->object->getModule());
  147. }
  148. }