|
18 | 18 | use Doctrine\Common\Annotations\SimpleAnnotationReader; |
19 | 19 | use Doctrine\DBAL\DriverManager; |
20 | 20 | use Doctrine\ORM\EntityManager; |
| 21 | +use Doctrine\ORM\Events; |
21 | 22 | use Doctrine\ORM\Tools\SchemaTool; |
22 | 23 | use Doctrine\Tests\DoctrineTestCase; |
23 | 24 | use Doctrine\Tests\OrmTestCase; |
24 | 25 | use Geocoder\Provider\Nominatim\Nominatim; |
25 | 26 | use Http\Client\Curl\Client; |
| 27 | +use Psr\Http\Message\RequestInterface; |
| 28 | +use Psr\Http\Message\ResponseInterface; |
26 | 29 | use Symfony\Bridge\PhpUnit\SetUpTearDownTrait; |
27 | 30 |
|
28 | 31 | /** |
@@ -148,6 +151,39 @@ public function testPersistForEmptyProperty() |
148 | 151 | $this->assertNull($dummy->latitude); |
149 | 152 | $this->assertNull($dummy->longitude); |
150 | 153 | } |
| 154 | + |
| 155 | + public function testDoesNotGeocodeIfAddressNotChanged() |
| 156 | + { |
| 157 | + $this->em->getEventManager()->removeEventListener(Events::onFlush, $this->listener); |
| 158 | + |
| 159 | + $reader = new SimpleAnnotationReader(); |
| 160 | + $reader->addNamespace('Bazinga\GeocoderBundle\Mapping\Annotations'); |
| 161 | + $reader->addNamespace('Doctrine\ORM\Mapping'); |
| 162 | + |
| 163 | + $driver = new AnnotationDriver($reader); |
| 164 | + |
| 165 | + $client = new TrackedCurlClient(); |
| 166 | + $geocoder = Nominatim::withOpenStreetMapServer($client, 'BazingaGeocoderBundle/Test'); |
| 167 | + $listener = new GeocoderListener($geocoder, $driver); |
| 168 | + |
| 169 | + $this->em->getEventManager()->addEventSubscriber($listener); |
| 170 | + |
| 171 | + $dummy = new DummyWithProperty(); |
| 172 | + $dummy->address = 'Frankfurt, Germany'; |
| 173 | + |
| 174 | + $this->em->persist($dummy); |
| 175 | + $this->em->flush(); |
| 176 | + |
| 177 | + $dummy->latitude = 0; |
| 178 | + $dummy->longitude = 0; |
| 179 | + |
| 180 | + $this->em->flush(); |
| 181 | + |
| 182 | + $this->assertSame('Frankfurt, Germany', $dummy->address); |
| 183 | + $this->assertSame(0, $dummy->latitude); |
| 184 | + $this->assertSame(0, $dummy->longitude); |
| 185 | + $this->assertCount(1, $client->getResponses()); |
| 186 | + } |
151 | 187 | } |
152 | 188 |
|
153 | 189 | /** |
@@ -337,3 +373,18 @@ class DummyWithEmptyProperty |
337 | 373 | */ |
338 | 374 | public $address; |
339 | 375 | } |
| 376 | + |
| 377 | +class TrackedCurlClient extends Client |
| 378 | +{ |
| 379 | + private $responses = []; |
| 380 | + |
| 381 | + public function sendRequest(RequestInterface $request): ResponseInterface |
| 382 | + { |
| 383 | + return $this->responses[] = parent::sendRequest($request); |
| 384 | + } |
| 385 | + |
| 386 | + public function getResponses(): array |
| 387 | + { |
| 388 | + return $this->responses; |
| 389 | + } |
| 390 | +} |
0 commit comments