8000 [PhpUnitBridge] Add ClockMockedTestCase · symfony/symfony@a997fbc · GitHub
[go: up one dir, main page]

Skip to content

Commit a997fbc

Browse files
[PhpUnitBridge] Add ClockMockedTestCase
1 parent cfbda28 commit a997fbc

16 files changed

+144
-257
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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;
13+
14+
class ClockMockedTestCase extends \PHPUnit_Framework_TestCase
EDBE
15+
{
16+
private static $now;
17+
18+
protected static $clockMockedNamespaces = array();
19+
20+
public static function setUpBeforeClass()
21+
{
22+
$self = get_called_class();
23+
if (!$clockMockedNamespaces = static::$clockMockedNamespaces) {
24+
$ns = substr($self, 0, strrpos($self, '\\'));
25+
if (strpos($ns, '\\Tests\\')) {
26+
$clockMockedNamespaces = array(
27+
$ns,
28+
str_replace('\\Tests\\', '\\', $ns),
29+
);
30+
}
31+
}
32+
foreach ($clockMockedNamespaces as $ns) {
33+
if (function_exists($ns.'\time')) {
34+
continue;
35+
}
36+
eval(<<<EOPHP
37+
namespace $ns;
38+
39+
function time()
40+
{
41+
return \\$self::time();
42+
}
43+
44+
function microtime(\$asFloat = false)
45+
{
46+
return \\$self::time(\$asFloat);
47+
}
48+
49+
function sleep(\$s)
50+
{
51+
return \\$self::sleep(\$s);
52+
}
53+
54+
function usleep(\$us)
55+
{
56+
return \\$self::sleep(\$us);
57+
}
58+
59+
EOPHP
60+
);
61+
}
62+
}
63+
64+
protected function setUp()
65+
{
66+
self::withClockMock(true);
67+
}
68+
69+
protected function tearDown()
70+
{
71+
self::withClockMock(false);
72+
}
73+
74+
protected static function withClockMock($enable)
75+
{
76+
self::$now = $enable ? microtime(true) : null;
77+
}
78+
79+
public static function time()
80+
{
81+
if (!self::$now) {
82+
return \time();
83+
}
84+
85+
return (int) self::$now;
86+
}
87+
88+
public static function sleep($s)
89+
{
90+
if (!self::$now) {
91+
return \sleep($s);
92+
}
93+
94+
self::$now += (int) $s;
95+
96+
return 0;
97+
}
98+
99+
public static function usleep($us)
100+
{
101+
if (!self::$now) {
102+
return \usleep($us);
103+
}
104+
105+
self::$now += $us / 1000000;
106+
}
107+
108+
public static function microtime($asFloat = false)
109+
{
110+
if (!self::$now) {
111+
return \microtime($asFloat);
112+
}
113+
114+
if ($asFloat) {
115+
return self::$now;
116+
}
117+
118+
return sprintf("%0.6f %d\n", $now - (int) $now, (int) self::$now);
119+
}
120+
}

src/Symfony/Component/Console/Tests/ClockMock.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/Symfony/Component/Console/Tests/Helper/LegacyProgressHelperTest.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,13 @@
1313

1414
use Symfony\Component\Console\Helper\ProgressHelper;
1515
use Symfony\Component\Console\Output\StreamOutput;
16-
use Symfony\Component\Console\Tests;
17-
18-
require_once __DIR__.'/../ClockMock.php';
16+
use Symfony\Bridge\PhpUnit\ClockMockedTestCase;
1917

2018
/**
2119
* @group legacy
2220
*/
23-
class LegacyProgressHelperTest extends \PHPUnit_Framework_TestCase
21+
class LegacyProgressHelperTest extends ClockMockedTestCase
2422
{
25-
protected function setUp()
26-
{
27-
Tests\with_clock_mock(true);
28-
}
29-
30-
protected function tearDown()
31-
{
32-
Tests\with_clock_mock(false);
33-
}
34-
3523
public function testAdvance()
3624
{
3725
$progress = new ProgressHelper();

src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,10 @@
1414
use Symfony\Component\Console\Helper\ProgressBar;
1515
use Symfony\Component\Console\Helper\Helper;
1616
use Symfony\Component\Console\Output\StreamOutput;
17-
use Symfony\Component\Console\Tests;
17+
use Symfony\Bridge\PhpUnit\ClockMockedTestCase;
1818

19-
require_once __DIR__.'/../ClockMock.php';
20-
21-
class ProgressBarTest extends \PHPUnit_Framework_TestCase
19+
class ProgressBarTest extends ClockMockedTestCase
2220
{
23-
protected function setUp()
24-
{
25-
Tests\with_clock_mock(true);
26-
}
27-
28-
protected function tearDown()
29-
{
30-
Tests\with_clock_mock(false);
31-
}
32-
3321
public function testMultipleStart()
3422
{
3523
$bar = new ProgressBar($output = $this->getOutputStream());

src/Symfony/Component/HttpFoundation/Tests/ClockMockTestCase.php

Lines changed: 0 additions & 88 deletions
This file was deleted.

src/Symfony/Component/HttpFoundation/Tests/CookieTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
namespace Symfony\Component\HttpFoundation\Tests;
1313

1414
use Symfony\Component\HttpFoundation\Cookie;
15+
use Symfony\Bridge\PhpUnit\ClockMockedTestCase;
1516

1617
/**
1718
* CookieTest.
1819
*
1920
* @author John Kary <john@johnkary.net>
2021
* @author Hugo Hamon <hugo.hamon@sensio.com>
2122
*/
22-
class CookieTest extends ClockMockTestCase
23+
class CookieTest extends ClockMockedTestCase
2324
{
2425
public function invalidNames()
2526
{

src/Symfony/Component/HttpFoundation/Tests/ResponseHeaderBagTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
1515
use Symfony\Component\HttpFoundation\Cookie;
16+
use Symfony\Bridge\PhpUnit\ClockMockedTestCase;
1617

17-
class ResponseHeaderBagTest extends ClockMockTestCase
18+
class ResponseHeaderBagTest extends ClockMockedTestCase
1819
{
1920
/**
2021
* @covers Symfony\Component\HttpFoundation\ResponseHeaderBag::allPreserveCase

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/LegacyPdoSessionHandlerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
1313

1414
use Symfony\Component\HttpFoundation\Session\Storage\Handler\LegacyPdoSessionHandler;
15-
use Symfony\Component\HttpFoundation\Tests\ClockMockTestCase;
15+
use Symfony\Bridge\PhpUnit\ClockMockedTestCase;
1616

1717
/**
1818
* @group legacy
1919
* @requires extension pdo_sqlite
2020
*/
21-
class LegacyPdoSessionHandlerTest extends ClockMockTestCase
21+
class LegacyPdoSessionHandlerTest extends ClockMockedTestCase
2222
{
2323
private $pdo;
2424

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcacheSessionHandlerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
1313

1414
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler;
15-
use Symfony\Component\HttpFoundation\Tests\ClockMockTestCase;
15+
use Symfony\Bridge\PhpUnit\ClockMockedTestCase;
1616

1717
/**
1818
* @requires extension memcache
1919
*/
20-
class MemcacheSessionHandlerTest extends ClockMockTestCase
20+
class MemcacheSessionHandlerTest extends ClockMockedTestCase
2121
{
2222
const PREFIX = 'prefix_';
2323
const TTL = 1000;

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcachedSessionHandlerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
1313

1414
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler;
15-
use Symfony\Component\HttpFoundation\Tests\ClockMockTestCase;
15+
use Symfony\Bridge\PhpUnit\ClockMockedTestCase;
1616

1717
/**
1818
* @requires extension memcached
1919
*/
20-
class MemcachedSessionHandlerTest extends ClockMockTestCase
20+
class MemcachedSessionHandlerTest extends ClockMockedTestCase
2121
{
2222
const PREFIX = 'prefix_';
2323
const TTL = 1000;

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
1313

1414
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;
15-
use Symfony\Component\HttpFoundation\Tests\ClockMockTestCase;
15+
use Symfony\Bridge\PhpUnit\ClockMockedTestCase;
1616

1717
/**
1818
* @author Markus Bachmann <markus.bachmann@bachi.biz>
1919
* @requires extension mongo
2020
*/
21-
class MongoDbSessionHandlerTest extends ClockMockTestCase
21+
class MongoDbSessionHandlerTest extends ClockMockedTestCase
2222
{
2323
/**
2424
* @var \PHPUnit_Framework_MockObject_MockObject

0 commit comments

Comments
 (0)
0