Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AnchorRendererTest.php
Go to the documentation of this file.
1 <?php
7 
13 
14 class AnchorRendererTest extends \PHPUnit\Framework\TestCase
15 {
19  private $activeMenuItemMock;
20 
24  private $menuItemMock;
25 
29  private $escaperMock;
30 
34  private $objectManagerHelper;
35 
39  private $menuItemCheckerMock;
40 
44  private $anchorRenderer;
45 
46  protected function setUp()
47  {
48  $this->activeMenuItemMock = $this->getMockBuilder(Item::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51  $this->menuItemMock = $this->getMockBuilder(Item::class)
52  ->disableOriginalConstructor()
53  ->getMock();
54  $this->menuItemWithoutChildrenMock = $this->getMockBuilder(Item::class)
55  ->disableOriginalConstructor()
56  ->getMock();
57  $this->menuItemCheckerMock = $this->getMockBuilder(MenuItemChecker::class)
58  ->disableOriginalConstructor()
59  ->getMock();
60  $this->escaperMock = $this->getMockBuilder(Escaper::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63 
64  $this->objectManagerHelper = new ObjectManagerHelper($this);
65  $this->anchorRenderer = $this->objectManagerHelper->getObject(
66  AnchorRenderer::class,
67  [
68  'menuItemChecker' => $this->menuItemCheckerMock,
69  'escaper' => $this->escaperMock
70  ]
71  );
72  }
73 
74  public function testRenderAnchorLevelIsOne()
75  {
76  $title = 'Title';
77  $html = 'Test html';
78  $this->menuItemMock->expects($this->once())->method('getUrl')->willReturn('#');
79  $this->menuItemMock->expects($this->once())->method('getTitle')->willReturn($title);
80  $this->menuItemMock->expects($this->once())->method('hasChildren')->willReturn(true);
81  $this->escaperMock->expects($this->once())->method('escapeHtml')->with(__($title))->willReturn($html);
82 
83  $expected = '<strong class="submenu-group-title" role="presentation">'
84  . '<span>' . $html . '</span>'
85  . '</strong>';
86 
87  $this->assertEquals(
88  $expected,
89  $this->anchorRenderer->renderAnchor($this->activeMenuItemMock, $this->menuItemMock, 1)
90  );
91  }
92 
94  {
95  $this->menuItemWithoutChildrenMock->expects($this->once())->method('getUrl')->willReturn('#');
96  $this->menuItemWithoutChildrenMock->expects($this->once())->method('hasChildren')->willReturn(false);
97 
98  $expected = '';
99 
100  $this->assertEquals(
101  $expected,
102  $this->anchorRenderer->renderAnchor($this->activeMenuItemMock, $this->menuItemWithoutChildrenMock, 1)
103  );
104  }
105 
110  public function testRenderAnchorLevelIsNotOne($hasTarget)
111  {
112  $level = 0;
113  $title = 'Title';
114  $html = 'Test html';
115  $url = 'test/url';
116  $tooltip = 'Anchor title';
117  $onclick = '';
118  $target = '_blank';
119  $finalTarget = $hasTarget ? ('target=' . $target) : '';
120  $this->menuItemMock->expects($this->any())->method('getTarget')->willReturn($hasTarget ? $target : null);
121  $this->menuItemMock->expects($this->once())->method('getUrl')->willReturn($url);
122  $this->menuItemMock->expects($this->once())->method('getTitle')->willReturn($title);
123  $this->escaperMock->expects($this->once())->method('escapeHtml')->with(__($title))->willReturn($html);
124  $this->menuItemMock->expects($this->once())->method('hasTooltip')->willReturn(true);
125  $this->menuItemMock->expects($this->any())->method('getTooltip')->willReturn(__($tooltip));
126  $this->menuItemMock->expects($this->once())->method('hasClickCallback')->willReturn(true);
127  $this->menuItemMock->expects($this->once())->method('getClickCallback')->willReturn($onclick);
128  $this->menuItemCheckerMock->expects($this->once())
129  ->method('isItemActive')
130  ->with($this->activeMenuItemMock, $this->menuItemMock, $level)->willReturn(true);
131 
132  $expected = '<a href="' . $url . '" ' . $finalTarget . ' ' . 'title="' . $tooltip . '"'
133  . ' onclick="' . $onclick . '"'
134  . ' class="' . '_active'
135  . '">' . '<span>' . $html
136  . '</span>' . '</a>';
137 
138  $this->assertEquals(
139  $expected,
140  $this->anchorRenderer->renderAnchor($this->activeMenuItemMock, $this->menuItemMock, $level)
141  );
142  }
143 
147  public function targetDataProvider()
148  {
149  return [
150  'item has target' => [true],
151  'item does not have target' => [false]
152  ];
153  }
154 }
$title
Definition: default.phtml:14
$target
Definition: skip.phtml:8
__()
Definition: __.php:13