Skip to content

Instantly share code, notes, and snippets.

@lucaswerkmeister
Created July 13, 2018 07:47
Show Gist options
  • Save lucaswerkmeister/14a0b55d400345ac3374ee6c2fe14a5d to your computer and use it in GitHub Desktop.
Save lucaswerkmeister/14a0b55d400345ac3374ee6c2fe14a5d to your computer and use it in GitHub Desktop.
From e5692dfc74d09edd3145ee1ca0d08f8436ea4369 Mon Sep 17 00:00:00 2001
From: Lucas Werkmeister <lucas.werkmeister@wikimedia.de>
Date: Fri, 13 Jul 2018 09:46:02 +0200
Subject: [PATCH] Improve test data providers
---
tests/DataValues/TimeValueCalculatorTest.php | 312 +++++++++----------
1 file changed, 142 insertions(+), 170 deletions(-)
diff --git a/tests/DataValues/TimeValueCalculatorTest.php b/tests/DataValues/TimeValueCalculatorTest.php
index e98e57f..c151853 100644
--- a/tests/DataValues/TimeValueCalculatorTest.php
+++ b/tests/DataValues/TimeValueCalculatorTest.php
@@ -284,7 +284,7 @@ public function provideTimeValuesHighVsLowPrecisions() {
$precision,
TimeValue::CALENDAR_GREGORIAN
);
- yield [ [ $oldTimeValue, $timeValue ] ];
+ yield [ $oldTimeValue, $timeValue ];
}
$oldTimestamp = $timestamp;
$oldPrecision = $precision;
@@ -298,7 +298,7 @@ public function provideTimeValuesHighVsLowPrecisions() {
public function provideTimestampsAndPrecisions() {
foreach ( $this->provideTimestamps() as $timestamp ) {
foreach ( $this->provideAllPrecisions() as $precision ) {
- yield [ [ $timestamp, $precision ] ];
+ yield [ $timestamp, $precision ];
}
}
}
@@ -313,8 +313,8 @@ public function provideTimestampPairs() {
$timestamp = $sign . $timestamp;
if ( $oldTimestamp !== null ) {
yield $sign === '+' ?
- [ [ $oldTimestamp, $timestamp ] ] :
- [ [ $timestamp, $oldTimestamp ] ];
+ [ $oldTimestamp, $timestamp ] :
+ [ $timestamp, $oldTimestamp ];
}
$oldTimestamp = $timestamp;
}
@@ -322,13 +322,13 @@ public function provideTimestampPairs() {
}
/**
- * @return \Generator of arrays of:
+ * @return array of:
* - a TimeValue,
* - its lower Unix timestamp, and
* - its Unix higher timestamp.
*/
public function provideTimeValuesAndLowerAndHigherTimestamps() {
- $timeValuesAndLowerAndHigherTimestamps = [
+ return [
[
new TimeValue( // BCE calculations can differ one year from external ones
'-0754-11-27T10:49:31Z',
@@ -400,7 +400,6 @@ public function provideTimeValuesAndLowerAndHigherTimestamps() {
5999999999999999
],
];
- yield $timeValuesAndLowerAndHigherTimestamps;
}
/**
@@ -408,13 +407,14 @@ public function provideTimeValuesAndLowerAndHigherTimestamps() {
*
* @dataProvider provideTimeValuesAndLowerAndHigherTimestamps
*/
- public function testSpecificLowerTimestamps() {
- $timeValuesAndLowerTimestamps = func_get_args();
+ public function testSpecificLowerTimestamps(
+ $timeValue,
+ $expectedUnixLowerTimestamp,
+ $expectedUnixHigherTimestamp
+ ) {
$calculator = new TimeValueCalculator();
- foreach ( $timeValuesAndLowerTimestamps as $timeValueAndLowerTimestamp ) {
- $unixLowerTimestamp = $calculator->getLowerTimestamp( $timeValueAndLowerTimestamp[0] );
- $this->assertEquals( $timeValueAndLowerTimestamp[1], $unixLowerTimestamp );
- }
+ $unixLowerTimestamp = $calculator->getLowerTimestamp( $timeValue );
+ $this->assertEquals( $expectedUnixLowerTimestamp, $unixLowerTimestamp );
}
/**
@@ -422,13 +422,14 @@ public function testSpecificLowerTimestamps() {
*
* @dataProvider provideTimeValuesAndLowerAndHigherTimestamps
*/
- public function testSpecificHigherTimestamps() {
- $timeValuesAndHigherTimestamps = func_get_args();
+ public function testSpecificHigherTimestamps(
+ $timeValue,
+ $expectedUnixLowerTimestamp,
+ $expectedUnixHigherTimestamp
+ ) {
$calculator = new TimeValueCalculator();
- foreach ( $timeValuesAndHigherTimestamps as $timeValueAndHigherTimestamp ) {
- $unixHigherTimestamp = $calculator->getHigherTimestamp( $timeValueAndHigherTimestamp[0] );
- $this->assertEquals( $timeValueAndHigherTimestamp[2], $unixHigherTimestamp );
- }
+ $unixHigherTimestamp = $calculator->getHigherTimestamp( $timeValue );
+ $this->assertEquals( $expectedUnixHigherTimestamp, $unixHigherTimestamp );
}
/**
@@ -437,28 +438,23 @@ public function testSpecificHigherTimestamps() {
*
* @dataProvider provideTimestampPairs
*/
- public function testOrderingLowerTimestamps() {
+ public function testOrderingLowerTimestamps( $earlierTimestamp, $laterTimestamp ) {
$calculator = new TimeValueCalculator();
- $timestampPairs = func_get_args();
- foreach ( $timestampPairs as $timestampPair ) {
- $earlierTimestamp = $timestampPair[0];
- $laterTimestamp = $timestampPair[1];
- $earlierTimeValue = new TimeValue(
- $earlierTimestamp,
- 0, 0, 0,
- TimeValue::PRECISION_DAY,
- TimeValue::CALENDAR_GREGORIAN
- );
- $laterTimeValue = new TimeValue(
- $laterTimestamp,
- 0, 0, 0,
- TimeValue::PRECISION_DAY,
- TimeValue::CALENDAR_GREGORIAN
- );
- $earlierUnixLowerTimestamp = $calculator->getLowerTimestamp( $earlierTimeValue );
- $laterUnixLowerTimestamp = $calculator->getLowerTimestamp( $laterTimeValue );
- $this->assertGreaterThanOrEqual( $earlierUnixLowerTimestamp, $laterUnixLowerTimestamp );
- }
+ $earlierTimeValue = new TimeValue(
+ $earlierTimestamp,
+ 0, 0, 0,
+ TimeValue::PRECISION_DAY,
+ TimeValue::CALENDAR_GREGORIAN
+ );
+ $laterTimeValue = new TimeValue(
+ $laterTimestamp,
+ 0, 0, 0,
+ TimeValue::PRECISION_DAY,
+ TimeValue::CALENDAR_GREGORIAN
+ );
+ $earlierUnixLowerTimestamp = $calculator->getLowerTimestamp( $earlierTimeValue );
+ $laterUnixLowerTimestamp = $calculator->getLowerTimestamp( $laterTimeValue );
+ $this->assertGreaterThanOrEqual( $earlierUnixLowerTimestamp, $laterUnixLowerTimestamp );
}
/**
@@ -467,28 +463,23 @@ public function testOrderingLowerTimestamps() {
*
* @dataProvider provideTimestampPairs
*/
- public function testOrderingHigherTimestamps() {
+ public function testOrderingHigherTimestamps( $earlierTimestamp, $laterTimestamp ) {
$calculator = new TimeValueCalculator();
- $timestampPairs = func_get_args();
- foreach ( $timestampPairs as $timestampPair ) {
- $earlierTimestamp = $timestampPair[0];
- $laterTimestamp = $timestampPair[1];
- $earlierTimeValue = new TimeValue(
- $earlierTimestamp,
- 0, 0, 0,
- TimeValue::PRECISION_DAY,
- TimeValue::CALENDAR_GREGORIAN
- );
- $laterTimeValue = new TimeValue(
- $laterTimestamp,
- 0, 0, 0,
- TimeValue::PRECISION_DAY,
- TimeValue::CALENDAR_GREGORIAN
- );
- $earlierUnixHigherTimestamp = $calculator->getHigherTimestamp( $earlierTimeValue );
- $laterUnixHigherTimestamp = $calculator->getHigherTimestamp( $laterTimeValue );
- $this->assertGreaterThanOrEqual( $earlierUnixHigherTimestamp, $laterUnixHigherTimestamp );
- }
+ $earlierTimeValue = new TimeValue(
+ $earlierTimestamp,
+ 0, 0, 0,
+ TimeValue::PRECISION_DAY,
+ TimeValue::CALENDAR_GREGORIAN
+ );
+ $laterTimeValue = new TimeValue(
+ $laterTimestamp,
+ 0, 0, 0,
+ TimeValue::PRECISION_DAY,
+ TimeValue::CALENDAR_GREGORIAN
+ );
+ $earlierUnixHigherTimestamp = $calculator->getHigherTimestamp( $earlierTimeValue );
+ $laterUnixHigherTimestamp = $calculator->getHigherTimestamp( $laterTimeValue );
+ $this->assertGreaterThanOrEqual( $earlierUnixHigherTimestamp, $laterUnixHigherTimestamp );
}
/**
@@ -496,22 +487,17 @@ public function testOrderingHigherTimestamps() {
*
* @dataProvider provideTimestampsAndPrecisions
*/
- public function testLowerTimestampsVsTimestamps() {
+ public function testLowerTimestampsVsTimestamps( $timestamp, $precision ) {
$calculator = new TimeValueCalculator();
- $timestampsAndPrecisions = func_get_args();
- foreach ( $timestampsAndPrecisions as $timestampAndPrecision ) {
- $timestamp = $timestampAndPrecision[0];
- $precision = $timestampAndPrecision[1];
- $timeValue = new TimeValue(
- $timestamp,
- 0, 0, 0,
- $precision,
- TimeValue::CALENDAR_GREGORIAN
- );
- $unixTimestampAsIs = $calculator->getTimestamp( $timeValue );
- $unixLowerTimestamp = $calculator->getLowerTimestamp( $timeValue );
- $this->assertGreaterThanOrEqual( $unixLowerTimestamp, $unixTimestampAsIs );
- }
+ $timeValue = new TimeValue(
+ $timestamp,
+ 0, 0, 0,
+ $precision,
+ TimeValue::CALENDAR_GREGORIAN
+ );
+ $unixTimestampAsIs = $calculator->getTimestamp( $timeValue );
+ $unixLowerTimestamp = $calculator->getLowerTimestamp( $timeValue );
+ $this->assertGreaterThanOrEqual( $unixLowerTimestamp, $unixTimestampAsIs );
}
/**
@@ -519,22 +505,17 @@ public function testLowerTimestampsVsTimestamps() {
*
* @dataProvider provideTimestampsAndPrecisions
*/
- public function testHigherTimestampsVsTimestamps() {
+ public function testHigherTimestampsVsTimestamps( $timestamp, $precision ) {
$calculator = new TimeValueCalculator();
- $timestampsAndPrecisions = func_get_args();
- foreach ( $timestampsAndPrecisions as $timestampAndPrecision ) {
- $timestamp = $timestampAndPrecision[0];
- $precision = $timestampAndPrecision[1];
- $timeValue = new TimeValue(
- $timestamp,
- 0, 0, 0,
- $precision,
- TimeValue::CALENDAR_GREGORIAN
- );
- $unixTimestampAsIs = $calculator->getTimestamp( $timeValue );
- $unixHigherTimestamp = $calculator->getHigherTimestamp( $timeValue );
- $this->assertGreaterThanOrEqual( $unixTimestampAsIs, $unixHigherTimestamp );
- }
+ $timeValue = new TimeValue(
+ $timestamp,
+ 0, 0, 0,
+ $precision,
+ TimeValue::CALENDAR_GREGORIAN
+ );
+ $unixTimestampAsIs = $calculator->getTimestamp( $timeValue );
+ $unixHigherTimestamp = $calculator->getHigherTimestamp( $timeValue );
+ $this->assertGreaterThanOrEqual( $unixTimestampAsIs, $unixHigherTimestamp );
}
/**
@@ -543,40 +524,35 @@ public function testHigherTimestampsVsTimestamps() {
*
* @dataProvider provideTimestampsAndPrecisions
*/
- public function testGetLowerTimestampBefore() {
+ public function testGetLowerTimestampBefore( $timestamp, $precision ) {
$calculator = new TimeValueCalculator();
- $timestampsAndPrecisions = func_get_args();
- foreach ( $timestampsAndPrecisions as $timestampAndPrecision ) {
- $timestamp = $timestampAndPrecision[0];
- $precision = $timestampAndPrecision[1];
- $timeValue = new TimeValue(
- $timestamp,
- 0, 0, 1,
- $precision,
- TimeValue::CALENDAR_GREGORIAN
- );
- $unixLowerTimestamp = $calculator->getLowerTimestamp( $timeValue );
- $timeValueBefore1 = new TimeValue(
- $timestamp,
- 0, 1, 1,
- $precision,
- TimeValue::CALENDAR_GREGORIAN
- );
- $unixLowerTimestampBefore1 = $calculator->getLowerTimestamp( $timeValueBefore1 );
- $timeValueBefore2 = new TimeValue(
- $timestamp,
- 0, 2, 0,
- $precision,
- TimeValue::CALENDAR_GREGORIAN
- );
- $unixLowerTimestampBefore2 = $calculator->getLowerTimestamp( $timeValueBefore2 );
- if ( $unixLowerTimestamp > -10000000000000 && $unixLowerTimestamp < 10000000000000 ) {
- $this->assertGreaterThan( $unixLowerTimestampBefore1, $unixLowerTimestamp );
- $this->assertGreaterThan( $unixLowerTimestampBefore2, $unixLowerTimestampBefore1 );
- } else {
- $this->assertGreaterThanOrEqual( $unixLowerTimestampBefore1, $unixLowerTimestamp );
- $this->assertGreaterThanOrEqual( $unixLowerTimestampBefore2, $unixLowerTimestampBefore1 );
- }
+ $timeValue = new TimeValue(
+ $timestamp,
+ 0, 0, 1,
+ $precision,
+ TimeValue::CALENDAR_GREGORIAN
+ );
+ $unixLowerTimestamp = $calculator->getLowerTimestamp( $timeValue );
+ $timeValueBefore1 = new TimeValue(
+ $timestamp,
+ 0, 1, 1,
+ $precision,
+ TimeValue::CALENDAR_GREGORIAN
+ );
+ $unixLowerTimestampBefore1 = $calculator->getLowerTimestamp( $timeValueBefore1 );
+ $timeValueBefore2 = new TimeValue(
+ $timestamp,
+ 0, 2, 0,
+ $precision,
+ TimeValue::CALENDAR_GREGORIAN
+ );
+ $unixLowerTimestampBefore2 = $calculator->getLowerTimestamp( $timeValueBefore2 );
+ if ( $unixLowerTimestamp > -10000000000000 && $unixLowerTimestamp < 10000000000000 ) {
+ $this->assertGreaterThan( $unixLowerTimestampBefore1, $unixLowerTimestamp );
+ $this->assertGreaterThan( $unixLowerTimestampBefore2, $unixLowerTimestampBefore1 );
+ } else {
+ $this->assertGreaterThanOrEqual( $unixLowerTimestampBefore1, $unixLowerTimestamp );
+ $this->assertGreaterThanOrEqual( $unixLowerTimestampBefore2, $unixLowerTimestampBefore1 );
}
}
@@ -586,40 +562,35 @@ public function testGetLowerTimestampBefore() {
*
* @dataProvider provideTimestampsAndPrecisions
*/
- public function testGetHigherTimestampAfter() {
+ public function testGetHigherTimestampAfter( $timestamp, $precision ) {
$calculator = new TimeValueCalculator();
- $timestampsAndPrecisions = func_get_args();
- foreach ( $timestampsAndPrecisions as $timestampAndPrecision ) {
- $timestamp = $timestampAndPrecision[0];
- $precision = $timestampAndPrecision[1];
- $timeValue = new TimeValue(
- $timestamp,
- 0, 1, 0,
- $precision,
- TimeValue::CALENDAR_GREGORIAN
- );
- $unixHigherTimestamp = $calculator->getHigherTimestamp( $timeValue );
- $timeValueAfter1 = new TimeValue(
- $timestamp,
- 0, 1, 1,
- $precision,
- TimeValue::CALENDAR_GREGORIAN
- );
- $unixHigherTimestampAfter1 = $calculator->getHigherTimestamp( $timeValueAfter1 );
- $timeValueAfter2 = new TimeValue(
- $timestamp,
- 0, 0, 2,
- $precision,
- TimeValue::CALENDAR_GREGORIAN
- );
- $unixHigherTimestampAfter2 = $calculator->getHigherTimestamp( $timeValueAfter2 );
- if ( $unixHigherTimestamp > -10000000000000 && $unixHigherTimestamp < 10000000000000 ) {
- $this->assertGreaterThan( $unixHigherTimestamp, $unixHigherTimestampAfter1 );
- $this->assertGreaterThan( $unixHigherTimestampAfter1, $unixHigherTimestampAfter2 );
- } else {
- $this->assertGreaterThanOrEqual( $unixHigherTimestamp, $unixHigherTimestampAfter1 );
- $this->assertGreaterThanOrEqual( $unixHigherTimestampAfter1, $unixHigherTimestampAfter2 );
- }
+ $timeValue = new TimeValue(
+ $timestamp,
+ 0, 1, 0,
+ $precision,
+ TimeValue::CALENDAR_GREGORIAN
+ );
+ $unixHigherTimestamp = $calculator->getHigherTimestamp( $timeValue );
+ $timeValueAfter1 = new TimeValue(
+ $timestamp,
+ 0, 1, 1,
+ $precision,
+ TimeValue::CALENDAR_GREGORIAN
+ );
+ $unixHigherTimestampAfter1 = $calculator->getHigherTimestamp( $timeValueAfter1 );
+ $timeValueAfter2 = new TimeValue(
+ $timestamp,
+ 0, 0, 2,
+ $precision,
+ TimeValue::CALENDAR_GREGORIAN
+ );
+ $unixHigherTimestampAfter2 = $calculator->getHigherTimestamp( $timeValueAfter2 );
+ if ( $unixHigherTimestamp > -10000000000000 && $unixHigherTimestamp < 10000000000000 ) {
+ $this->assertGreaterThan( $unixHigherTimestamp, $unixHigherTimestampAfter1 );
+ $this->assertGreaterThan( $unixHigherTimestampAfter1, $unixHigherTimestampAfter2 );
+ } else {
+ $this->assertGreaterThanOrEqual( $unixHigherTimestamp, $unixHigherTimestampAfter1 );
+ $this->assertGreaterThanOrEqual( $unixHigherTimestampAfter1, $unixHigherTimestampAfter2 );
}
}
@@ -629,14 +600,14 @@ public function testGetHigherTimestampAfter() {
*
* @dataProvider provideTimeValuesHighVsLowPrecisions
*/
- public function testGetLowerTimestampsHighVsLowPrecisions() {
+ public function testGetLowerTimestampsHighVsLowPrecisions(
+ $timeValueHighPrecision,
+ $timeValueLowPrecision
+ ) {
$calculator = new TimeValueCalculator();
- $timeValuesHighVsLowPrecisions = func_get_args();
- foreach ( $timeValuesHighVsLowPrecisions as $timeValueHighVsLowPrecision ) {
- $timestampHighPrecision = $calculator->getLowerTimestamp( $timeValueHighVsLowPrecision[0] );
- $timestampLowPrecision = $calculator->getLowerTimestamp( $timeValueHighVsLowPrecision[1] );
- $this->assertGreaterThanOrEqual( $timestampLowPrecision, $timestampHighPrecision );
- }
+ $timestampHighPrecision = $calculator->getLowerTimestamp( $timeValueHighPrecision );
+ $timestampLowPrecision = $calculator->getLowerTimestamp( $timeValueLowPrecision );
+ $this->assertGreaterThanOrEqual( $timestampLowPrecision, $timestampHighPrecision );
}
/**
@@ -645,14 +616,15 @@ public function testGetLowerTimestampsHighVsLowPrecisions() {
*
* @dataProvider provideTimeValuesHighVsLowPrecisions
*/
- public function testGetHigherTimestampsHighVsLowPrecisions() {
+ public function testGetHigherTimestampsHighVsLowPrecisions(
+ $timeValueHighPrecision,
+ $timeValueLowPrecision
+ ) {
$calculator = new TimeValueCalculator();
$timeValuesHighVsLowPrecisions = func_get_args();
- foreach ( $timeValuesHighVsLowPrecisions as $timeValueHighVsLowPrecision ) {
- $timestampHighPrecision = $calculator->getHigherTimestamp( $timeValueHighVsLowPrecision[0] );
- $timestampLowPrecision = $calculator->getHigherTimestamp( $timeValueHighVsLowPrecision[1] );
- $this->assertGreaterThanOrEqual( $timestampHighPrecision, $timestampLowPrecision );
- }
+ $timestampHighPrecision = $calculator->getHigherTimestamp( $timeValueHighPrecision );
+ $timestampLowPrecision = $calculator->getHigherTimestamp( $timeValueLowPrecision );
+ $this->assertGreaterThanOrEqual( $timestampHighPrecision, $timestampLowPrecision );
}
}
--
2.17.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment