Skip to content

Commit

Permalink
Split out testing methods into a separate trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 4, 2015
1 parent c013fba commit ec57851
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 51 deletions.
52 changes: 1 addition & 51 deletions src/DateTimeTrait.php
Expand Up @@ -29,6 +29,7 @@ trait DateTimeTrait
use FormattingTrait;
use ModifierTrait;
use TimezoneTrait;
use TestingAidTrait;

/**
* Terms used to detect if a time passed is a relative date for testing purposes
Expand All @@ -48,13 +49,6 @@ trait DateTimeTrait
'ago',
];

/**
* A test ChronosInterface instance to be returned when now instances are created
*
* @var ChronosInterface
*/
protected static $testNow;

/**
* Get a part of the ChronosInterface object
*
Expand Down Expand Up @@ -134,50 +128,6 @@ public function __isset($name)
return true;
}

/**
* Set a ChronosInterface instance (real or mock) to be returned when a "now"
* instance is created. The provided instance will be returned
* specifically under the following conditions:
* - A call to the static now() method, ex. ChronosInterface::now()
* - When a null (or blank string) is passed to the constructor or parse(), ex. new Chronos(null)
* - When the string "now" is passed to the constructor or parse(), ex. new Chrono('now')
*
* Note the timezone parameter was left out of the examples above and
* has no affect as the mock value will be returned regardless of its value.
*
* To clear the test instance call this method using the default
* parameter of null.
*
* @param ChronosInterface $testNow The instance to use for all future instances.
* @return void
*/
public static function setTestNow(ChronosInterface $testNow = null)
{
static::$testNow = $testNow;
}

/**
* Get the ChronosInterface instance (real or mock) to be returned when a "now"
* instance is created.
*
* @return static the current instance used for testing
*/
public static function getTestNow()
{
return static::$testNow;
}

/**
* Determine if there is a valid test instance set. A valid test instance
* is anything that is not null.
*
* @return bool true if there is a test instance, otherwise false
*/
public static function hasTestNow()
{
return static::getTestNow() !== null;
}

/**
* Determine if there is a relative keyword in the time string, this is to
* create dates relative to now for test instances. e.g.: next tuesday
Expand Down
74 changes: 74 additions & 0 deletions src/TestingAidTrait.php
@@ -0,0 +1,74 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice. Provides various operator methods for datetime
* objects.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;

use Cake\Chronos\ChronosInterface;

/**
* Provides methods for setting a 'test' now. This lets you
* retreive pre-determined times with now().
*/
trait TestingAidTrait
{
/**
* A test ChronosInterface instance to be returned when now instances are created
*
* @var ChronosInterface
*/
protected static $testNow;

/**
* Set a ChronosInterface instance (real or mock) to be returned when a "now"
* instance is created. The provided instance will be returned
* specifically under the following conditions:
* - A call to the static now() method, ex. ChronosInterface::now()
* - When a null (or blank string) is passed to the constructor or parse(), ex. new Chronos(null)
* - When the string "now" is passed to the constructor or parse(), ex. new Chrono('now')
*
* Note the timezone parameter was left out of the examples above and
* has no affect as the mock value will be returned regardless of its value.
*
* To clear the test instance call this method using the default
* parameter of null.
*
* @param ChronosInterface $testNow The instance to use for all future instances.
* @return void
*/
public static function setTestNow(ChronosInterface $testNow = null)
{
static::$testNow = $testNow;
}

/**
* Get the ChronosInterface instance (real or mock) to be returned when a "now"
* instance is created.
*
* @return static the current instance used for testing
*/
public static function getTestNow()
{
return static::$testNow;
}

/**
* Determine if there is a valid test instance set. A valid test instance
* is anything that is not null.
*
* @return bool true if there is a test instance, otherwise false
*/
public static function hasTestNow()
{
return static::getTestNow() !== null;
}
}

0 comments on commit ec57851

Please sign in to comment.