LogEntryExportTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Test\Model;
  7. use Magento\Framework\Exception\NotFoundException;
  8. use Magento\Framework\Filesystem;
  9. use Magento\Framework\Filesystem\Directory\WriteInterface as DirectoryWriteInterface;
  10. use Magento\Framework\Filesystem\File\WriteInterface as FileWriteInterface;
  11. use Magento\Framework\Stdlib\DateTime;
  12. use Vertex\Tax\Api\Data\LogEntryInterface;
  13. use Vertex\Tax\Model\Data\LogEntry;
  14. use Vertex\Tax\Model\LogEntryExport;
  15. use Vertex\Tax\Test\Unit\TestCase;
  16. /**
  17. * Test that LogEntryExport can work with its filesystem.
  18. */
  19. class LogEntryExportTest extends TestCase
  20. {
  21. /** @var \PHPUnit_Framework_MockObject_MockObject|DateTime */
  22. private $dateTimeMock;
  23. /** @var \PHPUnit_Framework_MockObject_MockObject|DirectoryWriteInterface */
  24. private $directoryWriteMock;
  25. /** @var \PHPUnit_Framework_MockObject_MockObject|Filesystem */
  26. private $fileSystemMock;
  27. /** @var \PHPUnit_Framework_MockObject_MockObject|FileWriteInterface */
  28. private $fileWriteMock;
  29. /** @var LogEntryExport */
  30. private $logEntryExport;
  31. /**
  32. * Perform test setup.
  33. */
  34. protected function setUp()
  35. {
  36. parent::setUp();
  37. $this->fileSystemMock = $this->createMock(Filesystem::class);
  38. $this->dateTimeMock = $this->createMock(DateTime::class);
  39. $this->directoryWriteMock = $this->createMock(DirectoryWriteInterface::class);
  40. $this->fileWriteMock = $this->createMock(FileWriteInterface::class);
  41. $this->dateTimeMock->method('formatDate')
  42. ->willReturn('2018-06-01');
  43. $this->directoryWriteMock->method('openFile')
  44. ->willReturn($this->fileWriteMock);
  45. $this->fileSystemMock->method('getDirectoryWrite')
  46. ->willReturn($this->directoryWriteMock);
  47. $this->logEntryExport = $this->getObject(
  48. LogEntryExport::class,
  49. [
  50. 'fileSystem' => $this->fileSystemMock,
  51. 'dateTime' => $this->dateTimeMock,
  52. ]
  53. );
  54. }
  55. /**
  56. * Test the expected file path on export close.
  57. *
  58. * @covers \Vertex\Tax\Model\LogEntryExport::close()
  59. */
  60. public function testExpectedFilePathOnClose()
  61. {
  62. $expectedBasePath = '/path/to/output/';
  63. $expectedFilename = 'test.csv';
  64. $this->directoryWriteMock->expects($this->any())
  65. ->method('getAbsolutePath')
  66. ->willReturn($expectedBasePath);
  67. $this->logEntryExport->open($expectedFilename);
  68. $actualFilePath = $this->logEntryExport->close();
  69. $this->assertEquals($expectedBasePath . $expectedFilename, $actualFilePath);
  70. }
  71. /**
  72. * Test an unexpected file path on export close.
  73. *
  74. * @covers \Vertex\Tax\Model\LogEntryExport::close()
  75. */
  76. public function testUnexpectedFilePathOnClose()
  77. {
  78. $expectedBasePath = '/path/to/output/';
  79. $unexpectedBasePath = '/path/to/another/output/';
  80. $expectedFilename = 'test.csv';
  81. $this->directoryWriteMock->expects($this->any())
  82. ->method('getAbsolutePath')
  83. ->willReturn($expectedBasePath);
  84. $this->logEntryExport->open($expectedFilename);
  85. $actualFilePath = $this->logEntryExport->close();
  86. $this->assertNotEquals($unexpectedBasePath . $expectedFilename, $actualFilePath);
  87. }
  88. /**
  89. * Test that the file write stream can be acquired.
  90. *
  91. * @covers \Vertex\Tax\Model\LogEntryExport::open()
  92. */
  93. public function testStreamOpenSuccess()
  94. {
  95. $expectedBasePath = '/path/to/output/';
  96. $this->directoryWriteMock->expects($this->any())
  97. ->method('getAbsolutePath')
  98. ->willReturn($expectedBasePath);
  99. $this->directoryWriteMock->expects($this->once())
  100. ->method('openFile')
  101. ->willReturn($this->fileWriteMock);
  102. $this->logEntryExport->open();
  103. }
  104. /**
  105. * Test that the export does not re-open a file if already working on an existing file.
  106. *
  107. * @covers \Vertex\Tax\Model\LogEntryExport::open()
  108. */
  109. public function testBlockOpenWhenStreamExists()
  110. {
  111. $this->directoryWriteMock->expects($this->once())
  112. ->method('openFile')
  113. ->willReturn($this->fileWriteMock);
  114. $this->logEntryExport->open(); // will succeed
  115. $this->logEntryExport->open(); // will be blocked
  116. }
  117. /**
  118. * Test that archiving fails appropriately when no stream is available.
  119. *
  120. * @covers \Vertex\Tax\Model\LogEntryExport::write()
  121. */
  122. public function testExpectedExceptionOnWriteFailure()
  123. {
  124. $expectedException = NotFoundException::class;
  125. $actualException = '';
  126. try {
  127. $this->logEntryExport->write(
  128. $this->createMock(LogEntryInterface::class)
  129. );
  130. } catch (\Exception $error) {
  131. $actualException = get_class($error);
  132. }
  133. $this->assertEquals($expectedException, $actualException);
  134. }
  135. /**
  136. * Provider for {@see testWriteSucceedsWithAnyDataset}.
  137. *
  138. * @return array
  139. */
  140. public function provideMockEntryData()
  141. {
  142. return [
  143. [
  144. 'entryData' => [
  145. 'request_id' => 1,
  146. 'request_type' => 'tax_area_lookup',
  147. 'quote_id' => 1,
  148. 'order_id' => 1,
  149. 'total_tax' => 2.00,
  150. 'source_path' => '',
  151. 'tax_area_id' => '',
  152. 'sub_total' => 10.00,
  153. 'total' => 12.00,
  154. 'lookup_result' => '',
  155. 'request_date' => '',
  156. 'request_xml' => '',
  157. 'response_xml' => '',
  158. ],
  159. ]
  160. ];
  161. }
  162. }