From 4f5889cc5b89264937260328900cf27d4fe282a5 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 29 Jun 2015 09:14:39 +0200 Subject: [PATCH] New tests for navigation. --- app/Support/Navigation.php | 18 +- tests/support/NavigationTest.php | 287 +++++++++++++++++++++++++++++++ 2 files changed, 293 insertions(+), 12 deletions(-) create mode 100644 tests/support/NavigationTest.php diff --git a/app/Support/Navigation.php b/app/Support/Navigation.php index 8089b94b2e..2f3c15fe8c 100644 --- a/app/Support/Navigation.php +++ b/app/Support/Navigation.php @@ -83,7 +83,7 @@ class Navigation $subDay = ['week', 'weekly', '1W', 'month', 'monthly', '1M', '3M', 'quarter', 'quarterly', '6M', 'half-year', 'year', 'yearly']; if (!isset($functionMap[$repeatFreq])) { - throw new FireflyException('Cannot do endOfPeriod for $repeat_freq ' . $repeatFreq); + throw new FireflyException('Cannot do endOfPeriod for $repeat_freq "' . $repeatFreq.'"'); } $function = $functionMap[$repeatFreq]; if (isset($modifierMap[$repeatFreq])) { @@ -119,7 +119,6 @@ class Navigation 'year' => 'endOfYear', 'yearly' => 'endOfYear', ]; - $specials = ['mont', 'monthly']; $currentEnd = clone $theCurrentEnd; @@ -128,12 +127,7 @@ class Navigation $currentEnd->$function(); } - if (isset($specials[$repeatFreq])) { - $currentEnd->endOfYear(); - if ($theCurrentEnd->month <= 6) { - $currentEnd->subMonths(6); - } - } + if ($currentEnd > $maxDate) { return clone $maxDate; } @@ -210,7 +204,7 @@ class Navigation return $date; } - throw new FireflyException('Cannot do startOfPeriod for $repeat_freq ' . $repeatFreq); + throw new FireflyException('Cannot do startOfPeriod for $repeat_freq "' . $repeatFreq.'"'); } /** @@ -252,7 +246,7 @@ class Navigation return $date; } - throw new FireflyException('Cannot do subtractPeriod for $repeat_freq ' . $repeatFreq); + throw new FireflyException('Cannot do subtractPeriod for $repeat_freq "' . $repeatFreq.'"'); } /** @@ -288,7 +282,7 @@ class Navigation return $end; } - throw new FireflyException('updateEndDate cannot handle $range ' . $range); + throw new FireflyException('updateEndDate cannot handle $range "' . $range.'"'); } /** @@ -322,7 +316,7 @@ class Navigation return $start; } - throw new FireflyException('updateStartDate cannot handle $range ' . $range); + throw new FireflyException('updateStartDate cannot handle $range "' . $range.'"'); } diff --git a/tests/support/NavigationTest.php b/tests/support/NavigationTest.php new file mode 100644 index 0000000000..7e93a09e69 --- /dev/null +++ b/tests/support/NavigationTest.php @@ -0,0 +1,287 @@ +object = new Navigation; + } + + /** + * @covers FireflyIII\Support\Navigation::addPeriod + */ + public function testAddPeriod() + { + $date = new Carbon('2015-01-01'); + + $result = $this->object->addPeriod($date, 'quarter', 0); + $this->assertEquals('2015-04-01', $result->format('Y-m-d')); + } + + /** + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage Cannot do addPeriod for $repeat_freq "something" + * @covers FireflyIII\Support\Navigation::addPeriod + */ + public function testAddPeriodError() + { + $date = new Carbon('2015-01-01'); + + $this->object->addPeriod($date, 'something', 0); + + } + + /** + * @covers FireflyIII\Support\Navigation::endOfPeriod + */ + public function testEndOfPeriod() + { + $date = new Carbon('2015-01-01'); + + $result = $this->object->endOfPeriod($date, '1D'); + $this->assertEquals('2015-01-02', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::endOfPeriod + */ + public function testEndOfPeriodModifier() + { + $date = new Carbon('2015-01-01'); + + $result = $this->object->endOfPeriod($date, 'quarter'); + $this->assertEquals('2015-03-31', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::endOfPeriod + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage Cannot do endOfPeriod for $repeat_freq "something" + */ + public function testEndOfPeriodModifierError() + { + $date = new Carbon('2015-01-01'); + + $this->object->endOfPeriod($date, 'something'); + } + + /** + * @covers FireflyIII\Support\Navigation::endOfX + */ + public function testEndOfX() + { + $date = new Carbon('2015-01-01'); + $maxEnd = new Carbon('2016-01-01'); + + $result = $this->object->endOfX($date, 'month', $maxEnd); + + $this->assertEquals('2015-01-31', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::endOfX + */ + public function testEndOfXBeyondMax() + { + $date = new Carbon('2015-01-01'); + $maxEnd = new Carbon('2015-01-15'); + + $result = $this->object->endOfX($date, 'monthly', $maxEnd); + + $this->assertEquals('2015-01-15', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::periodShow + */ + public function testPeriodShow() + { + $date = new Carbon('2015-01-01'); + $result = $this->object->periodShow($date, 'month'); + $this->assertEquals('January 2015', $result); + } + + /** + * @covers FireflyIII\Support\Navigation::periodShow + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage No date formats for frequency "something" + */ + public function testPeriodShowError() + { + $date = new Carbon('2015-01-01'); + $this->object->periodShow($date, 'something'); + } + + + /** + * @covers FireflyIII\Support\Navigation::startOfPeriod + */ + public function testStartOfPeriod() + { + $date = new Carbon('2015-01-15'); + $result = $this->object->startOfPeriod($date, 'month'); + $this->assertEquals('2015-01-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::startOfPeriod + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage Cannot do startOfPeriod for $repeat_freq "something" + */ + public function testStartOfPeriodError() + { + $date = new Carbon('2015-08-15'); + $this->object->startOfPeriod($date, 'something'); + } + + /** + * @covers FireflyIII\Support\Navigation::startOfPeriod + */ + public function testStartOfPeriodHalfYear() + { + $date = new Carbon('2015-01-15'); + $result = $this->object->startOfPeriod($date, 'half-year'); + $this->assertEquals('2015-01-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::startOfPeriod + */ + public function testStartOfPeriodHalfYearSecondHalf() + { + $date = new Carbon('2015-08-15'); + $result = $this->object->startOfPeriod($date, 'half-year'); + $this->assertEquals('2015-07-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::subtractPeriod + */ + public function testSubtractPeriod() + { + $date = new Carbon('2015-01-01'); + $result = $this->object->subtractPeriod($date, 'month'); + $this->assertEquals('2014-12-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::subtractPeriod + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage Cannot do subtractPeriod for $repeat_freq "something" + */ + public function testSubtractPeriodError() + { + $date = new Carbon('2015-01-01'); + $this->object->subtractPeriod($date, 'something'); + } + + /** + * @covers FireflyIII\Support\Navigation::subtractPeriod + */ + public function testSubtractPeriodQuarter() + { + $date = new Carbon('2015-01-01'); + $result = $this->object->subtractPeriod($date, 'quarter'); + $this->assertEquals('2014-10-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateEndDate + */ + public function testUpdateEndDate() + { + $date = new Carbon('2015-01-15'); + $result = $this->object->updateEndDate('1M', $date); + $this->assertEquals('2015-01-31', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateEndDate + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage updateEndDate cannot handle $range "something" + */ + public function testUpdateEndDateError() + { + $date = new Carbon('2015-01-15'); + $this->object->updateEndDate('something', $date); + } + + /** + * @covers FireflyIII\Support\Navigation::updateEndDate + */ + public function testUpdateEndDateHalf() + { + $date = new Carbon('2015-01-15'); + $result = $this->object->updateEndDate('6M', $date); + $this->assertEquals('2015-07-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateEndDate + */ + public function testUpdateEndDateSecondHalf() + { + $date = new Carbon('2015-08-15'); + $result = $this->object->updateEndDate('6M', $date); + $this->assertEquals('2015-12-31', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateStartDate + */ + public function testUpdateStartDate() + { + $date = new Carbon('2015-01-15'); + $result = $this->object->updateStartDate('1M', $date); + $this->assertEquals('2015-01-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateStartDate + */ + public function testUpdateStartDateHalf() + { + $date = new Carbon('2015-01-15'); + $result = $this->object->updateStartDate('6M', $date); + $this->assertEquals('2015-01-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateStartDate + */ + public function testUpdateStartDateSecondHalf() + { + $date = new Carbon('2015-09-15'); + $result = $this->object->updateStartDate('6M', $date); + $this->assertEquals('2015-07-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateStartDate + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage updateStartDate cannot handle $range "something" + */ + public function testUpdateStartDateError() + { + $date = new Carbon('2015-09-15'); + $this->object->updateStartDate('something', $date); + } + + +} \ No newline at end of file