From 0c7b639df105541f6d13667f2906158f10fc6a2e Mon Sep 17 00:00:00 2001 From: Xenofon Spafaridis Date: Thu, 21 Jan 2016 00:51:00 +0200 Subject: [PATCH] Add timestamp() and microtime() to globals --- src/Globals.php | 15 +++++++++++---- tests/src/GlobalsTest.php | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Globals.php b/src/Globals.php index 8079a81..d459dce 100644 --- a/src/Globals.php +++ b/src/Globals.php @@ -74,6 +74,14 @@ protected static function initializeGlobals() static::$globals->{'rand-boolean'} = function () { return (boolean)rand(0, 1); }; + + static::$globals->{'timestamp'} = function () { + return time(); + }; + + static::$globals->{'microtime'} = function ($get_as_float = false) { + return microtime((bool)$get_as_float); + }; } /** @@ -108,8 +116,8 @@ public static function exists($key) * * Globals::get('myArray[1]'); //Get second array element * ``` - * @throws Exception When expression key is invalid. - * @throws Phramework\Exceptions\NotFoundException When key is not found. + * @throws \Exception When expression key is invalid. + * @throws \Phramework\Exceptions\NotFoundException When key is not found. */ public static function get($key = null, $operators = null) { @@ -178,7 +186,7 @@ public static function get($key = null, $operators = null) * Globals::get('dots()'); //Will return a string of 4 dots * Globals::get('dots(10)'); //Will return a string of 10 dots * ``` - * @throws Exception When key is invalid, *see Expression::PATTERN_KEY* + * @throws \Exception When key is invalid, *see Expression::PATTERN_KEY* */ public static function set($key, $value) { @@ -202,7 +210,6 @@ public static function toString() $return = []; foreach (static::$globals as $key => $value) { - $valueString = ''; $type = gettype($value); if (is_callable($value)) { diff --git a/tests/src/GlobalsTest.php b/tests/src/GlobalsTest.php index 659c3cf..6327e2c 100644 --- a/tests/src/GlobalsTest.php +++ b/tests/src/GlobalsTest.php @@ -214,6 +214,29 @@ public function testGetRandInteger() ); } + /** + * @covers Phramework\Testphase\Globals::get + * + */ + public function testGetTimestamp() + { + $return = Globals::get('timestamp'); + + $this->assertInternalType( + 'callable', + $return, + 'Expect to return a function' + ); + + $return = Globals::get('timestamp()'); + + $this->assertInternalType( + 'integer', + $return, + 'Expect to return integer' + ); + } + /** * @covers Phramework\Testphase\Globals::exists */