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->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)); $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->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)); $this->assertEquals(404, $response->getStatusCode()); } /** * @test */ public function unexpectedExceptionWillReturnError() { $shortCode = 'abc123'; $this->visitsTracker->info($shortCode, Argument::type(DateRange::class))->willThrow( \Exception::class )->shouldBeCalledTimes(1); $response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)); $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->handle( ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode) ->withQueryParams(['endDate' => '2016-01-01 00:00:00']) ); $this->assertEquals(200, $response->getStatusCode()); } }