LinkProviderTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Test\Unit\Model;
  7. use Magento\Analytics\Api\Data\LinkInterface;
  8. use Magento\Analytics\Api\Data\LinkInterfaceFactory;
  9. use Magento\Analytics\Model\FileInfo;
  10. use Magento\Analytics\Model\FileInfoManager;
  11. use Magento\Analytics\Model\LinkProvider;
  12. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  13. use Magento\Framework\UrlInterface;
  14. use Magento\Store\Model\Store;
  15. use Magento\Store\Model\StoreManagerInterface;
  16. class LinkProviderTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var ObjectManagerHelper
  20. */
  21. private $objectManagerHelper;
  22. /**
  23. * @var LinkInterfaceFactory | \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $linkInterfaceFactoryMock;
  26. /**
  27. * @var FileInfoManager | \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $fileInfoManagerMock;
  30. /**
  31. * @var StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $storeManagerInterfaceMock;
  34. /**
  35. * @var LinkInterface | \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $linkInterfaceMock;
  38. /**
  39. * @var FileInfo | \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $fileInfoMock;
  42. /**
  43. * @var Store | \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $storeMock;
  46. /**
  47. * @var LinkProvider
  48. */
  49. private $linkProvider;
  50. /**
  51. * @return void
  52. */
  53. protected function setUp()
  54. {
  55. $this->linkInterfaceFactoryMock = $this->getMockBuilder(LinkInterfaceFactory::class)
  56. ->disableOriginalConstructor()
  57. ->setMethods(['create'])
  58. ->getMock();
  59. $this->fileInfoManagerMock = $this->getMockBuilder(FileInfoManager::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->storeManagerInterfaceMock = $this->getMockBuilder(StoreManagerInterface::class)
  63. ->getMockForAbstractClass();
  64. $this->linkInterfaceMock = $this->getMockBuilder(LinkInterface::class)
  65. ->getMockForAbstractClass();
  66. $this->fileInfoMock = $this->getMockBuilder(FileInfo::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->storeMock = $this->getMockBuilder(Store::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->objectManagerHelper = new ObjectManagerHelper($this);
  73. $this->linkProvider = $this->objectManagerHelper->getObject(
  74. LinkProvider::class,
  75. [
  76. 'linkFactory' => $this->linkInterfaceFactoryMock,
  77. 'fileInfoManager' => $this->fileInfoManagerMock,
  78. 'storeManager' => $this->storeManagerInterfaceMock
  79. ]
  80. );
  81. }
  82. public function testGet()
  83. {
  84. $baseUrl = 'http://magento.local/pub/media/';
  85. $fileInfoPath = 'analytics/data.tgz';
  86. $fileInitializationVector = 'er312esq23eqq';
  87. $this->fileInfoManagerMock->expects($this->once())
  88. ->method('load')
  89. ->willReturn($this->fileInfoMock);
  90. $this->linkInterfaceFactoryMock->expects($this->once())
  91. ->method('create')
  92. ->with(
  93. [
  94. 'initializationVector' => base64_encode($fileInitializationVector),
  95. 'url' => $baseUrl . $fileInfoPath
  96. ]
  97. )
  98. ->willReturn($this->linkInterfaceMock);
  99. $this->storeManagerInterfaceMock->expects($this->once())
  100. ->method('getStore')->willReturn($this->storeMock);
  101. $this->storeMock->expects($this->once())
  102. ->method('getBaseUrl')
  103. ->with(
  104. UrlInterface::URL_TYPE_MEDIA
  105. )
  106. ->willReturn($baseUrl);
  107. $this->fileInfoMock->expects($this->atLeastOnce())
  108. ->method('getPath')
  109. ->willReturn($fileInfoPath);
  110. $this->fileInfoMock->expects($this->atLeastOnce())
  111. ->method('getInitializationVector')
  112. ->willReturn($fileInitializationVector);
  113. $this->assertEquals($this->linkInterfaceMock, $this->linkProvider->get());
  114. }
  115. /**
  116. * @param string|null $fileInfoPath
  117. * @param string|null $fileInitializationVector
  118. *
  119. * @dataProvider fileNotReadyDataProvider
  120. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  121. * @expectedExceptionMessage File is not ready yet.
  122. */
  123. public function testFileNotReady($fileInfoPath, $fileInitializationVector)
  124. {
  125. $this->fileInfoManagerMock->expects($this->once())
  126. ->method('load')
  127. ->willReturn($this->fileInfoMock);
  128. $this->fileInfoMock->expects($this->once())
  129. ->method('getPath')
  130. ->willReturn($fileInfoPath);
  131. $this->fileInfoMock->expects($this->any())
  132. ->method('getInitializationVector')
  133. ->willReturn($fileInitializationVector);
  134. $this->linkProvider->get();
  135. }
  136. /**
  137. * @return array
  138. */
  139. public function fileNotReadyDataProvider()
  140. {
  141. return [
  142. [null, 'initVector'],
  143. ['path', null],
  144. ['', 'initVector'],
  145. ['path', ''],
  146. ['', ''],
  147. [null, null]
  148. ];
  149. }
  150. }