|
| 1 | +<?php |
| 2 | +/*** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\WebapiAsync\Test\Unit\Controller; |
| 10 | + |
| 11 | +use Magento\AsynchronousOperations\Api\Data\AsyncResponseInterfaceFactory; |
| 12 | +use Magento\AsynchronousOperations\Model\ConfigInterface as WebApiAsyncConfig; |
| 13 | +use Magento\AsynchronousOperations\Model\MassSchedule; |
| 14 | +use Magento\Framework\Webapi\Rest\Request; |
| 15 | +use Magento\Webapi\Controller\Rest\Router; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Magento\Framework\Webapi\Rest\Response; |
| 19 | +use Magento\WebapiAsync\Controller\Rest\Asynchronous\InputParamsResolver; |
| 20 | +use Magento\WebapiAsync\Controller\Rest\AsynchronousRequestProcessor; |
| 21 | +use Magento\Framework\Reflection\DataObjectProcessor; |
| 22 | + |
| 23 | +/** |
| 24 | + * Test for Magento\WebapiAsync\Controller\AsynchronousRequestProcessor class. |
| 25 | + */ |
| 26 | +class AsynchronousRequestProcessorTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var Response|MockObject |
| 30 | + */ |
| 31 | + private $responseMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var InputParamsResolver|MockObject |
| 35 | + */ |
| 36 | + private $inputParamsResolverMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var MassSchedule|MockObject |
| 40 | + */ |
| 41 | + private $asyncBulkPublisher; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var WebApiAsyncConfig|MockObject |
| 45 | + */ |
| 46 | + private $webapiAsyncConfig; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var DataObjectProcessor|MockObject |
| 50 | + */ |
| 51 | + private $dataObjectProcessor; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var AsyncResponseInterfaceFactory|MockObject |
| 55 | + */ |
| 56 | + private $asyncResponseFactory; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var string Regex pattern |
| 60 | + */ |
| 61 | + private $processorPath; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var Request|MockObject |
| 65 | + */ |
| 66 | + private $requestMock; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var AsynchronousRequestProcessor |
| 70 | + */ |
| 71 | + private $subject; |
| 72 | + |
| 73 | + /** |
| 74 | + * @inheritdoc |
| 75 | + */ |
| 76 | + protected function setUp(): void |
| 77 | + { |
| 78 | + $this->requestMock = $this->getRequestMock(); |
| 79 | + |
| 80 | + $this->responseMock = $this->getMockBuilder(Response::class) |
| 81 | + ->disableOriginalConstructor() |
| 82 | + ->getMock(); |
| 83 | + |
| 84 | + $this->inputParamsResolverMock = $this->getMockBuilder(InputParamsResolver::class) |
| 85 | + ->disableOriginalConstructor() |
| 86 | + ->getMock(); |
| 87 | + |
| 88 | + $this->asyncBulkPublisher = $this->getMockBuilder(MassSchedule::class) |
| 89 | + ->disableOriginalConstructor() |
| 90 | + ->getMock(); |
| 91 | + |
| 92 | + $this->webapiAsyncConfig = $this->getMockBuilder(WebApiAsyncConfig::class) |
| 93 | + ->disableOriginalConstructor() |
| 94 | + ->getMock(); |
| 95 | + |
| 96 | + $this->dataObjectProcessor = $this->getMockBuilder(DataObjectProcessor::class) |
| 97 | + ->disableOriginalConstructor() |
| 98 | + ->getMock(); |
| 99 | + |
| 100 | + $this->asyncResponseFactory = $this->getMockBuilder(AsyncResponseInterfaceFactory::class) |
| 101 | + ->disableOriginalConstructor() |
| 102 | + ->getMock(); |
| 103 | + |
| 104 | + $this->processorPath = AsynchronousRequestProcessor::PROCESSOR_PATH; |
| 105 | + |
| 106 | + $this->subject = new AsynchronousRequestProcessor( |
| 107 | + $this->responseMock, |
| 108 | + $this->inputParamsResolverMock, |
| 109 | + $this->asyncBulkPublisher, |
| 110 | + $this->webapiAsyncConfig, |
| 111 | + $this->dataObjectProcessor, |
| 112 | + $this->asyncResponseFactory, |
| 113 | + $this->processorPath |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + public function testCanNotProcess(): void |
| 118 | + { |
| 119 | + $this->requestMock->expects($this->any()) |
| 120 | + ->method('getPathInfo') |
| 121 | + ->willReturn("/async/bulk/V1/configurable-products/bySku/options"); |
| 122 | + |
| 123 | + $this->assertFalse($this->subject->canProcess($this->requestMock)); |
| 124 | + } |
| 125 | + |
| 126 | + public function testCanProcess(): void |
| 127 | + { |
| 128 | + $this->requestMock->expects($this->any()) |
| 129 | + ->method('getPathInfo') |
| 130 | + ->willReturn("/async/V1/configurable-products/bySku/options"); |
| 131 | + |
| 132 | + $route = $this->getMockBuilder(Router\Route::class) |
| 133 | + ->disableOriginalConstructor() |
| 134 | + ->getMock(); |
| 135 | + |
| 136 | + $route->expects($this->once()) |
| 137 | + ->method('getAclResources') |
| 138 | + ->willReturn(['Magento_Catalog::products']); |
| 139 | + |
| 140 | + $this->inputParamsResolverMock->expects($this->once()) |
| 141 | + ->method('getRoute') |
| 142 | + ->willReturn($route); |
| 143 | + |
| 144 | + $this->assertTrue($this->subject->canProcess($this->requestMock)); |
| 145 | + } |
| 146 | + |
| 147 | + public function testCanProcessSelfResourceRequest(): void |
| 148 | + { |
| 149 | + $this->requestMock->expects($this->any()) |
| 150 | + ->method('getPathInfo') |
| 151 | + ->willReturn("/async/V1/configurable-products/bySku/options"); |
| 152 | + |
| 153 | + $route = $this->getMockBuilder(Router\Route::class) |
| 154 | + ->disableOriginalConstructor() |
| 155 | + ->getMock(); |
| 156 | + |
| 157 | + $route->expects($this->once()) |
| 158 | + ->method('getAclResources') |
| 159 | + ->willReturn(['self']); |
| 160 | + |
| 161 | + $this->inputParamsResolverMock->expects($this->once()) |
| 162 | + ->method('getRoute') |
| 163 | + ->willReturn($route); |
| 164 | + |
| 165 | + $this->assertFalse($this->subject->canProcess($this->requestMock)); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @return Request|MockObject |
| 170 | + */ |
| 171 | + private function getRequestMock() |
| 172 | + { |
| 173 | + return $this->getMockBuilder(Request::class) |
| 174 | + ->onlyMethods( |
| 175 | + [ |
| 176 | + 'isSecure', |
| 177 | + 'getRequestData', |
| 178 | + 'getParams', |
| 179 | + 'getParam', |
| 180 | + 'getRequestedServices', |
| 181 | + 'getPathInfo', |
| 182 | + 'getHttpHost', |
| 183 | + 'getMethod', |
| 184 | + ] |
| 185 | + )->disableOriginalConstructor() |
| 186 | + ->getMock(); |
| 187 | + } |
| 188 | +} |
0 commit comments