Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoriesUrlRewriteGeneratorTest.php
Go to the documentation of this file.
1 <?php
7 
11 
12 class CategoriesUrlRewriteGeneratorTest extends \PHPUnit\Framework\TestCase
13 {
16 
19 
21  protected $product;
22 
24  protected $categoryRegistry;
25 
27  protected $urlRewriteFactory;
28 
30  protected $urlRewrite;
31 
32  protected function setUp()
33  {
34  $this->urlRewriteFactory = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory::class)
35  ->setMethods(['create'])
36  ->disableOriginalConstructor()->getMock();
37  $this->urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
38  ->disableOriginalConstructor()->getMock();
39  $this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
40  ->disableOriginalConstructor()->getMock();
41  $this->categoryRegistry = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\ObjectRegistry::class)
42  ->disableOriginalConstructor()->getMock();
43  $this->productUrlPathGenerator = $this->getMockBuilder(
44  \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class
45  )->disableOriginalConstructor()->getMock();
46  $this->categoriesUrlRewriteGenerator = (new ObjectManager($this))->getObject(
47  \Magento\CatalogUrlRewrite\Model\Product\CategoriesUrlRewriteGenerator::class,
48  [
49  'productUrlPathGenerator' => $this->productUrlPathGenerator,
50  'urlRewriteFactory' => $this->urlRewriteFactory
51  ]
52  );
53  }
54 
55  public function testGenerateEmpty()
56  {
57  $this->categoryRegistry->expects($this->any())->method('getList')->will($this->returnValue([]));
58 
59  $this->assertEquals(
60  [],
61  $this->categoriesUrlRewriteGenerator->generate(1, $this->product, $this->categoryRegistry)
62  );
63  }
64 
65  public function testGenerateCategories()
66  {
67  $urlPathWithCategory = 'category/simple-product.html';
68  $storeId = 10;
69  $productId = 'product_id';
70  $canonicalUrlPathWithCategory = 'canonical-path-with-category';
71  $categoryId = 'category_id';
72 
73  $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId));
74  $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')
75  ->will($this->returnValue($urlPathWithCategory));
76  $this->productUrlPathGenerator->expects($this->any())->method('getCanonicalUrlPath')
77  ->will($this->returnValue($canonicalUrlPathWithCategory));
78  $category = $this->createMock(\Magento\Catalog\Model\Category::class);
79  $category->expects($this->any())->method('getId')->will($this->returnValue($categoryId));
80  $this->categoryRegistry->expects($this->any())->method('getList')
81  ->will($this->returnValue([$category]));
82 
83  $this->urlRewrite->expects($this->any())->method('setStoreId')->with($storeId)
84  ->will($this->returnSelf());
85  $this->urlRewrite->expects($this->any())->method('setEntityId')->with($productId)
86  ->will($this->returnSelf());
87  $this->urlRewrite->expects($this->any())->method('setEntityType')
88  ->with(ProductUrlRewriteGenerator::ENTITY_TYPE)->will($this->returnSelf());
89  $this->urlRewrite->expects($this->any())->method('setRequestPath')->with($urlPathWithCategory)
90  ->will($this->returnSelf());
91  $this->urlRewrite->expects($this->any())->method('setTargetPath')->with($canonicalUrlPathWithCategory)
92  ->will($this->returnSelf());
93  $this->urlRewrite->expects($this->any())->method('setMetadata')
94  ->with(['category_id' => $categoryId])->will($this->returnSelf());
95  $this->urlRewriteFactory->expects($this->any())->method('create')->will($this->returnValue($this->urlRewrite));
96 
97  $this->assertEquals(
98  [
99  $this->urlRewrite,
100  ],
101  $this->categoriesUrlRewriteGenerator->generate($storeId, $this->product, $this->categoryRegistry)
102  );
103  }
104 }