Skip to content

Commit

Permalink
Cover Date_Intervals generation with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
delawski committed Jul 18, 2024
1 parent 0addba8 commit 448e0e9
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 68 deletions.
147 changes: 79 additions & 68 deletions classes/class-date-interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,74 +51,7 @@ public function get_predefined_intervals() {

try {
$today = new \DateTimeImmutable( 'today', $timezone_object );
$date_intervals = array(
'today' => array(
'label' => esc_html__( 'Today', 'stream' ),
'start' => $today,
'end' => $today->modify( '+1 day -1 microsecond' ),
),
'yesterday' => array(
'label' => esc_html__( 'Yesterday', 'stream' ),
'start' => $today->modify( '-1 day' ),
'end' => $today->modify( '-1 microsecond' ),
),
'last-7-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ),
'start' => $today->modify( '-7 days' ),
'end' => $today,
),
'last-14-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ),
'start' => $today->modify( '-14 days' ),
'end' => $today,
),
'last-30-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ),
'start' => $today->modify( '-30 days' ),
'end' => $today,
),
'this-month' => array(
'label' => esc_html__( 'This Month', 'stream' ),
'start' => $today->modify( 'first day of this month' ),
'end' => $today->modify( 'last day of this month' )->modify( '+1 day -1 microsecond' ),
),
'last-month' => array(
'label' => esc_html__( 'Last Month', 'stream' ),
'start' => $today->modify( 'first day of last month' ),
'end' => $today->modify( 'last day of last month' )->modify( '+1 day -1 microsecond' ),
),
'last-3-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ),
'start' => $today->modify( '-3 months' ),
'end' => $today,
),
'last-6-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ),
'start' => $today->modify( '-6 months' ),
'end' => $today,
),
'last-12-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ),
'start' => $today->modify( '-12 months' ),
'end' => $today,
),
'this-year' => array(
'label' => esc_html__( 'This Year', 'stream' ),
'start' => $today->modify( 'first day of January' ),
'end' => $today->modify( 'last day of December' )->modify( '+1 day -1 microsecond' ),
),
'last-year' => array(
'label' => esc_html__( 'Last Year', 'stream' ),
'start' => $today->modify( 'first day of January' )->modify( '-1 year' ),
'end' => $today->modify( 'first day of January' )->modify( '-1 microsecond' ),
),
);
$date_intervals = $this->generate_date_intervals( $today );
} catch ( \Exception $e ) {
$date_intervals = array();
}
Expand All @@ -131,4 +64,82 @@ public function get_predefined_intervals() {
*/
return apply_filters( 'wp_stream_predefined_date_intervals', $date_intervals, $timezone );
}

/**
* Generate date intervals relative to date object provided.
*
* @param \DateTimeImmutable $date Date object.
*
* @return array[]
*/
public function generate_date_intervals( \DateTimeImmutable $date ) {
return array(
'today' => array(
'label' => esc_html__( 'Today', 'stream' ),
'start' => $date,
'end' => $date->modify( '+1 day -1 microsecond' ),
),
'yesterday' => array(
'label' => esc_html__( 'Yesterday', 'stream' ),
'start' => $date->modify( '-1 day' ),
'end' => $date->modify( '-1 microsecond' ),
),
'last-7-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ),
'start' => $date->modify( '-7 days' ),
'end' => $date,
),
'last-14-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ),
'start' => $date->modify( '-14 days' ),
'end' => $date,
),
'last-30-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ),
'start' => $date->modify( '-30 days' ),
'end' => $date,
),
'this-month' => array(
'label' => esc_html__( 'This Month', 'stream' ),
'start' => $date->modify( 'first day of this month' ),
'end' => $date->modify( 'last day of this month' )->modify( '+1 day -1 microsecond' ),
),
'last-month' => array(
'label' => esc_html__( 'Last Month', 'stream' ),
'start' => $date->modify( 'first day of last month' ),
'end' => $date->modify( 'last day of last month' )->modify( '+1 day -1 microsecond' ),
),
'last-3-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ),
'start' => $date->modify( '-3 months' ),
'end' => $date,
),
'last-6-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ),
'start' => $date->modify( '-6 months' ),
'end' => $date,
),
'last-12-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ),
'start' => $date->modify( '-12 months' ),
'end' => $date,
),
'this-year' => array(
'label' => esc_html__( 'This Year', 'stream' ),
'start' => $date->modify( 'first day of January' ),
'end' => $date->modify( 'last day of December' )->modify( '+1 day -1 microsecond' ),
),
'last-year' => array(
'label' => esc_html__( 'Last Year', 'stream' ),
'start' => $date->modify( 'first day of January' )->modify( '-1 year' ),
'end' => $date->modify( 'first day of January' )->modify( '-1 microsecond' ),
),
);
}
}
48 changes: 48 additions & 0 deletions tests/tests/test-class-date-interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,52 @@ public function test_get_predefined_intervals() {
$this->assertArrayHasKey( 'end', $interval );
}
}

/**
* Test generate_date_intervals
*
* @return void
* @throws \Exception
*/
public function test_generate_date_intervals() {
$timezone = new \DateTimeZone( 'UTC' );
$date = new \DateTimeImmutable( '2024-07-18', $timezone );
$intervals = $this->date_interval->generate_date_intervals( $date );

$this->assertSame( '2024-07-18T00:00:00+00:00', $intervals['today']['start']->format( DATE_ATOM ) );
$this->assertSame( '2024-07-18T23:59:59+00:00', $intervals['today']['end']->format( DATE_ATOM ) );

$this->assertSame( '2024-07-17T00:00:00+00:00', $intervals['yesterday']['start']->format( DATE_ATOM ) );
$this->assertSame( '2024-07-17T23:59:59+00:00', $intervals['yesterday']['end']->format( DATE_ATOM ) );

$this->assertSame( '2024-07-11T00:00:00+00:00', $intervals['last-7-days']['start']->format( DATE_ATOM ) );
$this->assertSame( '2024-07-18T00:00:00+00:00', $intervals['last-7-days']['end']->format( DATE_ATOM ) );

$this->assertSame( '2024-07-04T00:00:00+00:00', $intervals['last-14-days']['start']->format( DATE_ATOM ) );
$this->assertSame( '2024-07-18T00:00:00+00:00', $intervals['last-14-days']['end']->format( DATE_ATOM ) );

$this->assertSame( '2024-06-18T00:00:00+00:00', $intervals['last-30-days']['start']->format( DATE_ATOM ) );
$this->assertSame( '2024-07-18T00:00:00+00:00', $intervals['last-30-days']['end']->format( DATE_ATOM ) );

$this->assertSame( '2024-07-01T00:00:00+00:00', $intervals['this-month']['start']->format( DATE_ATOM ) );
$this->assertSame( '2024-07-31T23:59:59+00:00', $intervals['this-month']['end']->format( DATE_ATOM ) );

$this->assertSame( '2024-06-01T00:00:00+00:00', $intervals['last-month']['start']->format( DATE_ATOM ) );
$this->assertSame( '2024-06-30T23:59:59+00:00', $intervals['last-month']['end']->format( DATE_ATOM ) );

$this->assertSame( '2024-04-18T00:00:00+00:00', $intervals['last-3-months']['start']->format( DATE_ATOM ) );
$this->assertSame( '2024-07-18T00:00:00+00:00', $intervals['last-3-months']['end']->format( DATE_ATOM ) );

$this->assertSame( '2024-01-18T00:00:00+00:00', $intervals['last-6-months']['start']->format( DATE_ATOM ) );
$this->assertSame( '2024-07-18T00:00:00+00:00', $intervals['last-6-months']['end']->format( DATE_ATOM ) );

$this->assertSame( '2023-07-18T00:00:00+00:00', $intervals['last-12-months']['start']->format( DATE_ATOM ) );
$this->assertSame( '2024-07-18T00:00:00+00:00', $intervals['last-12-months']['end']->format( DATE_ATOM ) );

$this->assertSame( '2024-01-01T00:00:00+00:00', $intervals['this-year']['start']->format( DATE_ATOM ) );
$this->assertSame( '2024-12-31T23:59:59+00:00', $intervals['this-year']['end']->format( DATE_ATOM ) );

$this->assertSame( '2023-01-01T00:00:00+00:00', $intervals['last-year']['start']->format( DATE_ATOM ) );
$this->assertSame( '2023-12-31T23:59:59+00:00', $intervals['last-year']['end']->format( DATE_ATOM ) );
}
}

1 comment on commit 448e0e9

@tharsheblows
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 praise
Thank you 😊

Please sign in to comment.