Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CurrentTest.php
Go to the documentation of this file.
1 <?php
7 
8 class CurrentTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_urlBuilderMock;
14 
18  protected $_requestMock;
19 
23  protected $_defaultPathMock;
24 
28  protected $_objectManager;
29 
30  protected function setUp()
31  {
32  $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
33  $this->_urlBuilderMock = $this->createMock(\Magento\Framework\UrlInterface::class);
34  $this->_requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
35  $this->_defaultPathMock = $this->createMock(\Magento\Framework\App\DefaultPathInterface::class);
36  }
37 
38  public function testGetUrl()
39  {
40  $path = 'test/path';
41  $url = 'http://example.com/asdasd';
42 
43  $this->_urlBuilderMock->expects($this->once())->method('getUrl')->with($path)->will($this->returnValue($url));
44 
46  $link = $this->_objectManager->getObject(
47  \Magento\Framework\View\Element\Html\Link\Current::class,
48  ['urlBuilder' => $this->_urlBuilderMock]
49  );
50 
51  $link->setPath($path);
52  $this->assertEquals($url, $link->getHref());
53  }
54 
55  public function testIsCurrentIfIsset()
56  {
58  $link = $this->_objectManager->getObject(\Magento\Framework\View\Element\Html\Link\Current::class);
59  $link->setCurrent(true);
60  $this->assertTrue($link->isCurrent());
61  }
62 
63  public function testIsCurrent()
64  {
65  $path = 'test/path';
66  $url = 'http://example.com/a/b';
67 
68  $this->_requestMock->expects($this->once())->method('getModuleName')->will($this->returnValue('a'));
69  $this->_requestMock->expects($this->once())->method('getControllerName')->will($this->returnValue('b'));
70  $this->_requestMock->expects($this->once())->method('getActionName')->will($this->returnValue('d'));
71  $this->_defaultPathMock->expects($this->atLeastOnce())->method('getPart')->will($this->returnValue('d'));
72 
73  $this->_urlBuilderMock->expects($this->at(0))->method('getUrl')->with($path)->will($this->returnValue($url));
74  $this->_urlBuilderMock->expects($this->at(1))->method('getUrl')->with('a/b')->will($this->returnValue($url));
75 
76  $this->_requestMock->expects($this->once())->method('getControllerName')->will($this->returnValue('b'));
78  $link = $this->_objectManager->getObject(
79  \Magento\Framework\View\Element\Html\Link\Current::class,
80  [
81  'urlBuilder' => $this->_urlBuilderMock,
82  'request' => $this->_requestMock,
83  'defaultPath' => $this->_defaultPathMock
84  ]
85  );
86  $link->setPath($path);
87  $this->assertTrue($link->isCurrent());
88  }
89 
90  public function testIsCurrentFalse()
91  {
92  $this->_urlBuilderMock->expects($this->at(0))->method('getUrl')->will($this->returnValue('1'));
93  $this->_urlBuilderMock->expects($this->at(1))->method('getUrl')->will($this->returnValue('2'));
94 
96  $link = $this->_objectManager->getObject(
97  \Magento\Framework\View\Element\Html\Link\Current::class,
98  ['urlBuilder' => $this->_urlBuilderMock, 'request' => $this->_requestMock]
99  );
100  $this->assertFalse($link->isCurrent());
101  }
102 }