TablerateTest.php 4.1 KB

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