8000 [Bridge/PhpUnit] Mock date() in ClockMock · symfony/symfony@e8ba79a · GitHub
[go: up one dir, main page]

Skip to content

Commit e8ba79a

Browse files
author
Dominic Tubach
committed
[Bridge/PhpUnit] Mock date() in ClockMock
As to the PHP documentation date() uses the value of time() as timestamp if none is given. date() has to be mocked in ClockMock as well for this still being true, otherwise \time() is used as default.
1 parent 3cfdc9e commit e8ba79a

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/Symfony/Bridge/PhpUnit/ClockMock.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
/**
1515
* @author Nicolas Grekas <p@tchwork.com>
16+
* @author Dominic Tubach <dominic.tubach@to.com>
1617
*/
1718
class ClockMock
1819
{
@@ -69,6 +70,15 @@ public static function microtime($asFloat = false)
6970
return sprintf('%0.6f %d', self::$now - (int) self::$now, (int) self::$now);
7071
}
7172

73+
public static function date($format, $timestamp = null)
74+
{
75+
if (null === $timestamp) {
76+
$timestamp = self::time();
77+
}
78+
79+
return \date($format, $timestamp);
80+
}
81+
7282
public static function register($class)
7383
{
7484
$self = get_called_class();
@@ -107,6 +117,11 @@ function usleep(\$us)
107117
return \\$self::usleep(\$us);
108118
}
109119
120+
function date(\$format, \$timestamp = null)
121+
{
122+
return \\$self::date(\$format, \$timestamp);
123+
}
124+
110125
EOPHP
111126
);
112127
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PhpUnit\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ClockMock;
16+
17+
/**
18+
* @author Dominic Tubach <dominic.tubach@to.com>
19+
*
20+
* @covers \Symfony\Bridge\PhpUnit\ClockMock
21+
*/
22+
class ClockMockTest extends TestCase
23+
{
24+
public static function setUpBeforeClass()
25+
{
26+
ClockMock::register(__CLASS__);
27+
}
28+
29+
protected function setUp()
30+
{
31+
ClockMock::withClockMock(1234567890.125);
32+
}
33+
34+
public function testTime()
35+
{
36+
$this->assertSame(1234567890, time());
37+
}
38+
39+
public function testSleep()
40+
{
41+
sleep(2);
42+
$this->assertSame(1234567892, time());
43+
}
44+
45+
public function testMicrotime()
46+
{
47+
$this->assertSame('0.125000 1234567890', microtime());
48+
}
49+
50+
public function testMicrotimeAsFloat()
51+
{
52+
$this->assertSame(1234567890.125, microtime(true));
53+
}
54+
55+
public function testUsleep()
56+
{
57+
usleep(2);
58+
$this->assertSame(1234567890.125002, microtime(true));
59+
}
60+
61+
public function testDate()
62+
{
63+
$this->assertSame('1234567890', date('U'));
64+
}
65+
}

0 commit comments

Comments
 (0)
0