96 lines
3.3 KiB
PHP
96 lines
3.3 KiB
PHP
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace App\Tests\Functional\Controller;
|
||
|
|
||
|
use App\Controller\ForecastController;
|
||
|
use App\DTO\Edge\Response\Mapper\WeatherAtTimePointResponseMapper;
|
||
|
use App\DTO\GEOPoint;
|
||
|
use App\DTO\WeatherAtTimePoint;
|
||
|
use App\Services\GeoPointResolverService;
|
||
|
use App\Services\WeatherStorageReceiverInterface;
|
||
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||
|
|
||
|
class ForecastControllerTest extends KernelTestCase
|
||
|
{
|
||
|
public function testGetForecast(): void
|
||
|
{
|
||
|
list($weatherPoint, $controller) = $this->createController('Prague', false, true);
|
||
|
/**
|
||
|
* @var WeatherAtTimePointResponseMapper $timePointResponseMapper
|
||
|
*/
|
||
|
$timePointResponseMapper = $this->getContainer()->get(WeatherAtTimePointResponseMapper::class);
|
||
|
$expectedResponse = new \stdClass();
|
||
|
$expectedResponse->city = 'Prague';
|
||
|
$expectedResponse->temperature = $timePointResponseMapper->fromWeatherAtTimePointsDateAsYYYYmmdd($weatherPoint);
|
||
|
|
||
|
$result = $controller->getForecast('Prague');
|
||
|
$this->assertEquals($result->getStatusCode(), 200);
|
||
|
$this->assertEquals(
|
||
|
json_encode($expectedResponse),
|
||
|
$result->getContent()
|
||
|
);
|
||
|
}
|
||
|
|
||
|
public function testGetInvalidCity(): void
|
||
|
{
|
||
|
list($_, $controller) = $this->createController('Atlantis', true, true);
|
||
|
$result = $controller->getForecast('Atlantis');
|
||
|
$this->assertEquals($result->getStatusCode(), 404);
|
||
|
}
|
||
|
|
||
|
public function testDataNotFound(): void
|
||
|
{
|
||
|
list($_, $controller) = $this->createController('Prague', false, false);
|
||
|
$result = $controller->getForecast('Prague');
|
||
|
$this->assertEquals($result->getStatusCode(), 404);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return array<mixed>
|
||
|
*/
|
||
|
public function createController(string $cityName, bool $invalidCity, bool $createWeatherPoints): array
|
||
|
{
|
||
|
$weatherPoint = match ($createWeatherPoints) {
|
||
|
true => [
|
||
|
new WeatherAtTimePoint(
|
||
|
new \DateTime(),
|
||
|
10,
|
||
|
10,
|
||
|
10
|
||
|
),
|
||
|
|
||
|
new WeatherAtTimePoint(
|
||
|
new \DateTime(),
|
||
|
9,
|
||
|
9,
|
||
|
9
|
||
|
),
|
||
|
],
|
||
|
false => []
|
||
|
};
|
||
|
$point = new GEOPoint(
|
||
|
22,
|
||
|
22
|
||
|
);
|
||
|
$weatherStorageReceiverMock = \Mockery::mock(WeatherStorageReceiverInterface::class);
|
||
|
// @phpstan-ignore-next-line method.notFound
|
||
|
$weatherStorageReceiverMock->shouldReceive('receiveWeatherForPointByDay')->with($point, \Mockery::any(), \Mockery::any())
|
||
|
->andReturn(
|
||
|
$weatherPoint
|
||
|
);
|
||
|
|
||
|
$geoPointResolverMock = \Mockery::mock(GeoPointResolverService::class);
|
||
|
// @phpstan-ignore-next-line method.notFound
|
||
|
$geoPointResolverMock->shouldReceive('getGeoPointFromCityName')->with($cityName)->andReturn(
|
||
|
$invalidCity === true ? null : $point
|
||
|
);
|
||
|
|
||
|
$this->getContainer()->set(WeatherStorageReceiverInterface::class, $weatherStorageReceiverMock);
|
||
|
$this->getContainer()->set(GeoPointResolverService::class, $geoPointResolverMock);
|
||
|
$controller = $this->getContainer()->get(ForecastController::class);
|
||
|
return array($weatherPoint, $controller);
|
||
|
}
|
||
|
}
|