Skip to content

Commit 3c4004a

Browse files
digitalrisedorsetglo63652
authored andcommitted
Moved unit tests to match latest commit making the page cache identifier logic external to lib folder
1 parent 1fd33a8 commit 3c4004a

File tree

2 files changed

+226
-37
lines changed

2 files changed

+226
-37
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageCache\Test\Unit\Model\App\Request\Http;
9+
10+
use Magento\Framework\App\Http\Context;
11+
use Magento\Framework\App\PageCache\Identifier;
12+
use Magento\Framework\App\Request\Http as HttpRequest;
13+
use Magento\Framework\Serialize\Serializer\Json;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\PageCache\Model\App\Request\Http\IdentifierStoreReader;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class IdentifierTest extends TestCase
20+
{
21+
/**
22+
* Test value for cache vary string
23+
*/
24+
const VARY = '123';
25+
26+
/**
27+
* @var ObjectManager
28+
*/
29+
private $objectManager;
30+
31+
/**
32+
* @var Context|MockObject
33+
*/
34+
private $contextMock;
35+
36+
/**
37+
* @var HttpRequest|MockObject
38+
*/
39+
private $requestMock;
40+
41+
/**
42+
* @var Identifier
43+
*/
44+
private $model;
45+
46+
/**
47+
* @var Json|MockObject
48+
*/
49+
private $serializerMock;
50+
/**
51+
* @var IdentifierStoreReader|MockObject
52+
*/
53+
private $identifierStoreReader;
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
protected function setUp(): void
59+
{
60+
$this->objectManager = new ObjectManager($this);
61+
$this->contextMock = $this->getMockBuilder(Context::class)
62+
->disableOriginalConstructor()
63+
->getMock();
64+
65+
$this->requestMock = $this->getMockBuilder(HttpRequest::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
69+
$this->serializerMock = $this->getMockBuilder(Json::class)
70+
->onlyMethods(['serialize'])
71+
->disableOriginalConstructor()
72+
->getMock();
73+
$this->serializerMock->expects($this->any())
74+
->method('serialize')
75+
->willReturnCallback(
76+
function ($value) {
77+
return json_encode($value);
78+
}
79+
);
80+
81+
$this->identifierStoreReader = $this->getMockBuilder(IdentifierStoreReader::class)
82+
->onlyMethods(['getPageTagsWithStoreCacheTags'])
83+
->disableOriginalConstructor()
84+
->getMock();
85+
86+
87+
$this->model = $this->objectManager->getObject(
88+
Identifier::class,
89+
[
90+
'request' => $this->requestMock,
91+
'context' => $this->contextMock,
92+
'serializer' => $this->serializerMock,
93+
'identifierStoreReader' => $this->identifierStoreReader
94+
]
95+
);
96+
parent::setUp();
97+
}
98+
99+
/**
100+
* @return void
101+
*/
102+
public function testSecureDifferentiator(): void
103+
{
104+
$this->requestMock
105+
->method('isSecure')
106+
->willReturnOnConsecutiveCalls(true, false);
107+
$this->requestMock->method('getUriString')
108+
->willReturn('http://example.com/path/');
109+
$this->contextMock->method('getVaryString')->willReturn(self::VARY);
110+
111+
$this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback(
112+
function ($value) {
113+
return $value;
114+
}
115+
);
116+
117+
$valueWithSecureRequest = $this->model->getValue();
118+
$valueWithInsecureRequest = $this->model->getValue();
119+
$this->assertNotEquals($valueWithSecureRequest, $valueWithInsecureRequest);
120+
}
121+
122+
/**
123+
* @return void
124+
*/
125+
public function testDomainDifferentiator(): void
126+
{
127+
$this->requestMock->method('isSecure')->willReturn(true);
128+
$this->requestMock
129+
->method('getUriString')
130+
->willReturnOnConsecutiveCalls('http://example.com/path/', 'http://example.net/path/');
131+
$this->contextMock->method('getVaryString')->willReturn(self::VARY);
132+
133+
$this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback(
134+
function ($value) {
135+
return $value;
136+
}
137+
);
138+
139+
$valueDomain1 = $this->model->getValue();
140+
$valueDomain2 = $this->model->getValue();
141+
$this->assertNotEquals($valueDomain1, $valueDomain2);
142+
}
143+
144+
/**
145+
* @return void
146+
*/
147+
public function testPathDifferentiator(): void
148+
{
149+
$this->requestMock->method('isSecure')->willReturn(true);
150+
$this->requestMock
151+
->method('getUriString')
152+
->willReturnOnConsecutiveCalls('http://example.com/path/', 'http://example.com/path1/');
153+
$this->contextMock->method('getVaryString')->willReturn(self::VARY);
154+
155+
$this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback(
156+
function ($value) {
157+
return $value;
158+
}
159+
);
160+
161+
$valuePath1 = $this->model->getValue();
162+
$valuePath2 = $this->model->getValue();
163+
$this->assertNotEquals($valuePath1, $valuePath2);
164+
}
165+
166+
/**
167+
* @param $cookieExists
168+
*
169+
* @return void
170+
* @dataProvider trueFalseDataProvider
171+
*/
172+
public function testVaryStringSource($cookieExists): void
173+
{
174+
$this->requestMock->method('get')->willReturn($cookieExists ? 'vary-string-from-cookie' : null);
175+
$this->contextMock->expects($cookieExists ? $this->never() : $this->once())->method('getVaryString');
176+
$this->model->getValue();
177+
}
178+
179+
/**
180+
* @return array
181+
*/
182+
public function trueFalseDataProvider(): array
183+
{
184+
return [[true], [false]];
185+
}
186+
187+
/**
188+
* Test get identifier value.
189+
*
190+
* @return void
191+
*/
192+
public function testGetValue(): void
193+
{
194+
$this->requestMock->expects($this->any())
195+
->method('isSecure')
196+
->willReturn(true);
197+
198+
$this->requestMock->expects($this->any())
199+
->method('getUriString')
200+
->willReturn('http://example.com/path1/');
201+
202+
$this->contextMock->expects($this->any())
203+
->method('getVaryString')
204+
->willReturn(self::VARY);
205+
206+
$this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback(
207+
function ($value) {
208+
return $value;
209+
}
210+
);
211+
212+
$this->assertEquals(
213+
sha1(
214+
json_encode(
215+
[
216+
true,
217+
'http://example.com/path1/',
218+
self::VARY
219+
]
220+
)
221+
),
222+
$this->model->getValue()
223+
);
224+
}
225+
}

lib/internal/Magento/Framework/App/Test/Unit/PageCache/IdentifierTest.php

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Framework\App\Request\Http as HttpRequest;
1313
use Magento\Framework\Serialize\Serializer\Json;
1414
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15-
use Magento\PageCache\Model\App\Request\Http\IdentifierStoreReader;
1615
use PHPUnit\Framework\MockObject\MockObject;
1716
use PHPUnit\Framework\TestCase;
1817

@@ -47,10 +46,6 @@ class IdentifierTest extends TestCase
4746
* @var Json|MockObject
4847
*/
4948
private $serializerMock;
50-
/**
51-
* @var IdentifierStoreReader|MockObject
52-
*/
53-
private $identifierStoreReader;
5449

5550
/**
5651
* @inheritdoc
@@ -78,19 +73,12 @@ function ($value) {
7873
}
7974
);
8075

81-
$this->identifierStoreReader = $this->getMockBuilder(IdentifierStoreReader::class)
82-
->onlyMethods(['getPageTagsWithStoreCacheTags'])
83-
->disableOriginalConstructor()
84-
->getMock();
85-
86-
8776
$this->model = $this->objectManager->getObject(
8877
Identifier::class,
8978
[
9079
'request' => $this->requestMock,
9180
'context' => $this->contextMock,
92-
'serializer' => $this->serializerMock,
93-
'identifierStoreReader' => $this->identifierStoreReader
81+
'serializer' => $this->serializerMock
9482
]
9583
);
9684
parent::setUp();
@@ -108,12 +96,6 @@ public function testSecureDifferentiator(): void
10896
->willReturn('http://example.com/path/');
10997
$this->contextMock->method('getVaryString')->willReturn(self::VARY);
11098

111-
$this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback(
112-
function ($value) {
113-
return $value;
114-
}
115-
);
116-
11799
$valueWithSecureRequest = $this->model->getValue();
118100
$valueWithInsecureRequest = $this->model->getValue();
119101
$this->assertNotEquals($valueWithSecureRequest, $valueWithInsecureRequest);
@@ -130,12 +112,6 @@ public function testDomainDifferentiator(): void
130112
->willReturnOnConsecutiveCalls('http://example.com/path/', 'http://example.net/path/');
131113
$this->contextMock->method('getVaryString')->willReturn(self::VARY);
132114

133-
$this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback(
134-
function ($value) {
135-
return $value;
136-
}
137-
);
138-
139115
$valueDomain1 = $this->model->getValue();
140116
$valueDomain2 = $this->model->getValue();
141117
$this->assertNotEquals($valueDomain1, $valueDomain2);
@@ -152,12 +128,6 @@ public function testPathDifferentiator(): void
152128
->willReturnOnConsecutiveCalls('http://example.com/path/', 'http://example.com/path1/');
153129
$this->contextMock->method('getVaryString')->willReturn(self::VARY);
154130

155-
$this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback(
156-
function ($value) {
157-
return $value;
158-
}
159-
);
160-
161131
$valuePath1 = $this->model->getValue();
162132
$valuePath2 = $this->model->getValue();
163133
$this->assertNotEquals($valuePath1, $valuePath2);
@@ -203,12 +173,6 @@ public function testGetValue(): void
203173
->method('getVaryString')
204174
->willReturn(self::VARY);
205175

206-
$this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback(
207-
function ($value) {
208-
return $value;
209-
}
210-
);
211-
212176
$this->assertEquals(
213177
sha1(
214178
json_encode(

0 commit comments

Comments
 (0)