|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogUrlRewrite\Test\Unit\Plugin\Webapi\Controller\Rest; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\CatalogUrlRewrite\Model\SetSaveRewriteHistory; |
| 13 | +use Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\ProductInputParamsResolver; |
| 14 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 15 | +use Magento\Framework\Webapi\Rest\Request as RestRequest; |
| 16 | +use Magento\Webapi\Controller\Rest\InputParamsResolver; |
| 17 | +use Magento\Webapi\Controller\Rest\Router\Route; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +/** |
| 22 | + * Unit test for ProductInputParamsResolver plugin |
| 23 | + */ |
| 24 | +class ProductInputParamsResolverTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var array |
| 28 | + */ |
| 29 | + private $result; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ObjectManager |
| 33 | + */ |
| 34 | + private $objectManager; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var InputParamsResolver|MockObject |
| 38 | + */ |
| 39 | + private $subject; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var RestRequest|MockObject |
| 43 | + */ |
| 44 | + private $request; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Product|MockObject |
| 48 | + */ |
| 49 | + private $product; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Route|MockObject |
| 53 | + */ |
| 54 | + private $route; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var SetSaveRewriteHistory|MockObject |
| 58 | + */ |
| 59 | + private $rewriteHistoryMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var ProductInputParamsResolver |
| 63 | + */ |
| 64 | + private $plugin; |
| 65 | + |
| 66 | + /** |
| 67 | + * @inheritdoc |
| 68 | + */ |
| 69 | + protected function setUp(): void |
| 70 | + { |
| 71 | + $this->route = $this->createMock(Route::class); |
| 72 | + $this->request = $this->createMock(RestRequest::class); |
| 73 | + $this->subject = $this->createMock(InputParamsResolver::class); |
| 74 | + $this->product = $this->createMock(Product::class); |
| 75 | + $this->rewriteHistoryMock = $this->createMock(SetSaveRewriteHistory::class); |
| 76 | + } |
| 77 | + |
| 78 | + public function testAfterResolveWithProduct() |
| 79 | + { |
| 80 | + $this->subject->expects($this->any()) |
| 81 | + ->method('getRoute') |
| 82 | + ->willReturn($this->route); |
| 83 | + |
| 84 | + $this->result = [false, $this->product, 'test']; |
| 85 | + |
| 86 | + $this->objectManager = new ObjectManager($this); |
| 87 | + $this->plugin = $this->objectManager->getObject( |
| 88 | + ProductInputParamsResolver::class, |
| 89 | + [ |
| 90 | + 'rewriteHistory' => $this->rewriteHistoryMock |
| 91 | + ] |
| 92 | + ); |
| 93 | + |
| 94 | + $this->route->expects($this->once()) |
| 95 | + ->method('getServiceClass') |
| 96 | + ->willReturn(ProductRepositoryInterface::class); |
| 97 | + $this->route->expects($this->once()) |
| 98 | + ->method('getServiceMethod') |
| 99 | + ->willReturn('save'); |
| 100 | + $this->rewriteHistoryMock->expects($this->once()) |
| 101 | + ->method('execute') |
| 102 | + ->with($this->result, 'product', Product::class) |
| 103 | + ->willReturn($this->result); |
| 104 | + |
| 105 | + $this->plugin->afterResolve($this->subject, $this->result); |
| 106 | + } |
| 107 | +} |
0 commit comments