Skip to content

Commit 7edc2d4

Browse files
committed
Add Test CustomerFlushFormKey Plugin
1 parent 03ac5b5 commit 7edc2d4

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Model\Plugin;
7+
8+
use Magento\Customer\Model\Plugin\CustomerFlushFormKey;
9+
use Magento\Customer\Model\Session;
10+
use Magento\Framework\App\PageCache\FormKey as CookieFormKey;
11+
use Magento\Framework\Data\Form\FormKey as DataFormKey;
12+
use Magento\PageCache\Observer\FlushFormKey;
13+
use PHPUnit\Framework\TestCase;
14+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
15+
16+
class CustomerFlushFormKeyTest extends TestCase
17+
{
18+
const CLOSURE_VALUE = 'CLOSURE';
19+
20+
/**
21+
* @var CookieFormKey | MockObject
22+
*/
23+
private $cookieFormKey;
24+
25+
/**
26+
* @var Session | MockObject
27+
*/
28+
private $customerSession;
29+
30+
/**
31+
* @var DataFormKey | MockObject
32+
*/
33+
private $dataFormKey;
34+
35+
/**
36+
* @var \Closure
37+
*/
38+
private $closure;
39+
40+
protected function setUp()
41+
{
42+
43+
/** @var CookieFormKey | MockObject */
44+
$this->cookieFormKey = $this->getMockBuilder(CookieFormKey::class)
45+
->disableOriginalConstructor()
46+
->getMock();
47+
48+
/** @var DataFormKey | MockObject */
49+
$this->dataFormKey = $this->getMockBuilder(DataFormKey::class)
50+
->disableOriginalConstructor()
51+
->getMock();
52+
53+
/** @var Session | MockObject */
54+
$this->customerSession = $this->getMockBuilder(Session::class)
55+
->disableOriginalConstructor()
56+
->setMethods(['getBeforeRequestParams', 'setBeforeRequestParams'])
57+
->getMock();
58+
59+
$this->closure = function () {
60+
return static::CLOSURE_VALUE;
61+
};
62+
}
63+
64+
/**
65+
* @dataProvider aroundFlushFormKeyProvider
66+
* @param $beforeFormKey
67+
* @param $currentFormKey
68+
* @param $getFormKeyTimes
69+
* @param $setBeforeParamsTimes
70+
*/
71+
public function testAroundFlushFormKey(
72+
$beforeFormKey,
73+
$currentFormKey,
74+
$getFormKeyTimes,
75+
$setBeforeParamsTimes
76+
) {
77+
$observer = new FlushFormKey($this->cookieFormKey, $this->dataFormKey);
78+
$plugin = new CustomerFlushFormKey($this->customerSession, $this->dataFormKey);
79+
80+
$beforeParams['form_key'] = $beforeFormKey;
81+
82+
$this->dataFormKey->expects($this->exactly($getFormKeyTimes))
83+
->method('getFormKey')
84+
->willReturn($currentFormKey);
85+
86+
$this->customerSession->expects($this->once())
87+
->method('getBeforeRequestParams')
88+
->willReturn($beforeParams);
89+
90+
$this->customerSession->expects($this->exactly($setBeforeParamsTimes))
91+
->method('setBeforeRequestParams')
92+
->with($beforeParams);
93+
94+
$plugin->aroundExecute($observer, $this->closure, $observer);
95+
}
96+
97+
/**
98+
* Data provider for testAroundFlushFormKey
99+
*
100+
* @return array
101+
*/
102+
public function aroundFlushFormKeyProvider()
103+
{
104+
return [
105+
['form_key_value', 'form_key_value', 2, 1],
106+
['form_old_key_value', 'form_key_value', 1, 0],
107+
[null, 'form_key_value', 1, 0]
108+
];
109+
}
110+
}

0 commit comments

Comments
 (0)