|
6 | 6 | use Netgen\Bundle\OpenWeatherMapBundle\Core\Weather; |
7 | 7 | use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotAuthorizedException; |
8 | 8 | use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotFoundException; |
| 9 | +use Symfony\Component\HttpFoundation\Request; |
9 | 10 | use Symfony\Component\HttpFoundation\Response; |
10 | 11 |
|
11 | 12 | class WeatherControllerTest extends \PHPUnit_Framework_TestCase |
@@ -339,4 +340,89 @@ public function testByCircleWithNotFoundException() |
339 | 340 | $this->assertEquals('Not found', $response->getContent()); |
340 | 341 | $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); |
341 | 342 | } |
| 343 | + |
| 344 | + public function testByCityIds() |
| 345 | + { |
| 346 | + $weather = $this->getMockBuilder(Weather::class) |
| 347 | + ->disableOriginalConstructor() |
| 348 | + ->setMethods(array('fetchWeatherDataForSeveralCityIds')) |
| 349 | + ->getMock(); |
| 350 | + |
| 351 | + $weather->expects($this->once()) |
| 352 | + ->willReturn('some_data') |
| 353 | + ->method('fetchWeatherDataForSeveralCityIds'); |
| 354 | + |
| 355 | + $request = new Request(array('cities' => 12)); |
| 356 | + |
| 357 | + $weatherController = new WeatherController($weather); |
| 358 | + $response = $weatherController->byCityIds($request); |
| 359 | + |
| 360 | + $this->assertInstanceOf(Response::class, $response); |
| 361 | + } |
| 362 | + |
| 363 | + public function testByCityIdsWithNotAuthorizedException() |
| 364 | + { |
| 365 | + $weather = $this->getMockBuilder(Weather::class) |
| 366 | + ->disableOriginalConstructor() |
| 367 | + ->setMethods(array('fetchWeatherDataForSeveralCityIds')) |
| 368 | + ->getMock(); |
| 369 | + |
| 370 | + $weather->expects($this->once()) |
| 371 | + ->willThrowException(new NotAuthorizedException('Not authorized')) |
| 372 | + ->method('fetchWeatherDataForSeveralCityIds'); |
| 373 | + |
| 374 | + $weatherController = new WeatherController($weather); |
| 375 | + |
| 376 | + $request = new Request(array('cities' => 12)); |
| 377 | + |
| 378 | + $response = $weatherController->byCityIds($request); |
| 379 | + |
| 380 | + $this->assertInstanceOf(Response::class, $response); |
| 381 | + $this->assertEquals('Not authorized', $response->getContent()); |
| 382 | + $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); |
| 383 | + } |
| 384 | + |
| 385 | + public function testByCityIdsWithNotFoundException() |
| 386 | + { |
| 387 | + $weather = $this->getMockBuilder(Weather::class) |
| 388 | + ->disableOriginalConstructor() |
| 389 | + ->setMethods(array('fetchWeatherDataForSeveralCityIds')) |
| 390 | + ->getMock(); |
| 391 | + |
| 392 | + $weather->expects($this->once()) |
| 393 | + ->willThrowException(new NotFoundException('Not found')) |
| 394 | + ->method('fetchWeatherDataForSeveralCityIds'); |
| 395 | + |
| 396 | + $weatherController = new WeatherController($weather); |
| 397 | + |
| 398 | + $request = new Request(array('cities' => 12)); |
| 399 | + |
| 400 | + $response = $weatherController->byCityIds($request); |
| 401 | + |
| 402 | + $this->assertInstanceOf(Response::class, $response); |
| 403 | + $this->assertEquals('Not found', $response->getContent()); |
| 404 | + $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); |
| 405 | + } |
| 406 | + |
| 407 | + public function testByCityIdsWithBadRequest() |
| 408 | + { |
| 409 | + $weather = $this->getMockBuilder(Weather::class) |
| 410 | + ->disableOriginalConstructor() |
| 411 | + ->setMethods(array('fetchWeatherDataForSeveralCityIds')) |
| 412 | + ->getMock(); |
| 413 | + |
| 414 | + $weather->expects($this->never()) |
| 415 | + ->willThrowException(new NotFoundException('Not found')) |
| 416 | + ->method('fetchWeatherDataForSeveralCityIds'); |
| 417 | + |
| 418 | + $weatherController = new WeatherController($weather); |
| 419 | + |
| 420 | + $request = new Request(array()); |
| 421 | + |
| 422 | + $response = $weatherController->byCityIds($request); |
| 423 | + |
| 424 | + $this->assertInstanceOf(Response::class, $response); |
| 425 | + $this->assertEquals('', $response->getContent()); |
| 426 | + $this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
| 427 | + } |
342 | 428 | } |
0 commit comments