From c6b7515285166700a6c49c076eae3f4371923fd5 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 31 Jul 2016 16:10:16 +0200 Subject: [PATCH] Created GetVisitsActionTest --- .../Rest/test/Action/GetVisitsActionTest.php | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 module/Rest/test/Action/GetVisitsActionTest.php diff --git a/module/Rest/test/Action/GetVisitsActionTest.php b/module/Rest/test/Action/GetVisitsActionTest.php new file mode 100644 index 00000000..901c549e --- /dev/null +++ b/module/Rest/test/Action/GetVisitsActionTest.php @@ -0,0 +1,99 @@ +visitsTracker = $this->prophesize(VisitsTracker::class); + $this->action = new GetVisitsAction($this->visitsTracker->reveal(), Translator::factory([])); + } + + /** + * @test + */ + public function providingCorrectShortCodeReturnsVisits() + { + $shortCode = 'abc123'; + $this->visitsTracker->info($shortCode, Argument::type(DateRange::class))->willReturn([]) + ->shouldBeCalledTimes(1); + + $response = $this->action->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), + new Response() + ); + $this->assertEquals(200, $response->getStatusCode()); + } + + /** + * @test + */ + public function providingInvalidShortCodeReturnsError() + { + $shortCode = 'abc123'; + $this->visitsTracker->info($shortCode, Argument::type(DateRange::class))->willThrow( + InvalidArgumentException::class + )->shouldBeCalledTimes(1); + + $response = $this->action->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), + new Response() + ); + $this->assertEquals(400, $response->getStatusCode()); + } + + /** + * @test + */ + public function unexpectedExceptionWillReturnError() + { + $shortCode = 'abc123'; + $this->visitsTracker->info($shortCode, Argument::type(DateRange::class))->willThrow( + \Exception::class + )->shouldBeCalledTimes(1); + + $response = $this->action->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), + new Response() + ); + $this->assertEquals(500, $response->getStatusCode()); + } + + /** + * @test + */ + public function datesAreReadFromQuery() + { + $shortCode = 'abc123'; + $this->visitsTracker->info($shortCode, new DateRange(null, new \DateTime('2016-01-01 00:00:00'))) + ->willReturn([]) + ->shouldBeCalledTimes(1); + + $response = $this->action->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode) + ->withQueryParams(['endDate' => '2016-01-01 00:00:00']), + new Response() + ); + $this->assertEquals(200, $response->getStatusCode()); + } +}