Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BreadcrumbsTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Magento\Catalog\Helper\Data as CatalogHelper;
15 
19 class BreadcrumbsTest extends \PHPUnit\Framework\TestCase
20 {
24  private $viewModel;
25 
29  private $catalogHelper;
30 
34  private $scopeConfig;
35 
39  private $objectManager;
40 
44  protected function setUp() : void
45  {
46  $this->catalogHelper = $this->getMockBuilder(CatalogHelper::class)
47  ->setMethods(['getProduct'])
48  ->disableOriginalConstructor()
49  ->getMock();
50 
51  $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
52  ->setMethods(['getValue', 'isSetFlag'])
53  ->disableOriginalConstructor()
54  ->getMockForAbstractClass();
55 
56  $escaper = $this->getObjectManager()->getObject(\Magento\Framework\Escaper::class);
57 
58  $this->viewModel = $this->getObjectManager()->getObject(
59  Breadcrumbs::class,
60  [
61  'catalogData' => $this->catalogHelper,
62  'scopeConfig' => $this->scopeConfig,
63  'escaper' => $escaper
64  ]
65  );
66  }
67 
71  public function testGetCategoryUrlSuffix() : void
72  {
73  $this->scopeConfig->expects($this->once())
74  ->method('getValue')
75  ->with('catalog/seo/category_url_suffix', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
76  ->willReturn('.html');
77 
78  $this->assertEquals('.html', $this->viewModel->getCategoryUrlSuffix());
79  }
80 
84  public function testIsCategoryUsedInProductUrl() : void
85  {
86  $this->scopeConfig->expects($this->once())
87  ->method('isSetFlag')
88  ->with('catalog/seo/product_use_categories', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
89  ->willReturn(false);
90 
91  $this->assertFalse($this->viewModel->isCategoryUsedInProductUrl());
92  }
93 
101  public function testGetProductName($product, string $expectedName) : void
102  {
103  $this->catalogHelper->expects($this->atLeastOnce())
104  ->method('getProduct')
105  ->willReturn($product);
106 
107  $this->assertEquals($expectedName, $this->viewModel->getProductName());
108  }
109 
113  public function productDataProvider() : array
114  {
115  return [
116  [$this->getObjectManager()->getObject(Product::class, ['data' => ['name' => 'Test']]), 'Test'],
117  [null, ''],
118  ];
119  }
120 
128  public function testGetJsonConfiguration($product, string $expectedJson) : void
129  {
130  $this->catalogHelper->expects($this->atLeastOnce())
131  ->method('getProduct')
132  ->willReturn($product);
133 
134  $this->scopeConfig->expects($this->any())
135  ->method('isSetFlag')
136  ->with('catalog/seo/product_use_categories', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
137  ->willReturn(false);
138 
139  $this->scopeConfig->expects($this->any())
140  ->method('getValue')
141  ->with('catalog/seo/category_url_suffix', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
142  ->willReturn('."html');
143 
144  $this->assertEquals($expectedJson, $this->viewModel->getJsonConfiguration());
145  }
146 
150  public function productJsonEncodeDataProvider() : array
151  {
152  return [
153  [
154  $this->getObjectManager()->getObject(Product::class, ['data' => ['name' => 'Test ™']]),
155  '{"breadcrumbs":{"categoryUrlSuffix":".&quot;html","userCategoryPathInUrl":0,"product":"Test \u2122"}}',
156  ],
157  [
158  $this->getObjectManager()->getObject(Product::class, ['data' => ['name' => 'Test "']]),
159  '{"breadcrumbs":{"categoryUrlSuffix":".&quot;html","userCategoryPathInUrl":0,"product":"Test &quot;"}}',
160  ],
161  [
162  $this->getObjectManager()->getObject(Product::class, ['data' => ['name' => 'Test <b>x</b>']]),
163  '{"breadcrumbs":{"categoryUrlSuffix":".&quot;html","userCategoryPathInUrl":0,"product":'
164  . '"Test &lt;b&gt;x&lt;\/b&gt;"}}',
165  ],
166  [
167  $this->getObjectManager()->getObject(Product::class, ['data' => ['name' => 'Test \'abc\'']]),
168  '{"breadcrumbs":'
169  . '{"categoryUrlSuffix":".&quot;html","userCategoryPathInUrl":0,"product":"Test &#039;abc&#039;"}}'
170  ],
171  ];
172  }
173 
177  private function getObjectManager() : ObjectManager
178  {
179  if (null === $this->objectManager) {
180  $this->objectManager = new ObjectManager($this);
181  }
182 
183  return $this->objectManager;
184  }
185 }