Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TablerateTest.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQueryFactory;
12 
18 class TablerateTest extends \PHPUnit\Framework\TestCase
19 {
23  private $model;
24 
28  private $storeManagerMock;
29 
33  private $filesystemMock;
34 
38  private $resource;
39 
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 }