15
15
use Magento \Framework \Exception \InputException ;
16
16
use Magento \Framework \Stdlib \Cookie \FailureToSendException ;
17
17
use Magento \Framework \Stdlib \Cookie \CookieSizeLimitReachedException ;
18
+ use Magento \Framework \Phrase ;
19
+ use Magento \Framework \HTTP \Header as HttpHeader ;
20
+ use Psr \Log \LoggerInterface ;
18
21
// @codingStandardsIgnoreEnd
19
22
20
23
/**
21
24
* Test PhpCookieManager
25
+ *
26
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22
27
*/
23
28
class PhpCookieManagerTest extends \PHPUnit_Framework_TestCase
24
29
{
@@ -95,6 +100,16 @@ class PhpCookieManagerTest extends \PHPUnit_Framework_TestCase
95
100
*/
96
101
protected $ readerMock ;
97
102
103
+ /**
104
+ * @var LoggerInterface | \PHPUnit_Framework_MockObject_MockObject
105
+ */
106
+ protected $ loggerMock ;
107
+
108
+ /**
109
+ * @var HttpHeader | \PHPUnit_Framework_MockObject_MockObject
110
+ */
111
+ protected $ httpHeaderMock ;
112
+
98
113
/**
99
114
* @var array
100
115
*/
@@ -113,11 +128,18 @@ protected function setUp()
113
128
->disableOriginalConstructor ()
114
129
->getMock ();
115
130
$ this ->readerMock = $ this ->getMock (\Magento \Framework \Stdlib \Cookie \CookieReaderInterface::class);
131
+ $ this ->loggerMock = $ this ->getMockBuilder (LoggerInterface::class)
132
+ ->getMockForAbstractClass ();
133
+ $ this ->httpHeaderMock = $ this ->getMockBuilder (HttpHeader::class)
134
+ ->disableOriginalConstructor ()
135
+ ->getMock ();
116
136
$ this ->cookieManager = $ this ->objectManager ->getObject (
117
137
\Magento \Framework \Stdlib \Cookie \PhpCookieManager::class,
118
138
[
119
139
'scope ' => $ this ->scopeMock ,
120
140
'reader ' => $ this ->readerMock ,
141
+ 'logger ' => $ this ->loggerMock ,
142
+ 'httpHeader ' => $ this ->httpHeaderMock
121
143
]
122
144
);
123
145
@@ -503,11 +525,11 @@ public function testSetTooManyCookies()
503
525
\Magento \Framework \Stdlib \Cookie \PublicCookieMetadata::class
504
526
);
505
527
506
- $ cookieValue = 'some_value ' ;
528
+ $ userAgent = 'some_user_agent ' ;
507
529
508
530
// Set self::MAX_NUM_COOKIES number of cookies in superglobal $_COOKIE.
509
531
for ($ i = count ($ _COOKIE ); $ i < self ::MAX_NUM_COOKIES ; $ i ++) {
510
- $ _COOKIE ['test_cookie_ ' . $ i ] = ' some_value ' ;
532
+ $ _COOKIE ['test_cookie_ ' . $ i ] = self :: COOKIE_VALUE . ' _ ' . $ i ;
511
533
}
512
534
513
535
$ this ->scopeMock ->expects ($ this ->once ())
@@ -517,19 +539,22 @@ public function testSetTooManyCookies()
517
539
$ this ->returnValue ($ publicCookieMetadata )
518
540
);
519
541
520
- try {
521
- $ this ->cookieManager ->setPublicCookie (
522
- self ::MAX_COOKIE_SIZE_TEST_NAME ,
523
- $ cookieValue ,
524
- $ publicCookieMetadata
525
- );
526
- $ this ->fail ('Failed to throw exception of too many cookies. ' );
527
- } catch (CookieSizeLimitReachedException $ e ) {
528
- $ this ->assertEquals (
529
- 'Unable to send the cookie. Maximum number of cookies would be exceeded. ' ,
530
- $ e ->getMessage ()
542
+ $ this ->httpHeaderMock ->expects ($ this ->any ())
543
+ ->method ('getHttpUserAgent ' )
544
+ ->willReturn ($ userAgent );
545
+
546
+ $ this ->loggerMock ->expects ($ this ->once ())
547
+ ->method ('warning ' )
548
+ ->with (
549
+ new Phrase ('Unable to send the cookie. Maximum number of cookies would be exceeded. ' ),
550
+ array_merge ($ _COOKIE , ['user-agent ' => $ userAgent ])
531
551
);
532
- }
552
+
553
+ $ this ->cookieManager ->setPublicCookie (
554
+ self ::MAX_COOKIE_SIZE_TEST_NAME ,
555
+ self ::COOKIE_VALUE ,
556
+ $ publicCookieMetadata
557
+ );
533
558
}
534
559
535
560
/**
0 commit comments