Skip to content

Commit

Permalink
Controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Blazek committed Jun 5, 2016
1 parent 09011e4 commit 5cfd5b2
Show file tree
Hide file tree
Showing 10 changed files with 912 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Controller/AirPollutionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ public function getOzoneData($latitude, $longitude, $datetime = 'current')
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand Down Expand Up @@ -90,8 +92,10 @@ public function getCarbonMonoxideData($latitude, $longitude, $datetime = 'curren
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand Down
6 changes: 6 additions & 0 deletions Controller/DailyForecastController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public function getForecastByCityName($cityName, $numberOfDays = 16, $countryCod
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand All @@ -70,8 +72,10 @@ public function getForecastByCityId($cityId, $numberOfDays = 16)
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand All @@ -95,8 +99,10 @@ public function getForecastByCityGeographicCoordinates($latitude, $longitude, $n
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand Down
8 changes: 7 additions & 1 deletion Controller/HourForecastController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public function getForecastByCityName($cityName, $countryCode = '')
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand All @@ -68,8 +70,10 @@ public function getForecastByCityId($cityId)
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand All @@ -88,12 +92,14 @@ public function getForecastByCityGeographicCoordinates($latitude, $longitude)
$response = new Response();

try {
$data = $this->hourForecast->fetchForecastByCityName($latitude, $longitude);
$data = $this->hourForecast->fetchForecastByCityGeographicCoordinates($latitude, $longitude);
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand Down
2 changes: 2 additions & 0 deletions Controller/UltravioletIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ public function getUltravioletIndex($latitude, $longitude, $datetime = 'current'
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand Down
14 changes: 14 additions & 0 deletions Controller/WeatherController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public function byGeographicCoordinates($latitude, $longitude)
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand All @@ -71,8 +73,10 @@ public function byCityName($cityName, $countryCode = '')
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand All @@ -94,8 +98,10 @@ public function byCityId($cityId)
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand All @@ -118,8 +124,10 @@ public function byZipCode($zipCode, $countryCode = '')
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand Down Expand Up @@ -148,8 +156,10 @@ public function byRectangleZone($longitudeLeft, $latitudeBottom, $logitudeRigth,
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand All @@ -174,8 +184,10 @@ public function byCircle($latitude, $longitude, $cluster = 'yes', $numberOfCitie
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand Down Expand Up @@ -203,8 +215,10 @@ public function byCityIds(Request $request)
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand Down
6 changes: 6 additions & 0 deletions Controller/WeatherStationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ public function getFromOnStationById($stationId)
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand Down Expand Up @@ -77,8 +79,10 @@ public function getFromSeveralByRectangleZone($longitudeTopLeft, $latitudeTopLef
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand All @@ -102,8 +106,10 @@ public function getFromSeveralByGeoPoint($latitude, $longitude, $numberOfStation
$response->setContent($data);
} catch (NotAuthorizedException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
} catch (NotFoundException $e) {
$response->setContent($e->getMessage());
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}

return $response;
Expand Down
177 changes: 177 additions & 0 deletions Tests/Controller/DailyForecastControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

namespace Netgen\Bundle\OpenWeatherMapBundle\Tests\Controller;

use Netgen\Bundle\OpenWeatherMapBundle\Controller\DailyForecastController;
use Netgen\Bundle\OpenWeatherMapBundle\Core\DailyForecast;
use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotAuthorizedException;
use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotFoundException;
use Symfony\Component\HttpFoundation\Response;

class DailyForecastControllerTest extends \PHPUnit_Framework_TestCase
{
public function testGetForecastByCityName()
{
$dailyForecast = $this->getMockBuilder(DailyForecast::class)
->disableOriginalConstructor()
->setMethods(array('fetchForecastByCityName'))
->getMock();

$dailyForecast->expects($this->once())
->willReturn('some_data')
->method('fetchForecastByCityName');

$dailyForecastController = new DailyForecastController($dailyForecast);
$response = $dailyForecastController->getForecastByCityName('London', 10, 'uk');

$this->assertInstanceOf(Response::class, $response);
}

public function testGetForecastByCityNameWithNotAuthorizedException()
{
$dailyForecast = $this->getMockBuilder(DailyForecast::class)
->disableOriginalConstructor()
->setMethods(array('fetchForecastByCityName'))
->getMock();

$dailyForecast->expects($this->once())
->willThrowException(new NotAuthorizedException('Not authorized'))
->method('fetchForecastByCityName');

$dailyForecastController = new DailyForecastController($dailyForecast);
$response = $dailyForecastController->getForecastByCityName('London', 10, 'uk');

$this->assertInstanceOf(Response::class, $response);
$this->assertEquals('Not authorized', $response->getContent());
$this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode());
}

public function testGetForecastByCityNameWithNotFoundException()
{
$dailyForecast = $this->getMockBuilder(DailyForecast::class)
->disableOriginalConstructor()
->setMethods(array('fetchForecastByCityName'))
->getMock();

$dailyForecast->expects($this->once())
->willThrowException(new NotFoundException('Not found'))
->method('fetchForecastByCityName');

$dailyForecastController = new DailyForecastController($dailyForecast);
$response = $dailyForecastController->getForecastByCityName('London', 10, 'uk');

$this->assertInstanceOf(Response::class, $response);
$this->assertEquals('Not found', $response->getContent());
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
}

public function testGetForecastByCityId()
{
$dailyForecast = $this->getMockBuilder(DailyForecast::class)
->disableOriginalConstructor()
->setMethods(array('fetchForecastByCityId'))
->getMock();

$dailyForecast->expects($this->once())
->willReturn('some_data')
->method('fetchForecastByCityId');

$dailyForecastController = new DailyForecastController($dailyForecast);
$response = $dailyForecastController->getForecastByCityId(9040, 10);

$this->assertInstanceOf(Response::class, $response);
}

public function testGetForecastByCityIdWithNotAuthorizedException()
{
$dailyForecast = $this->getMockBuilder(DailyForecast::class)
->disableOriginalConstructor()
->setMethods(array('fetchForecastByCityId'))
->getMock();

$dailyForecast->expects($this->once())
->willThrowException(new NotAuthorizedException('Not authorized'))
->method('fetchForecastByCityId');

$dailyForecastController = new DailyForecastController($dailyForecast);
$response = $dailyForecastController->getForecastByCityId(9040, 10);

$this->assertInstanceOf(Response::class, $response);
$this->assertEquals('Not authorized', $response->getContent());
$this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode());
}

public function testGetForecastByCityIdWithNotFoundException()
{
$dailyForecast = $this->getMockBuilder(DailyForecast::class)
->disableOriginalConstructor()
->setMethods(array('fetchForecastByCityId'))
->getMock();

$dailyForecast->expects($this->once())
->willThrowException(new NotFoundException('Not found'))
->method('fetchForecastByCityId');

$dailyForecastController = new DailyForecastController($dailyForecast);
$response = $dailyForecastController->getForecastByCityId(9040, 10);

$this->assertInstanceOf(Response::class, $response);
$this->assertEquals('Not found', $response->getContent());
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
}

public function testGetForecastByCityGeographicCoordinates()
{
$dailyForecast = $this->getMockBuilder(DailyForecast::class)
->disableOriginalConstructor()
->setMethods(array('fetchForecastByCityGeographicCoordinates'))
->getMock();

$dailyForecast->expects($this->once())
->willReturn('some_data')
->method('fetchForecastByCityGeographicCoordinates');

$dailyForecastController = new DailyForecastController($dailyForecast);
$response = $dailyForecastController->getForecastByCityGeographicCoordinates(32, 123, 10);

$this->assertInstanceOf(Response::class, $response);
}

public function testGetForecastByCityGeographicCoordinatesWithNotAuthorizedException()
{
$dailyForecast = $this->getMockBuilder(DailyForecast::class)
->disableOriginalConstructor()
->setMethods(array('fetchForecastByCityGeographicCoordinates'))
->getMock();

$dailyForecast->expects($this->once())
->willThrowException(new NotAuthorizedException('Not authorized'))
->method('fetchForecastByCityGeographicCoordinates');

$dailyForecastController = new DailyForecastController($dailyForecast);
$response = $dailyForecastController->getForecastByCityGeographicCoordinates(32, 123, 10);

$this->assertInstanceOf(Response::class, $response);
$this->assertEquals('Not authorized', $response->getContent());
$this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode());
}

public function testGetForecastByCityGeographicCoordinatesWithNotFoundException()
{
$dailyForecast = $this->getMockBuilder(DailyForecast::class)
->disableOriginalConstructor()
->setMethods(array('fetchForecastByCityGeographicCoordinates'))
->getMock();

$dailyForecast->expects($this->once())
->willThrowException(new NotFoundException('Not found'))
->method('fetchForecastByCityGeographicCoordinates');

$dailyForecastController = new DailyForecastController($dailyForecast);
$response = $dailyForecastController->getForecastByCityGeographicCoordinates(32, 123, 10);

$this->assertInstanceOf(Response::class, $response);
$this->assertEquals('Not found', $response->getContent());
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
}
}
Loading

0 comments on commit 5cfd5b2

Please sign in to comment.