Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #33 from RonRademaker/week-operation-bug
Browse files Browse the repository at this point in the history
Week operation bug
  • Loading branch information
RonRademaker committed Dec 9, 2015
2 parents d662dee + d217f6d commit 7c9bcc9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
24 changes: 9 additions & 15 deletions src/Operation/WeekOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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');
}
}
}
39 changes: 31 additions & 8 deletions tests/Operation/WeekOperationTest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php
namespace ConnectHolland\MongoAggregations\Operation\Test;

use ConnectHolland\MongoAggregations\Aggregation\Projection;
use ConnectHolland\MongoAggregations\Operation\InvalidAggregationOperationArgument;
use ConnectHolland\MongoAggregations\Operation\WeekOperation;
use PHPUnit_Framework_TestCase;
use ConnectHolland\MongoAggregations\Test\AbstractTestCase;
use MongoDate;

/**
* Tests the week operation
*
* @author Ron Rademaker
*/
class WeekOperationTest extends PHPUnit_Framework_TestCase
class WeekOperationTest extends AbstractTestCase
{
/**
* Tests if the week operation gives the correct mongo query
Expand All @@ -19,9 +21,8 @@ public function testWeekOperation()
{
$week = new WeekOperation();
$week->setDatefield('date');
$week->setWeekField('week');

$this->assertEquals(['week' => ['$week' => '$date']], $week->getOperation());
$this->assertEquals(['$week' => '$date'], $week->getOperation());
}

/**
Expand All @@ -35,12 +36,34 @@ public function testIncorrectDateFieldThrowsException()
}

/**
* Tests if passing in incorrect param throws an exception
* Test projecting week numbers
*/
public function testIncorrectWeekFieldThrowsException()
public function testProjectWeekNumber()
{
$this->setExpectedException(InvalidAggregationOperationArgument::class);
$testData = [
['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) {
$this->collection->save($test);
}

$week = new WeekOperation();
$week->setWeekField(['week']);
$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']);
}

}
}

0 comments on commit 7c9bcc9

Please sign in to comment.