|
| 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\Model\Products; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\CatalogUrlRewrite\Model\Product\GetProductUrlRewriteDataByStore; |
| 12 | +use Magento\CatalogUrlRewrite\Model\Products\AppendUrlRewritesToProducts; |
| 13 | +use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator; |
| 14 | +use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; |
| 15 | +use Magento\CatalogUrlRewrite\Service\V1\StoreViewService; |
| 16 | +use Magento\UrlRewrite\Model\UrlPersistInterface; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +class AppendUrlRewritesToProductsTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var ProductUrlRewriteGenerator|MockObject |
| 24 | + */ |
| 25 | + private ProductUrlRewriteGenerator $productUrlRewriteGenerator; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var StoreViewService|MockObject |
| 29 | + */ |
| 30 | + private StoreViewService $storeViewService; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ProductUrlPathGenerator|MockObject |
| 34 | + */ |
| 35 | + private ProductUrlPathGenerator $productUrlPathGenerator; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var UrlPersistInterface|MockObject |
| 39 | + */ |
| 40 | + private UrlPersistInterface $urlPersist; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var GetProductUrlRewriteDataByStore|MockObject |
| 44 | + */ |
| 45 | + private GetProductUrlRewriteDataByStore $getDataByStore; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var AppendUrlRewritesToProducts |
| 49 | + */ |
| 50 | + private AppendUrlRewritesToProducts $append; |
| 51 | + |
| 52 | + protected function setUp(): void |
| 53 | + { |
| 54 | + $this->productUrlRewriteGenerator = $this->createMock(ProductUrlRewriteGenerator::class); |
| 55 | + $this->storeViewService = $this->createMock(StoreViewService::class); |
| 56 | + $this->productUrlPathGenerator = $this->createMock(ProductUrlPathGenerator::class); |
| 57 | + $this->urlPersist = $this->createMock(UrlPersistInterface::class); |
| 58 | + $this->getDataByStore = $this->createMock(GetProductUrlRewriteDataByStore::class); |
| 59 | + |
| 60 | + $this->append = new AppendUrlRewritesToProducts( |
| 61 | + $this->productUrlRewriteGenerator, |
| 62 | + $this->storeViewService, |
| 63 | + $this->productUrlPathGenerator, |
| 64 | + $this->urlPersist, |
| 65 | + $this->getDataByStore |
| 66 | + ); |
| 67 | + parent::setUp(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return void |
| 72 | + * @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException |
| 73 | + */ |
| 74 | + public function testSaveProductUrlRewrites(): void |
| 75 | + { |
| 76 | + $rewrites = ['test']; |
| 77 | + $this->urlPersist->expects($this->once())->method('replace')->with($rewrites); |
| 78 | + $this->append->saveProductUrlRewrites($rewrites); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return void |
| 83 | + * @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException |
| 84 | + */ |
| 85 | + public function testGetProductUrlRewrites(): void |
| 86 | + { |
| 87 | + $storeId = $productId = 1; |
| 88 | + $product = $this->getMockBuilder(Product::class) |
| 89 | + ->disableOriginalConstructor() |
| 90 | + ->addMethods(['unsUrlPath', 'setUrlPath']) |
| 91 | + ->onlyMethods(['getStoreId', 'getId', 'getStoreIds']) |
| 92 | + ->getMock(); |
| 93 | + $product->expects($this->any())->method('getStoreId')->willReturn(0); |
| 94 | + $product->expects($this->any())->method('getId')->willReturn($productId); |
| 95 | + $product->expects($this->any())->method('getStoreIds')->willReturn([$storeId]); |
| 96 | + $product->expects($this->once())->method('unsUrlPath'); |
| 97 | + $product->expects($this->once())->method('setUrlPath'); |
| 98 | + |
| 99 | + $this->productUrlPathGenerator->expects($this->once())->method('getUrlPath'); |
| 100 | + $this->productUrlRewriteGenerator->expects($this->once())->method('generate')->willReturn([]); |
| 101 | + $this->getDataByStore->expects($this->once())->method('clearProductUrlRewriteDataCache'); |
| 102 | + $this->urlPersist->expects($this->once())->method('replace'); |
| 103 | + |
| 104 | + $this->storeViewService->expects($this->once()) |
| 105 | + ->method('doesEntityHaveOverriddenUrlKeyForStore') |
| 106 | + ->with($storeId, $productId, Product::ENTITY) |
| 107 | + ->willReturn(false); |
| 108 | + |
| 109 | + $this->append->execute([$product], [$storeId]); |
| 110 | + } |
| 111 | +} |
0 commit comments