Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out testing methods into a separate trait.
- Loading branch information
Showing
2 changed files
with
75 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |