diff --git a/Controller/AirPollutionController.php b/Controller/AirPollutionController.php index a796dc8..9bbc4d8 100644 --- a/Controller/AirPollutionController.php +++ b/Controller/AirPollutionController.php @@ -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; @@ -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; diff --git a/Controller/DailyForecastController.php b/Controller/DailyForecastController.php index ad69856..f395840 100644 --- a/Controller/DailyForecastController.php +++ b/Controller/DailyForecastController.php @@ -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; @@ -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; @@ -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; diff --git a/Controller/HourForecastController.php b/Controller/HourForecastController.php index e0aff93..0ca78d2 100644 --- a/Controller/HourForecastController.php +++ b/Controller/HourForecastController.php @@ -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; @@ -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; @@ -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; diff --git a/Controller/UltravioletIndexController.php b/Controller/UltravioletIndexController.php index 070c9d5..bab528e 100644 --- a/Controller/UltravioletIndexController.php +++ b/Controller/UltravioletIndexController.php @@ -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; diff --git a/Controller/WeatherController.php b/Controller/WeatherController.php index f695a8a..9d1de60 100644 --- a/Controller/WeatherController.php +++ b/Controller/WeatherController.php @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/Controller/WeatherStationsController.php b/Controller/WeatherStationsController.php index 4ded4c5..1700c98 100644 --- a/Controller/WeatherStationsController.php +++ b/Controller/WeatherStationsController.php @@ -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; @@ -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; @@ -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; diff --git a/Tests/Controller/DailyForecastControllerTest.php b/Tests/Controller/DailyForecastControllerTest.php new file mode 100644 index 0000000..a8ab493 --- /dev/null +++ b/Tests/Controller/DailyForecastControllerTest.php @@ -0,0 +1,177 @@ +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()); + } +} diff --git a/Tests/Controller/HourForecastControllerTest.php b/Tests/Controller/HourForecastControllerTest.php new file mode 100644 index 0000000..3b30c7d --- /dev/null +++ b/Tests/Controller/HourForecastControllerTest.php @@ -0,0 +1,177 @@ +getMockBuilder(HourForecast::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchForecastByCityName')) + ->getMock(); + + $hourForecast->expects($this->once()) + ->willReturn('some_data') + ->method('fetchForecastByCityName'); + + $hourForecastController = new HourForecastController($hourForecast); + $response = $hourForecastController->getForecastByCityName('London', 'uk'); + + $this->assertInstanceOf(Response::class, $response); + } + + public function testGetForecastByCityNameWithNotAuthorizedException() + { + $hourForecast = $this->getMockBuilder(HourForecast::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchForecastByCityName')) + ->getMock(); + + $hourForecast->expects($this->once()) + ->willThrowException(new NotAuthorizedException('Not authorized')) + ->method('fetchForecastByCityName'); + + $hourForecastController = new HourForecastController($hourForecast); + $response = $hourForecastController->getForecastByCityName('London', 'uk'); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not authorized', $response->getContent()); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + public function testGetForecastByCityNameWithNotFoundException() + { + $hourForecast = $this->getMockBuilder(HourForecast::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchForecastByCityName')) + ->getMock(); + + $hourForecast->expects($this->once()) + ->willThrowException(new NotFoundException('Not found')) + ->method('fetchForecastByCityName'); + + $hourForecastController = new HourForecastController($hourForecast); + $response = $hourForecastController->getForecastByCityName('London', 'uk'); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not found', $response->getContent()); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } + + public function testGetForecastByCityId() + { + $hourForecast = $this->getMockBuilder(HourForecast::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchForecastByCityId')) + ->getMock(); + + $hourForecast->expects($this->once()) + ->willReturn('some_data') + ->method('fetchForecastByCityId'); + + $hourForecastController = new HourForecastController($hourForecast); + $response = $hourForecastController->getForecastByCityId(94040); + + $this->assertInstanceOf(Response::class, $response); + } + + public function testGetForecastByCityIdWithNotAuthorizedException() + { + $hourForecast = $this->getMockBuilder(HourForecast::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchForecastByCityId')) + ->getMock(); + + $hourForecast->expects($this->once()) + ->willThrowException(new NotAuthorizedException('Not authorized')) + ->method('fetchForecastByCityId'); + + $hourForecastController = new HourForecastController($hourForecast); + $response = $hourForecastController->getForecastByCityId(94040); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not authorized', $response->getContent()); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + public function testGetForecastByCityIdWithNotFoundException() + { + $hourForecast = $this->getMockBuilder(HourForecast::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchForecastByCityId')) + ->getMock(); + + $hourForecast->expects($this->once()) + ->willThrowException(new NotFoundException('Not found')) + ->method('fetchForecastByCityId'); + + $hourForecastController = new HourForecastController($hourForecast); + $response = $hourForecastController->getForecastByCityId(94040); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not found', $response->getContent()); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } + + public function testGetForecastByCityGeographicCoordinates() + { + $hourForecast = $this->getMockBuilder(HourForecast::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchForecastByCityGeographicCoordinates')) + ->getMock(); + + $hourForecast->expects($this->once()) + ->willReturn('some_data') + ->method('fetchForecastByCityGeographicCoordinates'); + + $hourForecastController = new HourForecastController($hourForecast); + $response = $hourForecastController->getForecastByCityGeographicCoordinates(23, 129); + + $this->assertInstanceOf(Response::class, $response); + } + + public function testGetForecastByCityGeographicCoordinatesWithNotAuthorizedException() + { + $hourForecast = $this->getMockBuilder(HourForecast::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchForecastByCityGeographicCoordinates')) + ->getMock(); + + $hourForecast->expects($this->once()) + ->willThrowException(new NotAuthorizedException('Not authorized')) + ->method('fetchForecastByCityGeographicCoordinates'); + + $hourForecastController = new HourForecastController($hourForecast); + $response = $hourForecastController->getForecastByCityGeographicCoordinates(23, 129); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not authorized', $response->getContent()); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + public function testGetForecastByCityGeographicCoordinatesWithNotFoundException() + { + $hourForecast = $this->getMockBuilder(HourForecast::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchForecastByCityGeographicCoordinates')) + ->getMock(); + + $hourForecast->expects($this->once()) + ->willThrowException(new NotFoundException('Not found')) + ->method('fetchForecastByCityGeographicCoordinates'); + + $hourForecastController = new HourForecastController($hourForecast); + $response = $hourForecastController->getForecastByCityGeographicCoordinates(23, 129); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not found', $response->getContent()); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } +} diff --git a/Tests/Controller/WeatherControllerTest.php b/Tests/Controller/WeatherControllerTest.php new file mode 100644 index 0000000..c973d34 --- /dev/null +++ b/Tests/Controller/WeatherControllerTest.php @@ -0,0 +1,342 @@ +getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataByGeographicCoordinates')) + ->getMock(); + + $weather->expects($this->once()) + ->willReturn('some_data') + ->method('fetchWeatherDataByGeographicCoordinates'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byGeographicCoordinates(20, 139); + + $this->assertInstanceOf(Response::class, $response); + } + + public function testByGeographicCoordinatesWithNotAuthorizedException() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataByGeographicCoordinates')) + ->getMock(); + + $weather->expects($this->once()) + ->willThrowException(new NotAuthorizedException('Not authorized')) + ->method('fetchWeatherDataByGeographicCoordinates'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byGeographicCoordinates(20, 139); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not authorized', $response->getContent()); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + public function testByGeographicCoordinatesWithNotFoundException() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataByGeographicCoordinates')) + ->getMock(); + + $weather->expects($this->once()) + ->willThrowException(new NotFoundException('Not found')) + ->method('fetchWeatherDataByGeographicCoordinates'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byGeographicCoordinates(20, 139); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not found', $response->getContent()); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } + + public function testByCityName() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataByCityName')) + ->getMock(); + + $weather->expects($this->once()) + ->willReturn('some_data') + ->method('fetchWeatherDataByCityName'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byCityName('London', 'uk'); + + $this->assertInstanceOf(Response::class, $response); + } + + public function testByCityNameWithNotAuthorizedException() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataByCityName')) + ->getMock(); + + $weather->expects($this->once()) + ->willThrowException(new NotAuthorizedException('Not authorized')) + ->method('fetchWeatherDataByCityName'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byCityName('London', 'uk'); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not authorized', $response->getContent()); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + public function testByCityNameWithNotFoundException() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataByCityName')) + ->getMock(); + + $weather->expects($this->once()) + ->willThrowException(new NotFoundException('Not found')) + ->method('fetchWeatherDataByCityName'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byCityName('London', 'uk'); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not found', $response->getContent()); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } + + public function testByCityId() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataByCityId')) + ->getMock(); + + $weather->expects($this->once()) + ->willReturn('some_data') + ->method('fetchWeatherDataByCityId'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byCityId(12345); + + $this->assertInstanceOf(Response::class, $response); + } + + public function testByCityIdWithNotAuthorizedException() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataByCityId')) + ->getMock(); + + $weather->expects($this->once()) + ->willThrowException(new NotAuthorizedException('Not authorized')) + ->method('fetchWeatherDataByCityId'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byCityId(12345); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not authorized', $response->getContent()); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + public function testByCityIdWithNotFoundException() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataByCityId')) + ->getMock(); + + $weather->expects($this->once()) + ->willThrowException(new NotFoundException('Not found')) + ->method('fetchWeatherDataByCityId'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byCityId(12345); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not found', $response->getContent()); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } + + public function testByZipCode() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataByZipCode')) + ->getMock(); + + $weather->expects($this->once()) + ->willReturn('some_data') + ->method('fetchWeatherDataByZipCode'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byZipCode(12345, 'uk'); + + $this->assertInstanceOf(Response::class, $response); + } + + public function testByZipCodeWithNotAuthorizedException() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataByZipCode')) + ->getMock(); + + $weather->expects($this->once()) + ->willThrowException(new NotAuthorizedException('Not authorized')) + ->method('fetchWeatherDataByZipCode'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byZipCode(12345, 'uk'); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not authorized', $response->getContent()); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + public function testByZipCodeWithNotFoundException() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataByZipCode')) + ->getMock(); + + $weather->expects($this->once()) + ->willThrowException(new NotFoundException('Not found')) + ->method('fetchWeatherDataByZipCode'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byZipCode(12345, 'uk'); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not found', $response->getContent()); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } + + public function testByRectangleZone() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataForCitiesWithinRectangleZone')) + ->getMock(); + + $weather->expects($this->once()) + ->willReturn('some_data') + ->method('fetchWeatherDataForCitiesWithinRectangleZone'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byRectangleZone(20, 129, 30, 140, 5, 'yes'); + + $this->assertInstanceOf(Response::class, $response); + } + + public function testByRectangleZoneWithNotAuthorizedException() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataForCitiesWithinRectangleZone')) + ->getMock(); + + $weather->expects($this->once()) + ->willThrowException(new NotAuthorizedException('Not authorized')) + ->method('fetchWeatherDataForCitiesWithinRectangleZone'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byRectangleZone(20, 129, 30, 140, 5, 'yes'); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not authorized', $response->getContent()); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + public function testByRectangleZoneWithNotFoundException() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataForCitiesWithinRectangleZone')) + ->getMock(); + + $weather->expects($this->once()) + ->willThrowException(new NotFoundException('Not found')) + ->method('fetchWeatherDataForCitiesWithinRectangleZone'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byRectangleZone(20, 129, 30, 140, 5, 'yes'); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not found', $response->getContent()); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } + + public function testByCircle() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataForCitiesInCycle')) + ->getMock(); + + $weather->expects($this->once()) + ->willReturn('some_data') + ->method('fetchWeatherDataForCitiesInCycle'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byCircle(20, 130, 'yes', 10); + + $this->assertInstanceOf(Response::class, $response); + } + + public function testByCircleWithNotAuthorizedException() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataForCitiesInCycle')) + ->getMock(); + + $weather->expects($this->once()) + ->willThrowException(new NotAuthorizedException('Not authorized')) + ->method('fetchWeatherDataForCitiesInCycle'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byCircle(20, 130, 'yes', 10); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not authorized', $response->getContent()); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + public function testByCircleWithNotFoundException() + { + $weather = $this->getMockBuilder(Weather::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchWeatherDataForCitiesInCycle')) + ->getMock(); + + $weather->expects($this->once()) + ->willThrowException(new NotFoundException('Not found')) + ->method('fetchWeatherDataForCitiesInCycle'); + + $weatherController = new WeatherController($weather); + $response = $weatherController->byCircle(20, 130, 'yes', 10); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not found', $response->getContent()); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } +} diff --git a/Tests/Controller/WeatherStationsControllerTest.php b/Tests/Controller/WeatherStationsControllerTest.php new file mode 100644 index 0000000..dd555f5 --- /dev/null +++ b/Tests/Controller/WeatherStationsControllerTest.php @@ -0,0 +1,177 @@ +getMockBuilder(WeatherStations::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchFromOnStationById')) + ->getMock(); + + $weatherStations->expects($this->once()) + ->willReturn('some_data') + ->method('fetchFromOnStationById'); + + $weatherStationsController = new WeatherStationsController($weatherStations); + $response = $weatherStationsController->getFromOnStationById(123456); + + $this->assertInstanceOf(Response::class, $response); + } + + public function testGetFromOnStationByIdWithNotAuthorizedException() + { + $weatherStations = $this->getMockBuilder(WeatherStations::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchFromOnStationById')) + ->getMock(); + + $weatherStations->expects($this->once()) + ->willThrowException(new NotAuthorizedException('Not authorized')) + ->method('fetchFromOnStationById'); + + $weatherStationsController = new WeatherStationsController($weatherStations); + $response = $weatherStationsController->getFromOnStationById(123456); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not authorized', $response->getContent()); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + public function testGetFromOnStationByIdWithNotFoundException() + { + $weatherStations = $this->getMockBuilder(WeatherStations::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchFromOnStationById')) + ->getMock(); + + $weatherStations->expects($this->once()) + ->willThrowException(new NotFoundException('Not found')) + ->method('fetchFromOnStationById'); + + $weatherStationsController = new WeatherStationsController($weatherStations); + $response = $weatherStationsController->getFromOnStationById(123456); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not found', $response->getContent()); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } + + public function testGetFromSeveralByRectangleZone() + { + $weatherStations = $this->getMockBuilder(WeatherStations::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchFromSeveralByRectangleZone')) + ->getMock(); + + $weatherStations->expects($this->once()) + ->willReturn('some_data') + ->method('fetchFromSeveralByRectangleZone'); + + $weatherStationsController = new WeatherStationsController($weatherStations); + $response = $weatherStationsController->getFromSeveralByRectangleZone(20, 24, 45, 56, 5, 'yes', 10); + + $this->assertInstanceOf(Response::class, $response); + } + + public function testGetFromSeveralByRectangleZoneWithNotAuthorizedException() + { + $weatherStations = $this->getMockBuilder(WeatherStations::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchFromSeveralByRectangleZone')) + ->getMock(); + + $weatherStations->expects($this->once()) + ->willThrowException(new NotAuthorizedException('Not authorized')) + ->method('fetchFromSeveralByRectangleZone'); + + $weatherStationsController = new WeatherStationsController($weatherStations); + $response = $weatherStationsController->getFromSeveralByRectangleZone(20, 24, 45, 56, 5, 'yes', 10); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not authorized', $response->getContent()); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + public function testGetFromSeveralByRectangleZoneWithNotFoundException() + { + $weatherStations = $this->getMockBuilder(WeatherStations::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchFromSeveralByRectangleZone')) + ->getMock(); + + $weatherStations->expects($this->once()) + ->willThrowException(new NotFoundException('Not found')) + ->method('fetchFromSeveralByRectangleZone'); + + $weatherStationsController = new WeatherStationsController($weatherStations); + $response = $weatherStationsController->getFromSeveralByRectangleZone(20, 24, 45, 56, 5, 'yes', 10); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not found', $response->getContent()); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } + + public function testGetFromSeveralByGeoPoint() + { + $weatherStations = $this->getMockBuilder(WeatherStations::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchFromSeveralByGeoPoint')) + ->getMock(); + + $weatherStations->expects($this->once()) + ->willReturn('some_data') + ->method('fetchFromSeveralByGeoPoint'); + + $weatherStationsController = new WeatherStationsController($weatherStations); + $response = $weatherStationsController->getFromSeveralByGeoPoint(20, 123, 10); + + $this->assertInstanceOf(Response::class, $response); + } + + public function testGetFromSeveralByGeoPointWithNotAuthorizedException() + { + $weatherStations = $this->getMockBuilder(WeatherStations::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchFromSeveralByGeoPoint')) + ->getMock(); + + $weatherStations->expects($this->once()) + ->willThrowException(new NotAuthorizedException('Not authorized')) + ->method('fetchFromSeveralByGeoPoint'); + + $weatherStationsController = new WeatherStationsController($weatherStations); + $response = $weatherStationsController->getFromSeveralByGeoPoint(20, 123, 10); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not authorized', $response->getContent()); + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + public function testGetFromSeveralByGeoPointWithNotFoundException() + { + $weatherStations = $this->getMockBuilder(WeatherStations::class) + ->disableOriginalConstructor() + ->setMethods(array('fetchFromSeveralByGeoPoint')) + ->getMock(); + + $weatherStations->expects($this->once()) + ->willThrowException(new NotFoundException('Not found')) + ->method('fetchFromSeveralByGeoPoint'); + + $weatherStationsController = new WeatherStationsController($weatherStations); + $response = $weatherStationsController->getFromSeveralByGeoPoint(20, 123, 10); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals('Not found', $response->getContent()); + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); + } +}