Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite8ba79a

Browse files
author
Dominic Tubach
committed
[Bridge/PhpUnit] Mock date() in ClockMock
As to the PHP documentation date() uses the value of time() as timestampif none is given. date() has to be mocked in ClockMock as well for thisstill being true, otherwise \time() is used as default.
1 parent3cfdc9e commite8ba79a

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
returnsprintf('%0.6f %d',self::$now - (int)self::$now, (int)self::$now);
7071
}
7172

73+
publicstaticfunctiondate($format,$timestamp =null)
74+
{
75+
if (null ===$timestamp) {
76+
$timestamp =self::time();
77+
}
78+
79+
return\date($format,$timestamp);
80+
}
81+
7282
publicstaticfunctionregister($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+
namespaceSymfony\Bridge\PhpUnit\Tests;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Bridge\PhpUnit\ClockMock;
16+
17+
/**
18+
* @author Dominic Tubach <dominic.tubach@to.com>
19+
*
20+
* @covers \Symfony\Bridge\PhpUnit\ClockMock
21+
*/
22+
class ClockMockTestextends TestCase
23+
{
24+
publicstaticfunctionsetUpBeforeClass()
25+
{
26+
ClockMock::register(__CLASS__);
27+
}
28+
29+
protectedfunctionsetUp()
30+
{
31+
ClockMock::withClockMock(1234567890.125);
32+
}
33+
34+
publicfunctiontestTime()
35+
{
36+
$this->assertSame(1234567890,time());
37+
}
38+
39+
publicfunctiontestSleep()
40+
{
41+
sleep(2);
42+
$this->assertSame(1234567892,time());
43+
}
44+
45+
publicfunctiontestMicrotime()
46+
{
47+
$this->assertSame('0.125000 1234567890',microtime());
48+
}
49+
50+
publicfunctiontestMicrotimeAsFloat()
51+
{
52+
$this->assertSame(1234567890.125,microtime(true));
53+
}
54+
55+
publicfunctiontestUsleep()
56+
{
57+
usleep(2);
58+
$this->assertSame(1234567890.125002,microtime(true));
59+
}
60+
61+
publicfunctiontestDate()
62+
{
63+
$this->assertSame('1234567890',date('U'));
64+
}
65+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp