|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\OfflineShipping\Test\Unit\Model\ResourceModel\Carrier; |
| 8 | + |
| 9 | +use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate; |
| 10 | +use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Import; |
| 11 | +use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQueryFactory; |
| 12 | + |
| 13 | +/** |
| 14 | + * Unit test for Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate |
| 15 | + * |
| 16 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 17 | + */ |
| 18 | +class TablerateTest extends \PHPUnit\Framework\TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var Tablerate |
| 22 | + */ |
| 23 | + private $model; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + private $storeManagerMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + private $filesystemMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + private $resource; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 42 | + */ |
| 43 | + private $importMock; |
| 44 | + |
| 45 | + protected function setUp() |
| 46 | + { |
| 47 | + $contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class); |
| 48 | + $loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class); |
| 49 | + $coreConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); |
| 50 | + $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class); |
| 51 | + $carrierTablerateMock = $this->createMock(\Magento\OfflineShipping\Model\Carrier\Tablerate::class); |
| 52 | + $this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class); |
| 53 | + $this->importMock = $this->createMock(Import::class); |
| 54 | + $rateQueryFactoryMock = $this->createMock(RateQueryFactory::class); |
| 55 | + $this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class); |
| 56 | + |
| 57 | + $contextMock->expects($this->once())->method('getResources')->willReturn($this->resource); |
| 58 | + |
| 59 | + $this->model = new Tablerate( |
| 60 | + $contextMock, |
| 61 | + $loggerMock, |
| 62 | + $coreConfigMock, |
| 63 | + $this->storeManagerMock, |
| 64 | + $carrierTablerateMock, |
| 65 | + $this->filesystemMock, |
| 66 | + $this->importMock, |
| 67 | + $rateQueryFactoryMock |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + public function testUploadAndImport() |
| 72 | + { |
| 73 | + $_FILES['groups']['tmp_name']['tablerate']['fields']['import']['value'] = 'some/path/to/file'; |
| 74 | + $object = $this->createPartialMock( |
| 75 | + \Magento\OfflineShipping\Model\Config\Backend\Tablerate::class, |
| 76 | + ['getScopeId'] |
| 77 | + ); |
| 78 | + |
| 79 | + $websiteMock = $this->createMock(\Magento\Store\Api\Data\WebsiteInterface::class); |
| 80 | + $directoryReadMock = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadInterface::class); |
| 81 | + $fileReadMock = $this->createMock(\Magento\Framework\Filesystem\File\ReadInterface::class); |
| 82 | + $connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class); |
| 83 | + |
| 84 | + $this->storeManagerMock->expects($this->once())->method('getWebsite')->willReturn($websiteMock); |
| 85 | + $object->expects($this->once())->method('getScopeId')->willReturn(1); |
| 86 | + $websiteMock->expects($this->once())->method('getId')->willReturn(1); |
| 87 | + |
| 88 | + $this->filesystemMock->expects($this->once())->method('getDirectoryReadByPath') |
| 89 | + ->with('some/path/to')->willReturn($directoryReadMock); |
| 90 | + $directoryReadMock->expects($this->once())->method('openFile') |
| 91 | + ->with('file')->willReturn($fileReadMock); |
| 92 | + |
| 93 | + $this->resource->expects($this->once())->method('getConnection')->willReturn($connectionMock); |
| 94 | + |
| 95 | + $connectionMock->expects($this->once())->method('beginTransaction'); |
| 96 | + $connectionMock->expects($this->once())->method('delete'); |
| 97 | + $connectionMock->expects($this->once())->method('commit'); |
| 98 | + |
| 99 | + $this->importMock->expects($this->once())->method('getColumns')->willReturn([]); |
| 100 | + $this->importMock->expects($this->once())->method('getData')->willReturn([]); |
| 101 | + |
| 102 | + $this->model->uploadAndImport($object); |
| 103 | + unset($_FILES['groups']); |
| 104 | + } |
| 105 | +} |
0 commit comments