Skip to content

[PhpUnit] Mock clock on @group time-sensitive annotations #16194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/Symfony/Bridge/PhpUnit/ClockMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ClockMock
{
private static $now;

public static function withClockMock($enable = null)
{
if (null === $enable) {
return null !== self::$now;
}

self::$now = is_numeric($enable) ? (float) $enable : ($enable ? microtime(true) : null);
}

public static function time()
{
if (null === self::$now) {
return \time();
}

return (int) self::$now;
}

public static function sleep($s)
{
if (null === self::$now) {
return \sleep($s);
}

self::$now += (int) $s;

return 0;
}

public static function usleep($us)
{
if (null === self::$now) {
return \usleep($us);
}

self::$now += $us / 1000000;
}

public static function microtime($asFloat = false)
{
if (null === self::$now) {
return \microtime($asFloat);
}

if ($asFloat) {
return self::$now;
}

return sprintf("%0.6f %d\n", $now - (int) $now, (int) self::$now);
}

public static function register($class)
{
$self = get_called_class();

$mockedNs = array(substr($class, 0, strrpos($class, '\\')));
if (strpos($class, '\\Tests\\')) {
$ns = str_replace('\\Tests\\', '\\', $class);
$mockedNs[] = substr($ns, 0, strrpos($ns, '\\'));
}
foreach ($mockedNs as $ns) {
if (function_exists($ns.'\time')) {
continue;
}
eval(<<<EOPHP
namespace $ns;

function time()
{
return \\$self::time();
}

function microtime(\$asFloat = false)
{
return \\$self::microtime(\$asFloat);
}

function sleep(\$s)
{
return \\$self::sleep(\$s);
}

function usleep(\$us)
{
return \\$self::usleep(\$us);
}

EOPHP
);
}
}

}
24 changes: 24 additions & 0 deletions src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SymfonyTestsListener extends \PHPUnit_Framework_BaseTestListener
private $skippedFile = false;
private $wasSkipped = array();
private $isSkipped = array();
private $testsStack = array();

public function __destruct()
{
Expand Down Expand Up @@ -82,4 +83,27 @@ public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $ti
$this->isSkipped[$class][$method] = 1;
}
}

public function startTest(\PHPUnit_Framework_Test $test)
{
if ($test instanceof \PHPUnit_Framework_TestCase) {
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName());

if (in_array('time-sensitive', $groups, true)) {
ClockMock::register(get_class($test));
ClockMock::withClockMock(true);
}
}
}

public function endTest(\PHPUnit_Framework_Test $test, $time)
{
if ($test instanceof \PHPUnit_Framework_TestCase) {
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName());

if (in_array('time-sensitive', $groups, true)) {
ClockMock::withClockMock(false);
}
}
}
}
41 changes: 0 additions & 41 deletions src/Symfony/Component/Console/Tests/ClockMock.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,13 @@

use Symfony\Component\Console\Helper\ProgressHelper;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Tests;

require_once __DIR__.'/../ClockMock.php';

/**
* @group legacy
* @group time-sensitive
*/
class LegacyProgressHelperTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
Tests\with_clock_mock(true);
}

protected function tearDown()
{
Tests\with_clock_mock(false);
}

public function testAdvance()
{
$progress = new ProgressHelper();
Expand Down
16 changes: 3 additions & 13 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,12 @@
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Tests;

require_once __DIR__.'/../ClockMock.php';

/**
* @group time-sensitive
*/
class ProgressBarTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
Tests\with_clock_mock(true);
}

protected function tearDown()
{
Tests\with_clock_mock(false);
}

public function testMultipleStart()
{
$bar = new ProgressBar($output = $this->getOutputStream());
Expand Down
88 changes: 0 additions & 88 deletions src/Symfony/Component/HttpFoundation/Tests/ClockMockTestCase.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/Symfony/Component/HttpFoundation/Tests/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*
* @author John Kary <john@johnkary.net>
* @author Hugo Hamon <hugo.hamon@sensio.com>
*
* @group time-sensitive
*/
class CookieTest extends ClockMockTestCase
class CookieTest extends \PHPUnit_Framework_TestCase
{
public function invalidNames()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\Cookie;

class ResponseHeaderBagTest extends ClockMockTestCase
/**
* @group time-sensitive
*/
class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Symfony\Component\HttpFoundation\ResponseHeaderBag::allPreserveCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;

use Symfony\Component\HttpFoundation\Session\Storage\Handler\LegacyPdoSessionHandler;
use Symfony\Component\HttpFoundation\Tests\ClockMockTestCase;

/**
* @group legacy
* @requires extension pdo_sqlite
* @group time-sensitive
*/
class LegacyPdoSessionHandlerTest extends ClockMockTestCase
class LegacyPdoSessionHandlerTest extends \PHPUnit_Framework_TestCase
{
private $pdo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;

use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler;
use Symfony\Component\HttpFoundation\Tests\ClockMockTestCase;

/**
* @requires extension memcache
* @group time-sensitive
*/
class MemcacheSessionHandlerTest extends ClockMockTestCase
class MemcacheSessionHandlerTest extends \PHPUnit_Framework_TestCase
{
const PREFIX = 'prefix_';
const TTL = 1000;
Expand Down
Loading