minor #15746 Tests fix clockmock (ewgRa)

This PR was squashed before being merged into the 2.3 branch (closes #15746).

Discussion
----------

Tests fix clockmock

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

On my local computer tests for HttpCacheTest not passed, because ClockMock make side effects on another tests (it takes time from _SERVER['REQUEST_TIME'] and for my computer difference with real one time and this time was more than expected by HttpCacheTest, one part of code take time from mock, anothers - from native time function). This PR remove this side effects.

Commits
-------

6b21752 Tests fix clockmock
This commit is contained in:
Fabien Potencier 2015-09-10 13:45:38 +02:00
commit cfd8cc24ff
6 changed files with 84 additions and 0 deletions

View File

@ -18,7 +18,22 @@ function time($asFloat = false)
namespace Symfony\Component\HttpFoundation\Tests; namespace Symfony\Component\HttpFoundation\Tests;
function with_clock_mock($enable = null)
{
static $enabled;
if (null === $enable) {
return $enabled;
}
$enabled = $enable;
}
function time() function time()
{ {
if (!with_clock_mock()) {
return \time();
}
return $_SERVER['REQUEST_TIME']; return $_SERVER['REQUEST_TIME'];
} }

View File

@ -23,6 +23,18 @@ require_once __DIR__.'/ClockMock.php';
*/ */
class CookieTest extends \PHPUnit_Framework_TestCase class CookieTest extends \PHPUnit_Framework_TestCase
{ {
public function setUp()
{
with_clock_mock(true);
parent::setUp();
}
public function tearDown()
{
with_clock_mock(false);
parent::tearDown();
}
public function invalidNames() public function invalidNames()
{ {
return array( return array(

View File

@ -18,6 +18,18 @@ require_once __DIR__.'/ClockMock.php';
class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase
{ {
public function setUp()
{
with_clock_mock(true);
parent::setUp();
}
public function tearDown()
{
with_clock_mock(false);
parent::tearDown();
}
/** /**
* @covers Symfony\Component\HttpFoundation\ResponseHeaderBag::allPreserveCase * @covers Symfony\Component\HttpFoundation\ResponseHeaderBag::allPreserveCase
* @dataProvider provideAllPreserveCase * @dataProvider provideAllPreserveCase

View File

@ -18,10 +18,27 @@ function microtime($asFloat = false)
namespace Symfony\Component\Stopwatch\Tests; namespace Symfony\Component\Stopwatch\Tests;
function with_clock_mock($enable = null)
{
static $enabled;
if (null === $enable) {
return $enabled;
}
$enabled = $enable;
}
function usleep($us) function usleep($us)
{ {
static $now; static $now;
if (!with_clock_mock()) {
\usleep($us);
return;
}
if (null === $now) { if (null === $now) {
$now = \microtime(true); $now = \microtime(true);
} }
@ -31,6 +48,10 @@ function usleep($us)
function microtime($asFloat = false) function microtime($asFloat = false)
{ {
if (!with_clock_mock()) {
return \microtime($asFloat);
}
if (!$asFloat) { if (!$asFloat) {
return \microtime(false); return \microtime(false);
} }

View File

@ -24,6 +24,18 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase
{ {
const DELTA = 37; const DELTA = 37;
public function setUp()
{
with_clock_mock(true);
parent::setUp();
}
public function tearDown()
{
with_clock_mock(false);
parent::tearDown();
}
public function testGetOrigin() public function testGetOrigin()
{ {
$event = new StopwatchEvent(12); $event = new StopwatchEvent(12);

View File

@ -24,6 +24,18 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase
{ {
const DELTA = 20; const DELTA = 20;
public function setUp()
{
with_clock_mock(true);
parent::setUp();
}
public function tearDown()
{
with_clock_mock(false);
parent::tearDown();
}
public function testStart() public function testStart()
{ {
$stopwatch = new Stopwatch(); $stopwatch = new Stopwatch();