Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owm city #334

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Entity/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class City implements \Stringable
#[ORM\OneToMany(targetEntity: 'Station', mappedBy: 'city')]
protected $stations;

#[ORM\Column(type: 'integer', nullable: true)]
#[JMS\Expose]
protected ?int $openWeatherMapCityId = null;

public function __construct()
{
$this->createdAt = new \DateTime();
Expand Down Expand Up @@ -126,4 +130,16 @@ public function __toString(): string
{
return $this->name ?: '';
}

public function getOpenWeatherMapCityId(): ?int
{
return $this->openWeatherMapCityId;
}

public function setOpenWeatherMapCityId(?int $openWeatherMapCityId): self
{
$this->openWeatherMapCityId = $openWeatherMapCityId;

return $this;
}
}
28 changes: 28 additions & 0 deletions src/Migrations/Version20190725165138.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20190725165138 extends AbstractMigration
{
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE city ADD open_weather_map_city_id INT DEFAULT NULL');
}

public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE city DROP open_weather_map_city_id');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace App\Provider\OpenWeatherMapProvider\CityIdHandler;

class CityDataLoader
{
public function loadCityData(): array
{
return json_decode(file_get_contents( __DIR__.'/../../../../public/city.list.json'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

namespace App\Provider\OpenWeatherMapProvider\CityIdHandler;

use App\Entity\City;
use Symfony\Bridge\Doctrine\RegistryInterface;

class CityIdHandler
{
/** @var array $cityDataList */
protected $cityDataList;

/** @var RegistryInterface $registry */
protected $registry;

/** @var CityDataLoader $cityDataLoader */
protected $cityDataLoader;

/** @var array $cityList */
protected $cityList = [];

public function __construct(RegistryInterface $registry, CityDataLoader $cityDataLoader)
{
$this->registry = $registry;
$this->cityDataLoader = $cityDataLoader;
}

public function assign(): void
{
$this->cityDataList = $this->cityDataLoader->loadCityData();

$this->cityList = $this->registry->getRepository(City::class)->findAll();

/** @var City $city */
foreach ($this->cityList as $city) {
if ($city->getOpenWeatherMapCityId()) {
continue;
}

/** @var array $cityData */
foreach ($this->cityDataList as $cityData) {
if ($cityData['name'] === $city->getName()) {
dump($cityData);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php declare(strict_types=1);

namespace App\Tests\Provider\OpenWeatherMapProvider\CityIdHandler;

use App\Entity\City;
use App\Provider\OpenWeatherMapProvider\CityIdHandler\CityDataLoader;
use App\Provider\OpenWeatherMapProvider\CityIdHandler\CityIdHandler;
use App\Repository\CityRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\RegistryInterface;

class CityIdHandlerTest extends TestCase
{
public function testCalls(): void
{
$cityRepository = $this->createMock(CityRepository::class);
$cityRepository
->expects($this->once())
->method('findAll')
->with()
->will($this->returnValue([]));

$cityDataLoader = $this->createMock(CityDataLoader::class);
$registry = $this->createMock(RegistryInterface::class);
$registry
->expects($this->once())
->method('getRepository')
->with($this->equalTo(City::class))
->will($this->returnValue($cityRepository));

$cityIdHandler = new CityIdHandler($registry, $cityDataLoader);
$cityIdHandler->assign();
}

public function testHamburg(): void
{
$mockedCityDataList = [
[
'id' => 2911297,
'name' => 'Freie und Hansestadt Hamburg',
'country' => 'DE',
'coord' => [
'lon' => 10,
'lat' => 53.583328,
]
],
[
'id' => 2956829,
'name' => 'Altstadt',
'country' => 'DE',
'coord' => [
'lon' => 10,
'lat' => 53.549999,
]
],
[
'id' => 4627266,
'name' => 'Hamburg',
'country' => 'US',
'coord' => [
'lon' => -88.304207,
'lat' => 35.09481,
]
],
];

$mockedCity = new City();
$mockedCity
->setName('Hamburg');

$mockedCityList = [$mockedCity];

$cityRepository = $this->createMock(CityRepository::class);
$cityRepository
->expects($this->once())
->method('findAll')
->with()
->will($this->returnValue($mockedCityList));

$registry = $this->createMock(RegistryInterface::class);
$registry
->expects($this->once())
->method('getRepository')
->with($this->equalTo(City::class))
->will($this->returnValue($cityRepository));

$cityDataLoader = $this->createMock(CityDataLoader::class);
$cityDataLoader
->expects($this->once())
->method('loadCityData')
->will($this->returnValue($mockedCityDataList));

$cityIdHandler = new CityIdHandler($registry, $cityDataLoader);
$cityIdHandler->assign();
}
}