|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Product; |
| 7 | + |
| 8 | +use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; |
| 9 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 10 | + |
| 11 | +class AnchorUrlRewriteGeneratorTest extends \PHPUnit\Framework\TestCase |
| 12 | +{ |
| 13 | + /** @var \Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator */ |
| 14 | + protected $anchorUrlRewriteGenerator; |
| 15 | + |
| 16 | + /** @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject */ |
| 17 | + protected $productUrlPathGenerator; |
| 18 | + |
| 19 | + /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */ |
| 20 | + protected $product; |
| 21 | + |
| 22 | + /** @var \Magento\Catalog\Api\CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 23 | + private $categoryRepositoryInterface; |
| 24 | + |
| 25 | + /** @var \Magento\CatalogUrlRewrite\Model\ObjectRegistry|\PHPUnit_Framework_MockObject_MockObject */ |
| 26 | + protected $categoryRegistry; |
| 27 | + |
| 28 | + /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory|\PHPUnit_Framework_MockObject_MockObject */ |
| 29 | + protected $urlRewriteFactory; |
| 30 | + |
| 31 | + /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|\PHPUnit_Framework_MockObject_MockObject */ |
| 32 | + protected $urlRewrite; |
| 33 | + |
| 34 | + protected function setUp() |
| 35 | + { |
| 36 | + $this->urlRewriteFactory = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory::class) |
| 37 | + ->setMethods(['create']) |
| 38 | + ->disableOriginalConstructor()->getMock(); |
| 39 | + $this->urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class) |
| 40 | + ->disableOriginalConstructor()->getMock(); |
| 41 | + $this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class) |
| 42 | + ->disableOriginalConstructor()->getMock(); |
| 43 | + $this->categoryRepositoryInterface = $this->getMockBuilder( |
| 44 | + \Magento\Catalog\Api\CategoryRepositoryInterface::class |
| 45 | + )->disableOriginalConstructor()->getMock(); |
| 46 | + $this->categoryRegistry = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\ObjectRegistry::class) |
| 47 | + ->disableOriginalConstructor()->getMock(); |
| 48 | + $this->productUrlPathGenerator = $this->getMockBuilder( |
| 49 | + \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class |
| 50 | + )->disableOriginalConstructor()->getMock(); |
| 51 | + $this->anchorUrlRewriteGenerator = (new ObjectManager($this))->getObject( |
| 52 | + \Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator::class, |
| 53 | + [ |
| 54 | + 'productUrlPathGenerator' => $this->productUrlPathGenerator, |
| 55 | + 'urlRewriteFactory' => $this->urlRewriteFactory, |
| 56 | + 'categoryRepository' => $this->categoryRepositoryInterface |
| 57 | + ] |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public function testGenerateEmpty() |
| 62 | + { |
| 63 | + $this->categoryRegistry->expects($this->any())->method('getList')->will($this->returnValue([])); |
| 64 | + |
| 65 | + $this->assertEquals( |
| 66 | + [], |
| 67 | + $this->anchorUrlRewriteGenerator->generate(1, $this->product, $this->categoryRegistry) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + public function testGenerateCategories() |
| 72 | + { |
| 73 | + $urlPathWithCategory = 'category1/category2/category3/simple-product.html'; |
| 74 | + $storeId = 10; |
| 75 | + $productId = 12; |
| 76 | + $canonicalUrlPathWithCategory = 'canonical-path-with-category'; |
| 77 | + $categoryParentId = '1'; |
| 78 | + $categoryIds = [$categoryParentId,'2','3','4']; |
| 79 | + $urls = ['category1/simple-product.html', |
| 80 | + 'category1/category2/simple-product.html', |
| 81 | + 'category1/category2/category3/simple-product.html']; |
| 82 | + |
| 83 | + $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId)); |
| 84 | + $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix') |
| 85 | + ->will($this->returnValue($urlPathWithCategory)); |
| 86 | + $this->productUrlPathGenerator->expects($this->any())->method('getCanonicalUrlPath') |
| 87 | + ->will($this->returnValue($canonicalUrlPathWithCategory)); |
| 88 | + $category = $this->createMock(\Magento\Catalog\Model\Category::class); |
| 89 | + $category->expects($this->any())->method('getId')->will($this->returnValue($categoryIds)); |
| 90 | + $category->expects($this->any())->method('getAnchorsAbove')->will($this->returnValue($categoryIds)); |
| 91 | + $category->expects($this->any())->method('getParentId')->will( |
| 92 | + $this->onConsecutiveCalls( |
| 93 | + $categoryIds[0], |
| 94 | + $categoryIds[1], |
| 95 | + $categoryIds[2], |
| 96 | + $categoryIds[3] |
| 97 | + ) |
| 98 | + ); |
| 99 | + $this->categoryRepositoryInterface |
| 100 | + ->expects($this->any()) |
| 101 | + ->method('get') |
| 102 | + ->withConsecutive( |
| 103 | + [ 'category_id' => $categoryIds[0]], |
| 104 | + [ 'category_id' => $categoryIds[1]], |
| 105 | + [ 'category_id' => $categoryIds[2]] |
| 106 | + ) |
| 107 | + ->will($this->returnValue($category)); |
| 108 | + $this->categoryRegistry->expects($this->any())->method('getList') |
| 109 | + ->will($this->returnValue([$category])); |
| 110 | + $this->urlRewrite->expects($this->any())->method('setStoreId') |
| 111 | + ->with($storeId) |
| 112 | + ->will($this->returnSelf()); |
| 113 | + $this->urlRewrite->expects($this->any())->method('setEntityId') |
| 114 | + ->with($productId) |
| 115 | + ->will($this->returnSelf()); |
| 116 | + $this->urlRewrite->expects($this->any())->method('setEntityType') |
| 117 | + ->with(ProductUrlRewriteGenerator::ENTITY_TYPE) |
| 118 | + ->will($this->returnSelf()); |
| 119 | + $this->urlRewrite->expects($this->any())->method('setRequestPath') |
| 120 | + ->will($this->returnSelf()); |
| 121 | + $this->urlRewrite->expects($this->any())->method('setTargetPath') |
| 122 | + ->will($this->returnSelf()); |
| 123 | + $this->urlRewrite->expects($this->any())->method('setMetadata') |
| 124 | + ->will( |
| 125 | + $this->onConsecutiveCalls( |
| 126 | + $urls[0], |
| 127 | + $urls[1], |
| 128 | + $urls[2] |
| 129 | + ) |
| 130 | + ); |
| 131 | + $this->urlRewriteFactory->expects($this->any())->method('create')->will( |
| 132 | + $this->returnValue($this->urlRewrite) |
| 133 | + ); |
| 134 | + |
| 135 | + $this->assertEquals( |
| 136 | + $urls, |
| 137 | + $this->anchorUrlRewriteGenerator->generate($storeId, $this->product, $this->categoryRegistry) |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
0 commit comments