Skip to content

Commit

Permalink
Add 'hourly' as a default interval
Browse files Browse the repository at this point in the history
Fixes #18.
  • Loading branch information
stevegrunwell committed Apr 27, 2020
1 parent 24c3cc5 commit 5d11a75
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/OrderLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ public function get_interval_start() {
$start = $this->now;

switch ( $interval ) {
case 'hourly':
// Start at the top of the current hour.
$start = $start->setTime( (int) $start->format( 'h' ), 0, 0 );
break;

case 'daily':
// Start at midnight.
$start = $start->setTime( 0, 0, 0 );
break;

case 'weekly':
$start_of_week = (int) get_option( 'week_starts_on' );
$current_dow = (int) $start->format( 'w' );
Expand All @@ -175,16 +185,16 @@ public function get_interval_start() {
if ( 0 !== $diff ) {
$start = $start->sub( new \DateInterval( 'P' . $diff . 'D' ) );
}

$start = $start->setTime( 0, 0, 0 );
break;

case 'monthly':
$start = $start->setDate( (int) $start->format( 'Y' ), (int) $start->format( 'm' ), 1 );
$start = $start->setDate( (int) $start->format( 'Y' ), (int) $start->format( 'm' ), 1 )
->setTime( 0, 0, 0 );
break;
}

// Start everything at midnight.
$start = $start->setTime( 0, 0, 0 );

/**
* Filter the DateTime object representing the start of the current interval.
*
Expand All @@ -205,6 +215,10 @@ public function get_next_interval_start() {
$start = clone $current;

switch ( $interval ) {
case 'hourly':
$start = $start->add( new \DateInterval( 'PT1H' ) );
break;

case 'daily':
$start = $start->add( new \DateInterval( 'P1D' ) );
break;
Expand Down
1 change: 1 addition & 0 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ protected function get_intervals() {
global $wp_locale;

$intervals = [
'hourly' => _x( 'Hourly (resets at the top of every hour)', 'order threshold interval', 'limit orders' ),
'daily' => _x( 'Daily (resets every day)', 'order threshold interval', 'limit-orders' ),
'weekly' => sprintf(
/* Translators: %1$s is the first day of the week, based on site configuration. */
Expand Down
60 changes: 60 additions & 0 deletions tests/OrderLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,26 @@ public function get_remaining_orders_should_return_zero_if_limits_are_met_or_exc
$this->assertSame( 0, ( new OrderLimiter() )->get_remaining_orders() );
}

/**
* @test
* @group Intervals
* @ticket https://github.com/nexcess/limit-orders/issues/18
*/
public function get_interval_start_for_hourly() {
update_option( OrderLimiter::OPTION_KEY, [
'interval' => 'hourly',
] );

$now = new \DateTimeImmutable( '2020-04-27 12:05:00', wp_timezone() );
$start = new \DateTimeImmutable( '2020-04-27 12:00:00', wp_timezone() );

$this->assertSame(
$start->format( 'r' ),
( new OrderLimiter( $now ) )->get_interval_start()->format( 'r' ),
'Hourly intervals should start at the top of the hour.'
);
}

/**
* @test
* @group Intervals
Expand Down Expand Up @@ -325,6 +345,26 @@ public function get_interval_start_should_be_idempotent() {
}
}

/**
* @test
* @group Intervals
* @ticket https://github.com/nexcess/limit-orders/issues/18
*/
public function get_next_interval_start_for_hourly() {
update_option( OrderLimiter::OPTION_KEY, [
'interval' => 'hourly',
] );

$now = new \DateTimeImmutable( '2020-04-27 12:05:00', wp_timezone() );
$next = new \DateTimeImmutable( '2020-04-27 13:00:00', wp_timezone() );

$this->assertSame(
$next->format( 'r' ),
( new OrderLimiter( $now ) )->get_next_interval_start()->format( 'r' ),
'The next hourly interval should begin at the top of the next hour.'
);
}

/**
* @test
* @group Intervals
Expand Down Expand Up @@ -386,6 +426,26 @@ public function get_next_interval_start_for_monthly() {
);
}

/**
* @test
* @group Intervals
* @ticket https://github.com/nexcess/limit-orders/issues/18
*/
public function get_seconds_until_next_interval_for_hourly() {
update_option( OrderLimiter::OPTION_KEY, [
'interval' => 'hourly',
] );

$now = new \DateTimeImmutable( '2020-04-27 12:05:00', wp_timezone() );
$next = new \DateTimeImmutable( '2020-04-27 13:00:00', wp_timezone() );

$this->assertSame(
$next->getTimestamp() - $now->getTimestamp(),
( new OrderLimiter( $now ) )->get_seconds_until_next_interval(),
'It should return the number of seconds until the next hour begins.'
);
}

/**
* @test
* @group Intervals
Expand Down
17 changes: 17 additions & 0 deletions tests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ public function the_options_should_be_added_to_their_own_page() {
$this->assertSame( 'limit-orders-general', $settings[0]['id'] );
}

/**
* @test
* @group Intervals
* @ticket https://github.com/nexcess/limit-orders/issues/18
*/
public function it_should_include_default_intervals() {
$method = new \ReflectionMethod( Settings::class, 'get_intervals' );
$method->setAccessible( true );

$intervals = $method->invoke( new Settings( new OrderLimiter() ) );

$this->assertArrayHasKey( 'daily', $intervals );
$this->assertArrayHasKey( 'weekly', $intervals );
$this->assertArrayHasKey( 'monthly', $intervals );
$this->assertArrayHasKey( 'hourly', $intervals, 'Hourly was added in https://github.com/nexcess/limit-orders/issues/18' );
}

/**
* @test
* @group Intervals
Expand Down

0 comments on commit 5d11a75

Please sign in to comment.