|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\OfflineShipping\Test\Unit\Model\Plugin\AsyncConfig\Model; |
| 9 | + |
| 10 | +use Magento\AsyncConfig\Model\AsyncConfigPublisher; |
| 11 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 12 | +use Magento\Framework\App\Request\Http; |
| 13 | +use Magento\Framework\App\RequestFactory; |
| 14 | +use Magento\Framework\Exception\FileSystemException; |
| 15 | +use Magento\Framework\Filesystem; |
| 16 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 17 | +use Magento\Framework\Filesystem\DriverInterface; |
| 18 | +use Magento\Framework\Math\Random; |
| 19 | +use Magento\OfflineShipping\Model\Plugin\AsyncConfig\Model\AsyncConfigPublisherPlugin; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +class AsyncConfigPublisherPluginTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var Filesystem|MockObject |
| 27 | + */ |
| 28 | + private Filesystem $filesystem; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var Random|MockObject |
| 32 | + */ |
| 33 | + private Random $rand; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var RequestFactory|MockObject |
| 37 | + */ |
| 38 | + private RequestFactory $requestFactory; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var AsyncConfigPublisherPlugin |
| 42 | + */ |
| 43 | + private AsyncConfigPublisherPlugin $plugin; |
| 44 | + |
| 45 | + protected function setUp(): void |
| 46 | + { |
| 47 | + $this->filesystem = $this->createMock(Filesystem::class); |
| 48 | + $this->rand = $this->createMock(Random::class); |
| 49 | + $this->requestFactory = $this->createMock(RequestFactory::class); |
| 50 | + $this->plugin = new AsyncConfigPublisherPlugin($this->filesystem, $this->rand, $this->requestFactory); |
| 51 | + |
| 52 | + parent::setUp(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @return void |
| 57 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 58 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 59 | + */ |
| 60 | + public function testBeforeSaveConfigDataNoImportFile(): void |
| 61 | + { |
| 62 | + $request = $this->createMock(Http::class); |
| 63 | + $request->expects($this->once())->method('getFiles')->willReturn([]); |
| 64 | + $this->requestFactory->expects($this->once())->method('create')->willReturn($request); |
| 65 | + |
| 66 | + $subject = $this->createMock(AsyncConfigPublisher::class); |
| 67 | + $params = ['test']; |
| 68 | + $this->assertSame([$params], $this->plugin->beforeSaveConfigData($subject, $params)); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return void |
| 73 | + * @throws FileSystemException |
| 74 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 75 | + */ |
| 76 | + public function testBeforeSaveConfigDataException(): void |
| 77 | + { |
| 78 | + $files['groups']['tablerate']['fields']['import']['value'] = [ |
| 79 | + 'tmp_name' => 'some/path/to/file/import.csv', |
| 80 | + 'name' => 'import.csv' |
| 81 | + ]; |
| 82 | + $request = $this->createMock(Http::class); |
| 83 | + $request->expects($this->once())->method('getFiles')->willReturn($files); |
| 84 | + $this->requestFactory->expects($this->once())->method('create')->willReturn($request); |
| 85 | + $driver = $this->createMock(DriverInterface::class); |
| 86 | + $driver->expects($this->once())->method('copy')->willReturn(false); |
| 87 | + $varDir = $this->createMock(WriteInterface::class); |
| 88 | + $varDir->expects($this->once())->method('getDriver')->willReturn($driver); |
| 89 | + $this->filesystem->expects($this->once()) |
| 90 | + ->method('getDirectoryWrite') |
| 91 | + ->with(DirectoryList::VAR_IMPORT_EXPORT) |
| 92 | + ->willReturn($varDir); |
| 93 | + $this->rand->expects($this->once())->method('getRandomString')->willReturn('123456'); |
| 94 | + |
| 95 | + $this->expectException(FileSystemException::class); |
| 96 | + $subject = $this->createMock(AsyncConfigPublisher::class); |
| 97 | + $config['groups']['tablerate']['fields']['import']['value']['name'] = 'import.csv'; |
| 98 | + $this->plugin->beforeSaveConfigData($subject, $config); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return void |
| 103 | + * @throws FileSystemException |
| 104 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 105 | + */ |
| 106 | + public function testBeforeSaveConfigDataSuccess(): void |
| 107 | + { |
| 108 | + $files['groups']['tablerate']['fields']['import']['value'] = [ |
| 109 | + 'tmp_name' => 'some/path/to/file/import.csv', |
| 110 | + 'name' => 'import.csv' |
| 111 | + ]; |
| 112 | + $request = $this->createMock(Http::class); |
| 113 | + $request->expects($this->once())->method('getFiles')->willReturn($files); |
| 114 | + $this->requestFactory->expects($this->once())->method('create')->willReturn($request); |
| 115 | + $driver = $this->createMock(DriverInterface::class); |
| 116 | + $driver->expects($this->once())->method('copy')->willReturn(true); |
| 117 | + $varDir = $this->createMock(WriteInterface::class); |
| 118 | + $varDir->expects($this->once())->method('getDriver')->willReturn($driver); |
| 119 | + $varDir->expects($this->exactly(2))->method('getAbsolutePath')->willReturn('some/path/to/file'); |
| 120 | + $this->filesystem->expects($this->once()) |
| 121 | + ->method('getDirectoryWrite') |
| 122 | + ->with(DirectoryList::VAR_IMPORT_EXPORT) |
| 123 | + ->willReturn($varDir); |
| 124 | + $this->rand->expects($this->once())->method('getRandomString')->willReturn('123456'); |
| 125 | + |
| 126 | + $subject = $this->createMock(AsyncConfigPublisher::class); |
| 127 | + $config['groups']['tablerate']['fields']['import']['value']['name'] = 'import.csv'; |
| 128 | + $files['groups']['tablerate']['fields']['import']['value']['name'] = '123456_import.csv'; |
| 129 | + $result['groups']['tablerate']['fields']['import']['value'] = [ |
| 130 | + 'name' => '123456_import.csv', |
| 131 | + 'full_path' => 'some/path/to/file' |
| 132 | + ]; |
| 133 | + $this->assertSame([$result], $this->plugin->beforeSaveConfigData($subject, $config)); |
| 134 | + } |
| 135 | +} |
0 commit comments