@@ -27,29 +27,42 @@ class ValidatorTest extends \PHPUnit\Framework\TestCase
27
27
*/
28
28
private $ dateTimeMock ;
29
29
30
+ /**@var \PHPUnit\Framework\MockObject\MockObject|\Magento\Framework\Stdlib\DateTime\TimezoneInterface */
31
+ private $ timezoneMock ;
32
+
30
33
protected function setUp ()
31
34
{
32
35
$ objectManager = new ObjectManager ($ this );
33
36
$ this ->dateTimeMock =
34
37
$ this ->createPartialMock (\Magento \Framework \Stdlib \DateTime \DateTime::class, ['gmtTimestamp ' ]);
38
+ $ this ->timezoneMock =
39
+ $ this ->createPartialMock (
40
+ \Magento \Framework \Stdlib \DateTime \Timezone::class,
41
+ ['date ' , 'convertConfigTimeToUtc ' ]
42
+ );
35
43
$ this ->validator = $ objectManager ->getObject (
36
44
\Magento \Security \Model \UserExpiration \Validator::class,
37
- ['dateTime ' => $ this ->dateTimeMock ]
45
+ ['dateTime ' => $ this ->dateTimeMock , ' timezone ' => $ this -> timezoneMock ]
38
46
);
39
47
}
40
48
41
49
public function testWithPastDate ()
42
50
{
43
- $ currentTime = '1562447687 ' ;
44
- $ expireTime = '1561324487 ' ;
45
- $ testDate = new \DateTime ();
46
- $ testDate ->modify ('-10 days ' );
47
- $ this ->dateTimeMock ->expects (static ::exactly (2 ))->method ('gmtTimestamp ' )
48
- ->willReturnOnConsecutiveCalls (
49
- $ currentTime ,
50
- $ expireTime
51
- );
52
- static ::assertFalse ($ this ->validator ->isValid ($ testDate ->format ('Y-m-d H:i:s ' )));
51
+ /** @var \DateTime|\PHPUnit_Framework_MockObject_MockObject $dateObject */
52
+ $ dateObject = $ this ->createMock (\DateTime::class);
53
+ $ this ->timezoneMock ->expects (static ::once ())
54
+ ->method ('date ' )
55
+ ->will (static ::returnValue ($ dateObject ));
56
+
57
+ $ currentDate = new \DateTime ();
58
+ $ currentDate = $ currentDate ->getTimestamp ();
59
+ $ expireDate = new \DateTime ();
60
+ $ expireDate ->modify ('-10 days ' );
61
+
62
+ $ this ->dateTimeMock ->expects (static ::once ())->method ('gmtTimestamp ' )->willReturn ($ currentDate );
63
+ $ this ->timezoneMock ->expects (static ::once ())->method ('date ' )->willReturn ($ expireDate );
64
+ $ dateObject ->expects (static ::once ())->method ('getTimestamp ' )->willReturn ($ expireDate ->getTimestamp ());
65
+ static ::assertFalse ($ this ->validator ->isValid ($ expireDate ->format ('Y-m-d H:i:s ' )));
53
66
static ::assertContains (
54
67
'"Expiration date" must be later than the current date. ' ,
55
68
$ this ->validator ->getMessages ()
@@ -58,16 +71,20 @@ public function testWithPastDate()
58
71
59
72
public function testWithFutureDate ()
60
73
{
61
- $ currentTime = '1562447687 ' ;
62
- $ expireTime = '1563290841 ' ;
63
- $ testDate = new \DateTime ();
64
- $ testDate ->modify ('+10 days ' );
65
- $ this ->dateTimeMock ->expects (static ::exactly (2 ))->method ('gmtTimestamp ' )
66
- ->willReturnOnConsecutiveCalls (
67
- $ currentTime ,
68
- $ expireTime
69
- );
70
- static ::assertTrue ($ this ->validator ->isValid ($ testDate ->format ('Y-m-d H:i:s ' )));
74
+ /** @var \DateTime|\PHPUnit_Framework_MockObject_MockObject $dateObject */
75
+ $ dateObject = $ this ->createMock (\DateTime::class);
76
+ $ this ->timezoneMock ->expects (static ::once ())
77
+ ->method ('date ' )
78
+ ->will (static ::returnValue ($ dateObject ));
79
+ $ currentDate = new \DateTime ();
80
+ $ currentDate = $ currentDate ->getTimestamp ();
81
+ $ expireDate = new \DateTime ();
82
+ $ expireDate ->modify ('+10 days ' );
83
+
84
+ $ this ->dateTimeMock ->expects (static ::once ())->method ('gmtTimestamp ' )->willReturn ($ currentDate );
85
+ $ this ->timezoneMock ->expects (static ::once ())->method ('date ' )->willReturn ($ expireDate );
86
+ $ dateObject ->expects (static ::once ())->method ('getTimestamp ' )->willReturn ($ expireDate ->getTimestamp ());
87
+ static ::assertTrue ($ this ->validator ->isValid ($ expireDate ->format ('Y-m-d H:i:s ' )));
71
88
static ::assertEquals ([], $ this ->validator ->getMessages ());
72
89
}
73
90
}
0 commit comments