Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerFlushFormKeyTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Framework\App\PageCache\FormKey as CookieFormKey;
14 use PHPUnit\Framework\TestCase;
15 use PHPUnit_Framework_MockObject_MockObject as MockObject;
16 
17 class CustomerFlushFormKeyTest extends TestCase
18 {
22  private $cookieFormKey;
23 
27  private $customerSession;
28 
32  private $dataFormKey;
33 
34  protected function setUp()
35  {
36 
38  $this->cookieFormKey = $this->getMockBuilder(CookieFormKey::class)
39  ->disableOriginalConstructor()
40  ->getMock();
41 
43  $this->dataFormKey = $this->getMockBuilder(DataFormKey::class)
44  ->disableOriginalConstructor()
45  ->getMock();
46 
48  $this->customerSession = $this->getMockBuilder(Session::class)
49  ->disableOriginalConstructor()
50  ->setMethods(['getBeforeRequestParams', 'setBeforeRequestParams'])
51  ->getMock();
52  }
53 
61  public function testAroundFlushFormKey(
62  $beforeFormKey,
63  $currentFormKey,
64  $getFormKeyTimes,
65  $setBeforeParamsTimes
66  ) {
67  $observerDto = new Observer();
68  $observer = new FlushFormKey($this->cookieFormKey, $this->dataFormKey);
69  $plugin = new CustomerFlushFormKey($this->customerSession, $this->dataFormKey);
70 
71  $beforeParams['form_key'] = $beforeFormKey;
72 
73  $this->dataFormKey->expects($this->exactly($getFormKeyTimes))
74  ->method('getFormKey')
75  ->willReturn($currentFormKey);
76 
77  $this->customerSession->expects($this->once())
78  ->method('getBeforeRequestParams')
79  ->willReturn($beforeParams);
80 
81  $this->customerSession->expects($this->exactly($setBeforeParamsTimes))
82  ->method('setBeforeRequestParams')
83  ->with($beforeParams);
84 
85  $proceed = function ($observerDto) use ($observer) {
86  return $observer->execute($observerDto);
87  };
88 
89  $plugin->aroundExecute($observer, $proceed, $observerDto);
90  }
91 
97  public function aroundFlushFormKeyProvider()
98  {
99  return [
100  ['form_key_value', 'form_key_value', 2, 1],
101  ['form_old_key_value', 'form_key_value', 1, 0],
102  [null, 'form_key_value', 1, 0]
103  ];
104  }
105 }
testAroundFlushFormKey( $beforeFormKey, $currentFormKey, $getFormKeyTimes, $setBeforeParamsTimes)