From 0cb6dfde1311c1921e5537660836888483b1f3f9 Mon Sep 17 00:00:00 2001 From: Ron Rademaker Date: Wed, 9 Dec 2015 09:26:45 +0100 Subject: [PATCH 1/4] Extended week operation test to reproduce bug --- tests/Operation/WeekOperationTest.php | 41 ++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/tests/Operation/WeekOperationTest.php b/tests/Operation/WeekOperationTest.php index 9d5cc9d..60a92a5 100644 --- a/tests/Operation/WeekOperationTest.php +++ b/tests/Operation/WeekOperationTest.php @@ -1,16 +1,18 @@ setDatefield('date'); - $week->setWeekField('week'); - $this->assertEquals(['week' => ['$week' => '$date']], $week->getOperation()); + $this->assertEquals(['$week' => '$date'], $week->getOperation()); } /** @@ -43,4 +44,36 @@ public function testIncorrectWeekFieldThrowsException() $week = new WeekOperation(); $week->setWeekField(['week']); } + + /** + * Test projecting week numbers + */ + public function testProjectWeekNumber() + { + $testData = [ + ['foo' => new MongoDate(strtotime('2015W10')), 'expected' => 10], + ['foo' => new MongoDate(strtotime('2015W20')), 'expected' => 20], + ['foo' => new MongoDate(strtotime('2015W25')), 'expected' => 25] + ]; + + foreach ($testData as $test) { + $this->collection->save($test); + } + + $week = new WeekOperation(); + $week->setDatefield('foo'); + + $projection = new Projection(); + $projection->includeField('expected'); + $projection->includeOperationField('weeknumber', $week); + + $result = $this->collection->aggregate([$projection->getStage()]); + + $this->assertEquals(3, count($result['result']));; + + foreach ($result['result'] as $record) { + $this->assertEquals($record['expected'], $record['weeknumber']); + } + + } } From 390fb4b35d3165732e71497e67130775553d38ab Mon Sep 17 00:00:00 2001 From: Ron Rademaker Date: Wed, 9 Dec 2015 09:27:37 +0100 Subject: [PATCH 2/4] Removed test on the week operation method which is buggy --- tests/Operation/WeekOperationTest.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/Operation/WeekOperationTest.php b/tests/Operation/WeekOperationTest.php index 60a92a5..e4cade4 100644 --- a/tests/Operation/WeekOperationTest.php +++ b/tests/Operation/WeekOperationTest.php @@ -35,16 +35,6 @@ public function testIncorrectDateFieldThrowsException() $week->setDatefield(['date']); } - /** - * Tests if passing in incorrect param throws an exception - */ - public function testIncorrectWeekFieldThrowsException() - { - $this->setExpectedException(InvalidAggregationOperationArgument::class); - $week = new WeekOperation(); - $week->setWeekField(['week']); - } - /** * Test projecting week numbers */ From b6626a244e01c03accf3097b58637b8641d29868 Mon Sep 17 00:00:00 2001 From: Ron Rademaker Date: Wed, 9 Dec 2015 09:55:58 +0100 Subject: [PATCH 3/4] Use %U weeknumbers in test, this complies with how mongo calculates week numbers --- tests/Operation/WeekOperationTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Operation/WeekOperationTest.php b/tests/Operation/WeekOperationTest.php index e4cade4..f4f83fa 100644 --- a/tests/Operation/WeekOperationTest.php +++ b/tests/Operation/WeekOperationTest.php @@ -41,9 +41,9 @@ public function testIncorrectDateFieldThrowsException() public function testProjectWeekNumber() { $testData = [ - ['foo' => new MongoDate(strtotime('2015W10')), 'expected' => 10], - ['foo' => new MongoDate(strtotime('2015W20')), 'expected' => 20], - ['foo' => new MongoDate(strtotime('2015W25')), 'expected' => 25] + ['foo' => new MongoDate(strtotime('2015W10')), 'expected' => strftime('%U', strtotime('2015W10'))], + ['foo' => new MongoDate(strtotime('2015W20')), 'expected' => strftime('%U', strtotime('2015W20'))], + ['foo' => new MongoDate(strtotime('2015W25')), 'expected' => strftime('%U', strtotime('2015W25'))] ]; foreach ($testData as $test) { From d217f6db78841bba31540422d5b96133bf67ddf8 Mon Sep 17 00:00:00 2001 From: Ron Rademaker Date: Wed, 9 Dec 2015 09:56:18 +0100 Subject: [PATCH 4/4] Fixed bugs in WeekOperation --- src/Operation/WeekOperation.php | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/Operation/WeekOperation.php b/src/Operation/WeekOperation.php index 45423a9..07222d8 100644 --- a/src/Operation/WeekOperation.php +++ b/src/Operation/WeekOperation.php @@ -8,6 +8,14 @@ */ class WeekOperation extends AbstractOperation implements ProjectOperationInterface { + /** + * Sets the operation + */ + public function __construct() + { + $this->setOperationType('$week'); + } + /** * Sets the date field to create week numbers from * @@ -19,23 +27,9 @@ public function setDatefield($field) if (strpos($field, '$') !== 0) { $field = '$' . $field; } - $this->setArguments(['$week' => $field]); + $this->setArguments($field); } else { throw new InvalidAggregationOperationArgument('WeekOperation only allows scalar arguments'); } } - - /** - * Sets the field to add the week number under - * - * @param string $field - */ - public function setWeekField($field) - { - if (is_scalar($field)) { - parent::setOperationType($field); - } else { - throw new InvalidAggregationOperationArgument('WeekOperation only allows scalar arguments'); - } - } }