123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Analytics\Test\Unit\Model;
- use Magento\Analytics\Model\Cryptographer;
- use Magento\Analytics\Model\EncodedContext;
- use Magento\Analytics\Model\ExportDataHandler;
- use Magento\Analytics\Model\FileRecorder;
- use Magento\Analytics\Model\ReportWriterInterface;
- use Magento\Framework\Archive;
- use Magento\Framework\Filesystem;
- use Magento\Framework\Filesystem\Directory\WriteInterface;
- use Magento\Framework\Filesystem\DirectoryList;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
- class ExportDataHandlerTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
- */
- private $filesystemMock;
- /**
- * @var Archive|\PHPUnit_Framework_MockObject_MockObject
- */
- private $archiveMock;
- /**
- * @var ReportWriterInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $reportWriterMock;
- /**
- * @var Cryptographer|\PHPUnit_Framework_MockObject_MockObject
- */
- private $cryptographerMock;
- /**
- * @var FileRecorder|\PHPUnit_Framework_MockObject_MockObject
- */
- private $fileRecorderMock;
- /**
- * @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $directoryMock;
- /**
- * @var EncodedContext|\PHPUnit_Framework_MockObject_MockObject
- */
- private $encodedContextMock;
- /**
- * @var ObjectManagerHelper
- */
- private $objectManagerHelper;
- /**
- * @var ExportDataHandler
- */
- private $exportDataHandler;
- /**
- * @var string
- */
- private $subdirectoryPath = 'analytics/';
- /**
- * @var string
- */
- private $archiveName = 'data.tgz';
- /**
- * @return void
- */
- protected function setUp()
- {
- $this->filesystemMock = $this->getMockBuilder(Filesystem::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->archiveMock = $this->getMockBuilder(Archive::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->reportWriterMock = $this->getMockBuilder(ReportWriterInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->cryptographerMock = $this->getMockBuilder(Cryptographer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->fileRecorderMock = $this->getMockBuilder(FileRecorder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->directoryMock = $this->getMockBuilder(WriteInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->encodedContextMock = $this->getMockBuilder(EncodedContext::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->objectManagerHelper = new ObjectManagerHelper($this);
- $this->exportDataHandler = $this->objectManagerHelper->getObject(
- ExportDataHandler::class,
- [
- 'filesystem' => $this->filesystemMock,
- 'archive' => $this->archiveMock,
- 'reportWriter' => $this->reportWriterMock,
- 'cryptographer' => $this->cryptographerMock,
- 'fileRecorder' => $this->fileRecorderMock,
- 'subdirectoryPath' => $this->subdirectoryPath,
- 'archiveName' => $this->archiveName,
- ]
- );
- }
- /**
- * @param bool $isArchiveSourceDirectory
- * @dataProvider prepareExportDataDataProvider
- */
- public function testPrepareExportData($isArchiveSourceDirectory)
- {
- $tmpFilesDirectoryPath = $this->subdirectoryPath . 'tmp/';
- $archiveRelativePath = $this->subdirectoryPath . $this->archiveName;
- $archiveSource = $isArchiveSourceDirectory ? (__DIR__) : '/tmp/' . $tmpFilesDirectoryPath;
- $archiveAbsolutePath = '/tmp/' . $archiveRelativePath;
- $this->filesystemMock
- ->expects($this->once())
- ->method('getDirectoryWrite')
- ->with(DirectoryList::SYS_TMP)
- ->willReturn($this->directoryMock);
- $this->directoryMock
- ->expects($this->exactly(4))
- ->method('delete')
- ->withConsecutive(
- [$tmpFilesDirectoryPath],
- [$archiveRelativePath]
- );
- $this->directoryMock
- ->expects($this->exactly(4))
- ->method('getAbsolutePath')
- ->withConsecutive(
- [$tmpFilesDirectoryPath],
- [$tmpFilesDirectoryPath],
- [$archiveRelativePath],
- [$archiveRelativePath]
- )
- ->willReturnOnConsecutiveCalls(
- $archiveSource,
- $archiveSource,
- $archiveAbsolutePath,
- $archiveAbsolutePath
- );
- $this->reportWriterMock
- ->expects($this->once())
- ->method('write')
- ->with($this->directoryMock, $tmpFilesDirectoryPath);
- $this->directoryMock
- ->expects($this->exactly(2))
- ->method('isExist')
- ->withConsecutive(
- [$tmpFilesDirectoryPath],
- [$archiveRelativePath]
- )
- ->willReturnOnConsecutiveCalls(
- true,
- true
- );
- $this->directoryMock
- ->expects($this->once())
- ->method('create')
- ->with(dirname($archiveRelativePath));
- $this->archiveMock
- ->expects($this->once())
- ->method('pack')
- ->with(
- $archiveSource,
- $archiveAbsolutePath,
- $isArchiveSourceDirectory
- );
- $fileContent = 'Some text';
- $this->directoryMock
- ->expects($this->once())
- ->method('readFile')
- ->with($archiveRelativePath)
- ->willReturn($fileContent);
- $this->cryptographerMock
- ->expects($this->once())
- ->method('encode')
- ->with($fileContent)
- ->willReturn($this->encodedContextMock);
- $this->fileRecorderMock
- ->expects($this->once())
- ->method('recordNewFile')
- ->with($this->encodedContextMock);
- $this->assertTrue($this->exportDataHandler->prepareExportData());
- }
- /**
- * @return array
- */
- public function prepareExportDataDataProvider()
- {
- return [
- 'Data source for archive is directory' => [true],
- 'Data source for archive isn\'t directory' => [false],
- ];
- }
- /**
- * @return void
- * @expectedException \Magento\Framework\Exception\LocalizedException
- */
- public function testPrepareExportDataWithLocalizedException()
- {
- $tmpFilesDirectoryPath = $this->subdirectoryPath . 'tmp/';
- $archivePath = $this->subdirectoryPath . $this->archiveName;
- $this->filesystemMock
- ->expects($this->once())
- ->method('getDirectoryWrite')
- ->with(DirectoryList::SYS_TMP)
- ->willReturn($this->directoryMock);
- $this->reportWriterMock
- ->expects($this->once())
- ->method('write')
- ->with($this->directoryMock, $tmpFilesDirectoryPath);
- $this->directoryMock
- ->expects($this->exactly(3))
- ->method('delete')
- ->withConsecutive(
- [$tmpFilesDirectoryPath],
- [$tmpFilesDirectoryPath],
- [$archivePath]
- );
- $this->directoryMock
- ->expects($this->exactly(2))
- ->method('getAbsolutePath')
- ->with($tmpFilesDirectoryPath);
- $this->directoryMock
- ->expects($this->once())
- ->method('isExist')
- ->with($tmpFilesDirectoryPath)
- ->willReturn(false);
- $this->assertNull($this->exportDataHandler->prepareExportData());
- }
- }
|