FileManagerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Translation\Test\Unit\Model;
  7. use Magento\Translation\Model\FileManager;
  8. class FileManagerTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Translation\Model\FileManager|\PHPUnit_Framework_MockObject_MockObject
  12. */
  13. private $model;
  14. /**
  15. * @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $assetRepoMock;
  18. /**
  19. * @var \Magento\Framework\App\Filesystem\DirectoryList|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $directoryListMock;
  22. /**
  23. * @var \Magento\Framework\Filesystem\Driver\File|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $driverFileMock;
  26. protected function setUp()
  27. {
  28. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  29. $this->assetRepoMock = $this->createMock(\Magento\Framework\View\Asset\Repository::class);
  30. $this->directoryListMock = $this->createMock(\Magento\Framework\App\Filesystem\DirectoryList::class);
  31. $this->driverFileMock = $this->createMock(\Magento\Framework\Filesystem\Driver\File::class);
  32. $this->model = $objectManager->getObject(
  33. \Magento\Translation\Model\FileManager::class,
  34. [
  35. 'assetRepo' => $this->assetRepoMock,
  36. 'directoryList' => $this->directoryListMock,
  37. 'driverFile' => $this->driverFileMock,
  38. ]
  39. );
  40. }
  41. public function testCreateTranslateConfigAsset()
  42. {
  43. $path = 'relative path';
  44. $expectedPath = $path . '/' . FileManager::TRANSLATION_CONFIG_FILE_NAME;
  45. $fileMock = $this->createMock(\Magento\Framework\View\Asset\File::class);
  46. $contextMock = $this->getMockForAbstractClass(
  47. \Magento\Framework\View\Asset\ContextInterface::class,
  48. [],
  49. '',
  50. true,
  51. true,
  52. true,
  53. ['getPath']
  54. );
  55. $this->assetRepoMock->expects($this->once())->method('getStaticViewFileContext')->willReturn($contextMock);
  56. $contextMock->expects($this->once())->method('getPath')->willReturn($path);
  57. $this->assetRepoMock
  58. ->expects($this->once())
  59. ->method('createArbitrary')
  60. ->with($expectedPath, '')
  61. ->willReturn($fileMock);
  62. $this->assertSame($fileMock, $this->model->createTranslateConfigAsset());
  63. }
  64. public function testGetTranslationFileTimestamp()
  65. {
  66. $path = 'path';
  67. $contextMock = $this->getMockForAbstractClass(
  68. \Magento\Framework\View\Asset\ContextInterface::class,
  69. [],
  70. '',
  71. true,
  72. true,
  73. true,
  74. ['getPath']
  75. );
  76. $this->assetRepoMock->expects($this->atLeastOnce())
  77. ->method('getStaticViewFileContext')
  78. ->willReturn($contextMock);
  79. $contextMock->expects($this->atLeastOnce())->method('getPath')->willReturn($path);
  80. $this->directoryListMock->expects($this->atLeastOnce())->method('getPath')->willReturn($path);
  81. $this->driverFileMock->expects($this->once())
  82. ->method('isExists')
  83. ->with('path/path/js-translation.json')
  84. ->willReturn(true);
  85. $this->driverFileMock->expects($this->once())->method('stat')->willReturn(['mtime' => 1445736974]);
  86. $this->assertEquals(1445736974, $this->model->getTranslationFileTimestamp());
  87. }
  88. public function testGetTranslationFilePath()
  89. {
  90. $path = 'path';
  91. $contextMock = $this->getMockForAbstractClass(
  92. \Magento\Framework\View\Asset\ContextInterface::class,
  93. [],
  94. '',
  95. true,
  96. true,
  97. true,
  98. ['getPath']
  99. );
  100. $this->assetRepoMock->expects($this->atLeastOnce())
  101. ->method('getStaticViewFileContext')
  102. ->willReturn($contextMock);
  103. $contextMock->expects($this->atLeastOnce())->method('getPath')->willReturn($path);
  104. $this->assertEquals($path, $this->model->getTranslationFilePath());
  105. }
  106. }