Skip to content

Instantly share code, notes, and snippets.

@ezzatron
Created October 29, 2014 01:27
Show Gist options
  • Save ezzatron/1c9ce238bd7ef6a054b2 to your computer and use it in GitHub Desktop.
Save ezzatron/1c9ce238bd7ef6a054b2 to your computer and use it in GitHub Desktop.
Test runner syntax ideas
<?php
/*
* This file is part of the Phony package.
*
* Copyright © 2014 Erin Millard
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
namespace Eloquent\Phony\Clock;
use PHPUnit_Framework_TestCase;
use ReflectionClass;
class SystemClockTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
$time = 0.123;
$this->microtime = function ($isFloat) use (&$time) {
if (true === $isFloat) {
$currentTime = $time;
$time += 1.0;
return $currentTime;
}
return 'invalid';
};
$this->subject = new SystemClock($this->microtime);
}
public function testTime()
{
$this->assertSame(0.123, $this->subject->time());
$this->assertSame(1.123, $this->subject->time());
$this->assertSame(2.123, $this->subject->time());
}
public function testTimeReal()
{
$this->subject = new SystemClock();
$this->assertInternalType('float', $this->subject->time());
}
public function testInstance()
{
$class = get_class($this->subject);
$reflector = new ReflectionClass($class);
$property = $reflector->getProperty('instance');
$property->setAccessible(true);
$property->setValue(null, null);
$instance = $class::instance();
$this->assertInstanceOf($class, $instance);
$this->assertSame($instance, $class::instance());
}
}
<?php
/*
* This file is part of the Phony package.
*
* Copyright © 2014 Erin Millard
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
namespace Eloquent\Phony\Clock;
use Eloquent\Phony\Clock\SystemClock;
use ReflectionClass;
describe('SystemClock', function ($t, $s) {
describe('with a mock microtime() implementation', function () {
beforeEach(function ($t, &$s) {
$time = 0.123;
$microtime = function ($isFloat) use (&$time) {
if (true === $isFloat) {
$currentTime = $time;
$time += 1.0;
return $currentTime;
}
return 'invalid';
};
$s = new SystemClock($microtime);
});
it('calls microtime() to get the current time', function ($t) {
expect($s->time())->toBe(0.123);
expect($s->time())->toBe(1.123);
expect($s->time())->toBe(2.123);
});
});
describe('with a real microtime() implementation', function () {
beforeEach(function ($t, &$s) {
$s = new SystemClock();
});
it('returns a float', function ($t, $s) {
expect($s->time())->toBeA('float');
});
});
it('provides a static instance via ::instance()', function ($t, $s) {
$class = get_class($s);
$reflector = new ReflectionClass($class);
$property = $reflector->getProperty('instance');
$property->setAccessible(true);
$property->setValue(null, null);
$instance = $class::instance();
expect($instance)->toBeA($class);
expect($class::instance())->toBe($instance);
});
});
<?php
/*
* This file is part of the Phony package.
*
* Copyright © 2014 Erin Millard
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
namespace Eloquent\Phony\Clock;
use Eloquent\Phony\Clock\SystemClock;
use ReflectionClass;
// @describe SystemClock
// @describe with a mock microtime() implementation
// @beforeEach
$time = 0.123;
$microtime = function ($isFloat) use (&$time) {
if (true === $isFloat) {
$currentTime = $time;
$time += 1.0;
return $currentTime;
}
return 'invalid';
};
$s = new SystemClock($microtime);
// @it calls microtime() to get the current time
expect($s->time())->toBe(0.123);
expect($s->time())->toBe(1.123);
expect($s->time())->toBe(2.123);
// @describe with a real microtime() implementation
// @beforeEach
$s = new SystemClock();
// @it returns a float
expect($s->time())->toBeA('float');
// @it provides a static instance via ::instance()
$class = get_class($s);
$reflector = new ReflectionClass($class);
$property = $reflector->getProperty('instance');
$property->setAccessible(true);
$property->setValue(null, null);
$instance = $class::instance();
expect($instance)->toBeA($class);
expect($class::instance())->toBe($instance);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment