Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CanonicalUrlRewriteGeneratorTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class CanonicalUrlRewriteGeneratorTest extends \PHPUnit\Framework\TestCase
12 {
15 
18 
20  protected $product;
21 
23  protected $categoryRegistry;
24 
26  protected $urlRewriteFactory;
27 
29  protected $urlRewrite;
30 
31  protected function setUp()
32  {
33  $this->urlRewriteFactory = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory::class)
34  ->setMethods(['create'])
35  ->disableOriginalConstructor()->getMock();
36  $this->urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
37  ->disableOriginalConstructor()->getMock();
38  $this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
39  ->disableOriginalConstructor()->getMock();
40  $this->categoryRegistry = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\ObjectRegistry::class)
41  ->disableOriginalConstructor()->getMock();
42  $this->productUrlPathGenerator = $this->getMockBuilder(
43  \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class
44  )->disableOriginalConstructor()->getMock();
45  $this->canonicalUrlRewriteGenerator = (new ObjectManager($this))->getObject(
46  \Magento\CatalogUrlRewrite\Model\Product\CanonicalUrlRewriteGenerator::class,
47  [
48  'productUrlPathGenerator' => $this->productUrlPathGenerator,
49  'urlRewriteFactory' => $this->urlRewriteFactory
50  ]
51  );
52  }
53 
54  public function testGenerate()
55  {
56  $requestPath = 'simple-product.html';
57  $storeId = 10;
58  $productId = 'product_id';
59  $targetPath = 'catalog/product/view/id/' . $productId;
60 
61  $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId));
62  $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')
63  ->will($this->returnValue($requestPath));
64  $this->productUrlPathGenerator->expects($this->any())->method('getCanonicalUrlPath')
65  ->will($this->returnValue($targetPath));
66  $this->categoryRegistry->expects($this->any())->method('getList')->will($this->returnValue([]));
67 
68  $this->urlRewrite->expects($this->any())->method('setStoreId')->with($storeId)
69  ->will($this->returnSelf());
70  $this->urlRewrite->expects($this->any())->method('setEntityId')->with($productId)
71  ->will($this->returnSelf());
72  $this->urlRewrite->expects($this->any())->method('setEntityType')
73  ->with(ProductUrlRewriteGenerator::ENTITY_TYPE)->will($this->returnSelf());
74  $this->urlRewrite->expects($this->any())->method('setRequestPath')->with($requestPath)
75  ->will($this->returnSelf());
76  $this->urlRewrite->expects($this->any())->method('setTargetPath')->with($targetPath)
77  ->will($this->returnSelf());
78  $this->urlRewriteFactory->expects($this->any())->method('create')->will($this->returnValue($this->urlRewrite));
79  $this->assertEquals(
80  [
81  $this->urlRewrite,
82  ],
83  $this->canonicalUrlRewriteGenerator->generate($storeId, $this->product)
84  );
85  }
86 }