-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'trunk' into add/create-sensei-pages-on-activation
- Loading branch information
Showing
11 changed files
with
239 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: development | ||
|
||
Introduce Clock interface and corresponding public property for Sensei object. |
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
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,25 @@ | ||
<?php | ||
/** | ||
* File containing the Clock_Interface. | ||
* | ||
* @package sensei | ||
*/ | ||
|
||
namespace Sensei\Clock; | ||
|
||
use DateTimeZone; | ||
|
||
/** | ||
* Interface Clock_Interface | ||
* | ||
* @since $$next-version$$ | ||
*/ | ||
interface Clock_Interface { | ||
/** | ||
* Get the current time. | ||
* | ||
* @param DateTimeZone|null $timezone The timezone to use. Uses the default timezone if not provided. | ||
* @return \DateTimeImmutable | ||
*/ | ||
public function now( DateTimeZone $timezone = null ); | ||
} |
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,47 @@ | ||
<?php | ||
/** | ||
* File containing the Clock class. | ||
* | ||
* @package sensei | ||
*/ | ||
|
||
namespace Sensei\Clock; | ||
|
||
use DateTimeZone; | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
/** | ||
* Class Clock | ||
* | ||
* @since $$next-version$$ | ||
*/ | ||
class Clock implements Clock_Interface { | ||
/** | ||
* The timezone to use. | ||
* | ||
* @var DateTimeZone | ||
*/ | ||
private DateTimeZone $timezone; | ||
|
||
/** | ||
* Clock constructor. | ||
* | ||
* @param DateTimeZone $timezone The timezone to use. | ||
*/ | ||
public function __construct( DateTimeZone $timezone ) { | ||
$this->timezone = $timezone; | ||
} | ||
|
||
/** | ||
* Get the current time. | ||
* | ||
* @param DateTimeZone|null $timezone The timezone to use. | ||
* @return \DateTimeImmutable | ||
*/ | ||
public function now( DateTimeZone $timezone = null ) { | ||
return new \DateTimeImmutable( 'now', $timezone ?? $this->timezone ); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
/** | ||
* Stub for Clock_Interface. | ||
* | ||
* @package sensei-tests | ||
*/ | ||
|
||
use Sensei\Clock\Clock_Interface; | ||
|
||
/** | ||
* Class Sensei_Clock_Stub. | ||
*/ | ||
class Sensei_Clock_Stub implements Clock_Interface { | ||
/** | ||
* Get the current time. This is a stub that always returns the beginning of the Unix epoch. | ||
* | ||
* @return \DateTimeImmutable | ||
*/ | ||
public function now( \DateTimeZone $timezone = null ) { | ||
return ( new \DateTimeImmutable( '@0' ) )->setTimezone( $timezone ?? new \DateTimeZone( 'UTC' ) ); | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
/** | ||
* File with trait Sensei_Clock_Helpers. | ||
* | ||
* @package sensei-tests | ||
*/ | ||
|
||
// phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- Using PHPUnit conventions. | ||
|
||
use Sensei\Clock\Clock_Interface; | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
/** | ||
* Helpers related to the clock. | ||
* | ||
* @since $$next-version$$ | ||
*/ | ||
trait Sensei_Clock_Helpers { | ||
|
||
/** | ||
* Set the clock to a specific time. | ||
* | ||
* @param int|int[] $timestamp The timestamp or an array of timestamp clock will return on consecutive calls. | ||
* @param string $timezone The timezone to use. UTC by default. | ||
*/ | ||
private function set_clock_to( $timestamp, $timezone = 'UTC' ) { | ||
$return_values = array(); | ||
|
||
$timestamp = is_array( $timestamp ) ? $timestamp : array( $timestamp ); | ||
$timestamp = array_map( 'intval', $timestamp ); | ||
|
||
foreach ( $timestamp as $ts ) { | ||
$return_values[] = new \DateTimeImmutable( "@$ts", new \DateTimeZone( $timezone ) ); | ||
} | ||
|
||
$clock = $this->createMock( Clock_Interface::class ); | ||
$clock->method( 'now' ) | ||
->willReturnOnConsecutiveCalls( | ||
...$return_values | ||
); | ||
|
||
Sensei()->clock = $clock; | ||
} | ||
|
||
/** | ||
* Reset the clock to the default. | ||
*/ | ||
private function reset_clock() { | ||
Sensei()->clock = new Sensei_Clock_Stub(); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace SenseiTest\Clock; | ||
|
||
use Sensei\Clock\Clock; | ||
|
||
/** | ||
* Test the Clock class. | ||
* | ||
* @covers Sensei\Clock\Clock | ||
*/ | ||
class Clock_Test extends \WP_UnitTestCase { | ||
public function testNow_Always_ReturnsImmutableDateTime() { | ||
// Arrange. | ||
$clock = new Clock( new \DateTimeZone( 'UTC' ) ); | ||
|
||
// Act. | ||
$now = $clock->now(); | ||
|
||
// Assert. | ||
$this->assertInstanceOf( \DateTimeImmutable::class, $now ); | ||
} | ||
|
||
public function testNow_DateTimeZoneGiven_ReturnsDateTimeInGivenTimeZone() { | ||
// Arrange. | ||
$clock = new Clock( new \DateTimeZone( 'America/New_York' ) ); | ||
|
||
// Act. | ||
$now = $clock->now(); | ||
|
||
// Assert. | ||
$this->assertEquals( 'America/New_York', $now->getTimezone()->getName() ); | ||
} | ||
} |
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