BundleTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Bundle;
  8. /**
  9. * Unit test for Magento\Framework\View\Asset\Bundle
  10. */
  11. class BundleTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\View\Asset\Bundle
  15. */
  16. protected $bundle;
  17. /**
  18. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $filesystemMock;
  21. /**
  22. * @var \Magento\Framework\View\Asset\Bundle\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $bundleConfigMock;
  25. /**
  26. * @var \Magento\Framework\View\Asset\Minification|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $minificationMock;
  29. /**
  30. * {@inheritDoc}
  31. */
  32. protected function setUp()
  33. {
  34. $this->filesystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->bundleConfigMock = $this->getMockBuilder(\Magento\Framework\View\Asset\Bundle\ConfigInterface::class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->minificationMock = $this->getMockBuilder(\Magento\Framework\View\Asset\Minification::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->bundle = new Bundle(
  44. $this->filesystemMock,
  45. $this->bundleConfigMock,
  46. $this->minificationMock
  47. );
  48. }
  49. /**
  50. * @return void
  51. * @covers \Magento\Framework\View\Asset\Bundle::getAssetKey
  52. * @covers \Magento\Framework\View\Asset\Bundle::save
  53. */
  54. public function testMinSuffix()
  55. {
  56. $this->minificationMock
  57. ->expects($this->any())
  58. ->method('addMinifiedSign')
  59. ->withConsecutive(
  60. ['onefile.js'],
  61. ['onefile.js'],
  62. ['path-to-theme/js/bundle/bundle0.js']
  63. )
  64. ->willReturnOnConsecutiveCalls(
  65. 'onefile.min.js',
  66. 'onefile.min.js',
  67. 'path-to-theme/js/bundle/bundle0.min.js'
  68. );
  69. $contextMock = $this->getMockBuilder(\Magento\Framework\View\Asset\File\FallbackContext::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $contextMock
  73. ->expects($this->any())
  74. ->method('getAreaCode')
  75. ->willReturn('area');
  76. $contextMock
  77. ->expects($this->any())
  78. ->method('getThemePath')
  79. ->willReturn('theme-path');
  80. $contextMock
  81. ->expects($this->any())
  82. ->method('getLocale')
  83. ->willReturn('locale');
  84. $contextMock
  85. ->expects($this->any())
  86. ->method('getPath')
  87. ->willReturn('path-to-theme');
  88. $assetMock = $this->getMockBuilder(\Magento\Framework\View\Asset\LocalInterface::class)
  89. ->setMethods(['getContentType', 'getContext'])
  90. ->getMockForAbstractClass();
  91. $assetMock
  92. ->expects($this->any())
  93. ->method('getContext')
  94. ->willReturn($contextMock);
  95. $assetMock
  96. ->expects($this->any())
  97. ->method('getContentType')
  98. ->willReturn('js');
  99. $assetMock
  100. ->expects($this->any())
  101. ->method('getFilePath')
  102. ->willReturn('onefile.js');
  103. $writeMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
  104. ->getMockForAbstractClass();
  105. $writeMock
  106. ->expects($this->once())
  107. ->method('delete')
  108. ->with('path-to-theme' . DIRECTORY_SEPARATOR . \Magento\Framework\View\Asset\Bundle\Manager::BUNDLE_JS_DIR);
  109. $writeMock
  110. ->expects($this->once())
  111. ->method('writeFile')
  112. ->with('path-to-theme/js/bundle/bundle0.min.js', $this->stringContains('onefile.min.js'));
  113. $this->filesystemMock
  114. ->expects($this->any())
  115. ->method('getDirectoryWrite')
  116. ->willReturn($writeMock);
  117. $this->bundle->addAsset($assetMock);
  118. $this->bundle->flush();
  119. }
  120. }