Skip to content

Commit

Permalink
minor #15746 Tests fix clockmock (ewgRa)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Sep 10, 2015
2 parents 6c9f86b + 6b21752 commit cfd8cc2
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ClockMock.php
Expand Up @@ -18,7 +18,22 @@ function time($asFloat = false)

namespace Symfony\Component\HttpFoundation\Tests;

function with_clock_mock($enable = null)
{
static $enabled;

if (null === $enable) {
return $enabled;
}

$enabled = $enable;
}

function time()
{
if (!with_clock_mock()) {
return \time();
}

return $_SERVER['REQUEST_TIME'];
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/CookieTest.php
Expand Up @@ -23,6 +23,18 @@
*/
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()
{
return array(
Expand Down
Expand Up @@ -18,6 +18,18 @@

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
* @dataProvider provideAllPreserveCase
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Stopwatch/Tests/ClockMock.php
Expand Up @@ -18,10 +18,27 @@ function microtime($asFloat = false)

namespace Symfony\Component\Stopwatch\Tests;

function with_clock_mock($enable = null)
{
static $enabled;

if (null === $enable) {
return $enabled;
}

$enabled = $enable;
}

function usleep($us)
{
static $now;

if (!with_clock_mock()) {
\usleep($us);

return;
}

if (null === $now) {
$now = \microtime(true);
}
Expand All @@ -31,6 +48,10 @@ function usleep($us)

function microtime($asFloat = false)
{
if (!with_clock_mock()) {
return \microtime($asFloat);
}

if (!$asFloat) {
return \microtime(false);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php
Expand Up @@ -24,6 +24,18 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase
{
const DELTA = 37;

public function setUp()
{
with_clock_mock(true);
parent::setUp();
}

public function tearDown()
{
with_clock_mock(false);
parent::tearDown();
}

public function testGetOrigin()
{
$event = new StopwatchEvent(12);
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php
Expand Up @@ -24,6 +24,18 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase
{
const DELTA = 20;

public function setUp()
{
with_clock_mock(true);
parent::setUp();
}

public function tearDown()
{
with_clock_mock(false);
parent::tearDown();
}

public function testStart()
{
$stopwatch = new Stopwatch();
Expand Down

0 comments on commit cfd8cc2

Please sign in to comment.