|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\WebapiAsync\Test\Unit\Controller\Rest\Asynchronous; |
| 9 | + |
| 10 | +use Magento\Framework\Reflection\MethodsMap; |
| 11 | +use Magento\Framework\Webapi\Rest\Request; |
| 12 | +use Magento\Framework\Webapi\ServiceInputProcessor; |
| 13 | +use Magento\Framework\Webapi\Validator\EntityArrayValidator\InputArraySizeLimitValue; |
| 14 | +use Magento\Webapi\Controller\Rest\InputParamsResolver as WebapiInputParamsResolver; |
| 15 | +use Magento\Webapi\Controller\Rest\ParamsOverrider; |
| 16 | +use Magento\Webapi\Controller\Rest\RequestValidator; |
| 17 | +use Magento\Webapi\Controller\Rest\Router; |
| 18 | +use Magento\Webapi\Controller\Rest\Router\Route; |
| 19 | +use Magento\WebapiAsync\Controller\Rest\Asynchronous\InputParamsResolver; |
| 20 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +class InputParamsResolverTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var Request|MockObject |
| 28 | + */ |
| 29 | + private $requestMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ParamsOverrider|MockObject |
| 33 | + */ |
| 34 | + private $paramsOverriderMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var ServiceInputProcessor|MockObject |
| 38 | + */ |
| 39 | + private $serviceInputProcessorMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var Router|MockObject |
| 43 | + */ |
| 44 | + private $routerMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var RequestValidator|MockObject |
| 48 | + */ |
| 49 | + private $requestValidatorMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var WebapiInputParamsResolver|MockObject |
| 53 | + */ |
| 54 | + private $webapiInputParamsResolverMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var InputArraySizeLimitValue|MockObject |
| 58 | + */ |
| 59 | + private $inputArraySizeLimitValueMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var MethodsMap|MockObject |
| 63 | + */ |
| 64 | + private $methodsMap; |
| 65 | + |
| 66 | + protected function setUp(): void |
| 67 | + { |
| 68 | + $this->requestMock = $this->createMock(Request::class); |
| 69 | + $this->paramsOverriderMock = $this->createMock(ParamsOverrider::class); |
| 70 | + $this->serviceInputProcessorMock = $this->createMock(ServiceInputProcessor::class); |
| 71 | + $this->routerMock = $this->createMock(Router::class); |
| 72 | + $this->requestValidatorMock = $this->createMock(RequestValidator::class); |
| 73 | + $this->webapiInputParamsResolverMock = $this->createMock(WebapiInputParamsResolver::class); |
| 74 | + $this->inputArraySizeLimitValueMock = $this->createMock(InputArraySizeLimitValue::class); |
| 75 | + $this->methodsMap = $this->createMock(MethodsMap::class); |
| 76 | + } |
| 77 | + |
| 78 | + #[DataProvider('requestBodyDataProvider')] |
| 79 | + public function testResolveAsyncBulkShouldThrowAnErrorForInvalidRequestData( |
| 80 | + array $requestData, |
| 81 | + string $expectedExceptionMessage |
| 82 | + ): void { |
| 83 | + $routeMock = $this->createMock(Route::class); |
| 84 | + $routeMock->method('getParameters') |
| 85 | + ->willReturn([]); |
| 86 | + $this->webapiInputParamsResolverMock->method('getRoute') |
| 87 | + ->willReturn($routeMock); |
| 88 | + $this->paramsOverriderMock->method('override') |
| 89 | + ->willReturnArgument(0); |
| 90 | + $this->requestMock->expects($this->once()) |
| 91 | + ->method('getRequestData') |
| 92 | + ->willReturn($requestData); |
| 93 | + $this->expectException(\Magento\Framework\Webapi\Exception::class); |
| 94 | + $this->expectExceptionMessage($expectedExceptionMessage); |
| 95 | + $this->getModel(true)->resolve(); |
| 96 | + } |
| 97 | + |
| 98 | + public function testResolveAsyncBulk(): void |
| 99 | + { |
| 100 | + $requestData = [['param1' => 'value1'], ['param1' => 'value1']]; |
| 101 | + $routeMock = $this->createMock(Route::class); |
| 102 | + $routeMock->method('getServiceClass') |
| 103 | + ->willReturn('serviceClass'); |
| 104 | + $routeMock->method('getServiceMethod') |
| 105 | + ->willReturn('serviceMethod'); |
| 106 | + $routeMock->method('getParameters') |
| 107 | + ->willReturn([]); |
| 108 | + $this->paramsOverriderMock->method('override') |
| 109 | + ->willReturnArgument(0); |
| 110 | + $this->webapiInputParamsResolverMock->method('getRoute') |
| 111 | + ->willReturn($routeMock); |
| 112 | + $this->requestMock->expects($this->once()) |
| 113 | + ->method('getRequestData') |
| 114 | + ->willReturn($requestData); |
| 115 | + $this->serviceInputProcessorMock->expects($this->exactly(2)) |
| 116 | + ->method('process') |
| 117 | + ->willReturnArgument(2); |
| 118 | + $this->assertEquals($requestData, $this->getModel(true)->resolve()); |
| 119 | + } |
| 120 | + |
| 121 | + #[DataProvider('requestBodyDataProvider')] |
| 122 | + public function testResolveAsync(array $requestData): void |
| 123 | + { |
| 124 | + $this->webapiInputParamsResolverMock->expects($this->once()) |
| 125 | + ->method('resolve') |
| 126 | + ->willReturn($requestData); |
| 127 | + $this->requestMock->method('getRequestData') |
| 128 | + ->willReturn($requestData); |
| 129 | + $this->assertEquals([$requestData], $this->getModel(false)->resolve()); |
| 130 | + } |
| 131 | + |
| 132 | + public static function requestBodyDataProvider(): array |
| 133 | + { |
| 134 | + return [ |
| 135 | + [[1 => []], 'Request body must be an array'], |
| 136 | + [['0str' => []], 'Request body must be an array'], |
| 137 | + [['str' => []], 'Request body must be an array'], |
| 138 | + [['str' => [], 1 => []], 'Request body must be an array'], |
| 139 | + [['str' => [], 0 => [], 1 => []], 'Request body must be an array'], |
| 140 | + ]; |
| 141 | + } |
| 142 | + |
| 143 | + private function getModel(bool $isBulk = false): InputParamsResolver |
| 144 | + { |
| 145 | + return new InputParamsResolver( |
| 146 | + $this->requestMock, |
| 147 | + $this->paramsOverriderMock, |
| 148 | + $this->serviceInputProcessorMock, |
| 149 | + $this->routerMock, |
| 150 | + $this->requestValidatorMock, |
| 151 | + $this->webapiInputParamsResolverMock, |
| 152 | + $isBulk, |
| 153 | + $this->inputArraySizeLimitValueMock, |
| 154 | + $this->methodsMap |
| 155 | + ); |
| 156 | + } |
| 157 | +} |
0 commit comments