Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CookieTest.php
Go to the documentation of this file.
1 <?php
7 
8 class CookieTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $model;
14 
18  protected $contextMock;
19 
23  protected $sessionConfigMock;
24 
28  protected $ipValidatorMock;
29 
30  protected function setUp()
31  {
32  $this->contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
33  ->disableOriginalConstructor()
34  ->getMock();
35  $this->sessionConfigMock = $this->getMockBuilder(\Magento\Framework\Session\Config::class)
36  ->disableOriginalConstructor()
37  ->getMock();
38  $this->ipValidatorMock = $this->getMockBuilder(\Magento\Framework\Validator\Ip::class)
39  ->disableOriginalConstructor()
40  ->getMock();
41 
42  $validtorMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\File\Validator::class)
43  ->setMethods(['isValid'])->disableOriginalConstructor()->getMock();
44 
45  $scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config::class)
46  ->setMethods(['isSetFlag'])->disableOriginalConstructor()->getMock();
47 
48  $this->contextMock->expects($this->any())
49  ->method('getScopeConfig')
50  ->will($this->returnValue($scopeConfigMock));
51 
52  $this->contextMock->expects($this->any())
53  ->method('getValidator')
54  ->will($this->returnValue($validtorMock));
55 
56  $this->model = new \Magento\Framework\View\Element\Js\Cookie(
57  $this->contextMock,
58  $this->sessionConfigMock,
59  $this->ipValidatorMock,
60  []
61  );
62  }
63 
64  public function testInstanceOf()
65  {
66  $this->assertInstanceOf(\Magento\Framework\View\Element\Js\Cookie::class, $this->model);
67  }
68 
72  public function testGetDomain($domain, $isIp, $expectedResult)
73  {
74  $this->sessionConfigMock->expects($this->once())
75  ->method('getCookieDomain')
76  ->will($this->returnValue($domain));
77  $this->ipValidatorMock->expects($this->once())
78  ->method('isValid')
79  ->with($this->equalTo($domain))
80  ->will($this->returnValue($isIp));
81 
82  $result = $this->model->getDomain($domain);
83  $this->assertEquals($expectedResult, $result);
84  }
85 
89  public static function domainDataProvider()
90  {
91  return [
92  ['127.0.0.1', true, '127.0.0.1'],
93  ['example.com', false, '.example.com'],
94  ['.example.com', false, '.example.com'],
95  ];
96  }
97 
98  public function testGetPath()
99  {
100  $path = 'test_path';
101 
102  $this->sessionConfigMock->expects($this->once())
103  ->method('getCookiePath')
104  ->will($this->returnValue($path));
105 
106  $result = $this->model->getPath();
107  $this->assertEquals($path, $result);
108  }
109 
110  public function testGetLifetime()
111  {
112  $lifetime = 3600;
113  $this->sessionConfigMock->expects(static::once())
114  ->method('getCookieLifetime')
115  ->willReturn($lifetime);
116 
117  $this->assertEquals($lifetime, $this->model->getLifetime());
118  }
119 }