|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Meta\Promotions\Test\Unit\Controller\Adminhtml\Ajax; |
| 5 | + |
| 6 | +use Meta\BusinessExtension\Helper\FBEHelper; |
| 7 | +use Meta\Promotions\Model\Promotion\Feed\Uploader; |
| 8 | +use Meta\BusinessExtension\Model\System\Config as SystemConfig; |
| 9 | +use Magento\Backend\App\Action\Context; |
| 10 | +use Magento\Framework\Controller\Result\JsonFactory; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 13 | +use Meta\Promotions\Controller\Adminhtml\Ajax\PromotionFeedUpload; |
| 14 | +use Magento\Store\Api\Data\StoreInterface; |
| 15 | +use \Magento\Store\Model\StoreManagerInterface; |
| 16 | +use Magento\Framework\App\RequestInterface; |
| 17 | + |
| 18 | +class PromotionFeedUploadTest extends TestCase |
| 19 | +{ |
| 20 | + private $fbeHelperMock; |
| 21 | + private $systemConfigMock; |
| 22 | + private $uploaderMock; |
| 23 | + private $contextMock; |
| 24 | + private $resultJsonMock; |
| 25 | + private $storeMock; |
| 26 | + private $storeManagerMock; |
| 27 | + private $requestMock; |
| 28 | + private $object; |
| 29 | + private $subject; |
| 30 | + |
| 31 | + public function setUp(): void |
| 32 | + { |
| 33 | + $this->contextMock = $this->getMockBuilder(Context::class) |
| 34 | + ->disableOriginalConstructor() |
| 35 | + ->getMock(); |
| 36 | + $this->resultJsonMock = $this->getMockBuilder(JsonFactory::class) |
| 37 | + ->disableOriginalConstructor() |
| 38 | + ->getMock(); |
| 39 | + $this->fbeHelperMock = $this->getMockBuilder(FBEHelper::class) |
| 40 | + ->disableOriginalConstructor() |
| 41 | + ->getMock(); |
| 42 | + $this->systemConfigMock = $this->getMockBuilder(SystemConfig::class) |
| 43 | + ->disableOriginalConstructor() |
| 44 | + ->getMock(); |
| 45 | + $this->uploaderMock = $this->getMockBuilder(Uploader::class) |
| 46 | + ->disableOriginalConstructor() |
| 47 | + ->getMock(); |
| 48 | + |
| 49 | + $this->storeMock = $this->getMockBuilder(StoreInterface::class) |
| 50 | + ->disableOriginalConstructor() |
| 51 | + ->getMockForAbstractClass(); |
| 52 | + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) |
| 53 | + ->disableOriginalConstructor() |
| 54 | + ->getMockForAbstractClass(); |
| 55 | + $this->requestMock = $this->getMockBuilder(RequestInterface::class) |
| 56 | + ->disableOriginalConstructor() |
| 57 | + ->getMockForAbstractClass(); |
| 58 | + |
| 59 | + $this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock); |
| 60 | + |
| 61 | + $this->object = new ObjectManager($this); |
| 62 | + $this->subject = $this->object->getObject(PromotionFeedUpload::class, [ |
| 63 | + 'context' => $this->contextMock, |
| 64 | + 'resultJsonFactory' => $this->resultJsonMock, |
| 65 | + 'fbeHelper' => $this->fbeHelperMock, |
| 66 | + 'systemConfig' => $this->systemConfigMock, |
| 67 | + 'uploader' => $this->uploaderMock |
| 68 | + ]); |
| 69 | + } |
| 70 | + |
| 71 | + public function testExecuteForJsonNoStoreParam() |
| 72 | + { |
| 73 | + $storeId = 1; |
| 74 | + $storeName = 'Default Store'; |
| 75 | + |
| 76 | + $this->fbeHelperMock->expects($this->exactly(2)) |
| 77 | + ->method('getStore') |
| 78 | + ->willReturn($this->storeMock); |
| 79 | + $this->storeMock->expects($this->once()) |
| 80 | + ->method('getId') |
| 81 | + ->willReturn($storeId); |
| 82 | + $this->storeMock->expects($this->once()) |
| 83 | + ->method('getName') |
| 84 | + ->willReturn($storeName); |
| 85 | + |
| 86 | + $this->subject->executeForJson(); |
| 87 | + } |
| 88 | + |
| 89 | + public function testExecuteForJsonStoreParam() |
| 90 | + { |
| 91 | + $storeId = 1; |
| 92 | + $storeName = 'Default Store'; |
| 93 | + |
| 94 | + $this->fbeHelperMock->expects($this->exactly(2)) |
| 95 | + ->method('getStore') |
| 96 | + ->willReturn($this->storeMock); |
| 97 | + $this->storeMock->expects($this->once()) |
| 98 | + ->method('getId') |
| 99 | + ->willReturn($storeId); |
| 100 | + $this->storeMock->expects($this->exactly(2)) |
| 101 | + ->method('getName') |
| 102 | + ->willReturn($storeName); |
| 103 | + $this->requestMock->expects($this->once()) |
| 104 | + ->method('getParam') |
| 105 | + ->with('store') |
| 106 | + ->willReturn($storeId); |
| 107 | + $this->systemConfigMock->expects($this->once()) |
| 108 | + ->method('getStoreManager') |
| 109 | + ->willReturn($this->storeManagerMock); |
| 110 | + $this->storeManagerMock->expects($this->once()) |
| 111 | + ->method('getStore') |
| 112 | + ->with($storeId) |
| 113 | + ->willReturn($this->storeMock); |
| 114 | + |
| 115 | + $this->subject->executeForJson(); |
| 116 | + } |
| 117 | + |
| 118 | + public function testExecuteForJsonWithAccessToken() |
| 119 | + { |
| 120 | + $storeId = 1; |
| 121 | + $storeName = 'Default Store'; |
| 122 | + $accessToken = 'akjfbkhab-afjhavfj-ahfhvgja'; |
| 123 | + $pushFeedResponse = ['Success' => true]; |
| 124 | + |
| 125 | + $this->fbeHelperMock->expects($this->exactly(2)) |
| 126 | + ->method('getStore') |
| 127 | + ->willReturn($this->storeMock); |
| 128 | + $this->storeMock->expects($this->once()) |
| 129 | + ->method('getId') |
| 130 | + ->willReturn($storeId); |
| 131 | + $this->storeMock->expects($this->exactly(2)) |
| 132 | + ->method('getName') |
| 133 | + ->willReturn($storeName); |
| 134 | + $this->requestMock->expects($this->once()) |
| 135 | + ->method('getParam') |
| 136 | + ->with('store') |
| 137 | + ->willReturn($storeId); |
| 138 | + $this->systemConfigMock->expects($this->once()) |
| 139 | + ->method('getStoreManager') |
| 140 | + ->willReturn($this->storeManagerMock); |
| 141 | + $this->storeManagerMock->expects($this->once()) |
| 142 | + ->method('getStore') |
| 143 | + ->with($storeId) |
| 144 | + ->willReturn($this->storeMock); |
| 145 | + |
| 146 | + $this->uploaderMock->expects($this->once()) |
| 147 | + ->method('uploadPromotions') |
| 148 | + ->with($storeId) |
| 149 | + ->willReturn($pushFeedResponse); |
| 150 | + $this->systemConfigMock->expects($this->once()) |
| 151 | + ->method('getAccessToken') |
| 152 | + ->with($storeId) |
| 153 | + ->willReturn($accessToken); |
| 154 | + |
| 155 | + $this->subject->executeForJson(); |
| 156 | + } |
| 157 | +} |
0 commit comments