GeocodableBehavior /tests/GeocodableBehaviorTest.php

Language PHP Lines 427
MD5 Hash 01a209d73e31db1fea7c4a4b7b6b1468
Repository https://github.com/jpetitcolas/GeocodableBehavior.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php

/*
 * This file is part of the Propel package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license    MIT License
 */

/**
 * Tests for GeocodableBehavior class
 *
 * @author William Durand <william.durand1@gmail.com>
 */
class GeocodableBehaviorTest extends \PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        if (!class_exists('GeocodedObject')) {
            $schema = <<<EOF
<database name="bookstore" defaultIdMethod="native">
    <table name="simple_geocoded_object">
        <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
        <behavior name="geocodable" />
    </table>

    <table name="geocoded_object">
        <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
        <column name="name" type="VARCHAR" size="100" primaryString="true" />
        <column name="street" type="VARCHAR" size="100" primaryString="true" />
        <column name="city" type="VARCHAR" size="100" primaryString="true" />
        <column name="country" type="VARCHAR" size="100" primaryString="true" />

        <behavior name="geocodable">
            <!-- IP -->
            <parameter name="geocode_ip" value="true" />
            <!-- Address -->
            <parameter name="geocode_address" value="true" />
            <parameter name="address_columns" value="street, city, country" />
            <!-- Geocoder -->
            <parameter name="geocoder_api_key" value="YOUR_API_KEY" />
        </behavior>
    </table>

    <table name="geocoded_object_no_autoupdate">
        <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
        <column name="name" type="VARCHAR" size="100" primaryString="true" />
        <column name="street" type="VARCHAR" size="100" primaryString="true" />
        <column name="city" type="VARCHAR" size="100" primaryString="true" />
        <column name="country" type="VARCHAR" size="100" primaryString="true" />

        <behavior name="geocodable">
            <parameter name="auto_update" value="false" />
            <!-- IP -->
            <parameter name="geocode_ip" value="true" />
            <!-- Address -->
            <parameter name="geocode_address" value="true" />
            <parameter name="address_columns" value="street, city, country" />
            <!-- Geocoder -->
            <parameter name="geocoder_api_key" value="YOUR_API_KEY" />
        </behavior>
    </table>

    <table name="geocoded_object_key_provider">
        <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
        <column name="name" type="VARCHAR" size="100" primaryString="true" />
        <column name="street" type="VARCHAR" size="100" primaryString="true" />
        <column name="city" type="VARCHAR" size="100" primaryString="true" />
        <column name="country" type="VARCHAR" size="100" primaryString="true" />

        <behavior name="geocodable">
            <!-- IP -->
            <parameter name="geocode_ip" value="true" />
            <!-- Address -->
            <parameter name="geocode_address" value="true" />
            <parameter name="address_columns" value="street, city, country" />
            <!-- Geocoder -->
            <parameter name="geocoder_api_key_provider" value="GeoApikeyProvider" />
        </behavior>
    </table>

    <table name="geocoded_object_key_provider_static">
        <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
        <column name="name" type="VARCHAR" size="100" primaryString="true" />
        <column name="street" type="VARCHAR" size="100" primaryString="true" />
        <column name="city" type="VARCHAR" size="100" primaryString="true" />
        <column name="country" type="VARCHAR" size="100" primaryString="true" />

        <behavior name="geocodable">
            <!-- IP -->
            <parameter name="geocode_ip" value="true" />
            <!-- Address -->
            <parameter name="geocode_address" value="true" />
            <parameter name="address_columns" value="street, city, country" />
            <!-- Geocoder -->
            <parameter name="geocoder_api_key_provider" value="GeoApikeyProvider::getKey()" />
        </behavior>
    </table>

    <table name="geocoded_object_key_provider_method">
        <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
        <column name="name" type="VARCHAR" size="100" primaryString="true" />
        <column name="street" type="VARCHAR" size="100" primaryString="true" />
        <column name="city" type="VARCHAR" size="100" primaryString="true" />
        <column name="country" type="VARCHAR" size="100" primaryString="true" />

        <behavior name="geocodable">
            <!-- IP -->
            <parameter name="geocode_ip" value="true" />
            <!-- Address -->
            <parameter name="geocode_address" value="true" />
            <parameter name="address_columns" value="street, city, country" />
            <!-- Geocoder -->
            <parameter name="geocoder_api_key_provider" value="GeoApikeyProvider->getApiKeyMethod()" />
        </behavior>
    </table>
</database>
EOF;
            $builder = new PropelQuickBuilder();
            $config  = $builder->getConfig();
            $config->setBuildProperty('behavior.geocodable.class', '../src/GeocodableBehavior');
            $builder->setConfig($config);
            $builder->setSchema($schema);

            $con = $builder->build();
            $con->sqliteCreateFunction('ACOS', 'acos', 1);
            $con->sqliteCreateFunction('COS', 'cos', 1);
            $con->sqliteCreateFunction('RADIANS', 'deg2rad', 1);
            $con->sqliteCreateFunction('SIN', 'sin', 1);
        }
    }

    public function testObjectMethods()
    {
        $this->assertTrue(method_exists('GeocodedObject', 'getLatitude'));
        $this->assertTrue(method_exists('GeocodedObject', 'getLongitude'));
        $this->assertTrue(method_exists('GeocodedObject', 'geocode'));
        $this->assertTrue(method_exists('GeocodedObject', 'isGeocoded'));
        $this->assertTrue(method_exists('GeocodedObject', 'isGeocodingNecessary'));
        $this->assertTrue(method_exists('GeocodedObject', 'getDistanceTo'));
        $this->assertTrue(method_exists('GeocodedObject', 'getCoordinates'));
        $this->assertTrue(method_exists('GeocodedObject', 'setCoordinates'));
    }

    public function testQueryMethods()
    {
        $this->assertTrue(method_exists('GeocodedObjectQuery', 'filterByDistanceFrom'));
        $this->assertTrue(method_exists('GeocodedObjectQuery', 'filterNear'));
    }

    public function testPeerConstants()
    {
        $this->assertTrue(defined('GeocodedObjectPeer::KILOMETERS_UNIT'));
        $this->assertTrue(defined('GeocodedObjectPeer::MILES_UNIT'));
        $this->assertTrue(defined('GeocodedObjectPeer::NAUTICAL_MILES_UNIT'));
    }

    public function testGetDistanceToInKilometers()
    {
        $geo1 = new GeocodedObject();
        $geo1->setLatitude(45.795463);
        $geo1->setLongitude(3.163237);

        $geo2 = new GeocodedObject();
        $geo2->setLatitude(45.77722154971201);
        $geo2->setLongitude(3.086986541748047);

        $dist = round($geo1->getDistanceTo($geo2), 2);
        $this->assertEquals(6.25, $dist);
    }

    public function testGetDistanceToInMiles()
    {
        $geo1 = new GeocodedObject();
        $geo1->setLatitude(45.795463);
        $geo1->setLongitude(3.163237);

        $geo2 = new GeocodedObject();
        $geo2->setLatitude(45.77722154971201);
        $geo2->setLongitude(3.086986541748047);

        $dist = round($geo1->getDistanceTo($geo2, GeocodedObjectPeer::MILES_UNIT), 2);
        $this->assertEquals(3.88, $dist);
    }

    public function testGetDistanceToInNauticalMiles()
    {
        $geo1 = new GeocodedObject();
        $geo1->setLatitude(45.795463);
        $geo1->setLongitude(3.163237);

        $geo2 = new GeocodedObject();
        $geo2->setLatitude(45.77722154971201);
        $geo2->setLongitude(3.086986541748047);

        $dist = round($geo1->getDistanceTo($geo2, GeocodedObjectPeer::NAUTICAL_MILES_UNIT), 2);
        $this->assertEquals(3.37, $dist);
    }

    public function testSetCoordinates()
    {
        $geo = new GeocodedObject();
        $geo->setCoordinates(1, 2);

        $this->assertEquals(1, $geo->getLatitude());
        $this->assertEquals(2, $geo->getLongitude());
    }

    public function testGetCoordinates()
    {
        $obj = new GeocodedObject();
        $obj->setCoordinates(1, 2);

        $this->assertEquals(array('latitude' => 1, 'longitude' => 2), $obj->getCoordinates());
    }

    public function testIsGeocoded()
    {
        $obj = new GeocodedObject();
        $this->assertFalse($obj->isGeocoded());

        $obj->setCoordinates(1, 2);
        $this->assertTrue($obj->isGeocoded());
    }

    public function testFilterByDistanceFromReturnsNoObjects()
    {
        GeocodedObjectPeer::doDeleteAll();

        $geo1 = new GeocodedObject();
        $geo1->setName('Aulnat Area');
        $geo1->setCity('Aulnat');
        $geo1->setCountry('France');
        $geo1->save();

        $geo2 = new GeocodedObject();
        $geo2->setName('Lyon Area');
        $geo2->setCity('Lyon');
        $geo2->setCountry('France');
        $geo2->save();

        $objects = GeocodedObjectQuery::create()
            ->filterByDistanceFrom($geo1->getLatitude(), $geo1->getLongitude(), 5)
            ->find()
            ;
        $this->assertEquals(1, count($objects));
    }

    public function testFilterByDistanceFromReturnsObjects()
    {
        GeocodedObjectPeer::doDeleteAll();

        $geo1 = new GeocodedObject();
        $geo1->setName('Aulnat Area');
        $geo1->setCity('Aulnat');
        $geo1->setCountry('France');
        $geo1->save();

        $geo2 = new GeocodedObject();
        $geo2->setName('Lyon Area');
        $geo2->setCity('Lyon');
        $geo2->setCountry('France');
        $geo2->save();

        $geo3 = new GeocodedObject();
        $geo3->setName('Lempdes Area');
        $geo3->setCity('Lempdes');
        $geo3->setCountry('France');
        $geo3->save();

        $objects = GeocodedObjectQuery::create()
            ->filterByDistanceFrom($geo1->getLatitude(), $geo1->getLongitude(), 20)
            ->find()
            ;
        $this->assertEquals(2, count($objects));
    }

    public function testGeocodeIp()
    {
        $geo = new GeocodedObject();
        $geo->setIpAddress('74.200.247.59');
        $geo->save();

        $this->assertEquals(33.036711, $geo->getLatitude());
        $this->assertEquals(-96.813541, $geo->getLongitude());
    }

    public function testGeocodeAddress()
    {
        $geo = new GeocodedObject();
        $geo->setStreet('10 avenue Gambetta');
        $geo->setCity('Paris');
        $geo->setCountry('France');
        $geo->save();

        $this->assertEquals(48.863217, $geo->getLatitude());
        $this->assertEquals(2.388821, $geo->getLongitude());
    }

    public function testGeocodeAddressWithUpdate()
    {
        $geo = new GeocodedObject();
        $geo->setStreet('10 avenue Gambetta');
        $geo->setCity('Paris');
        $geo->setCountry('France');
        $geo->save();

        $this->assertEquals(48.863217, $geo->getLatitude());
        $this->assertEquals(2.388821, $geo->getLongitude());

        $geo->setStreet('1 avenue LĂŠon Maniez');
        $geo->save();

        $this->assertEquals(48.85693, $geo->getLatitude());
        $this->assertEquals(2.3412, $geo->getLongitude());
    }

    public function testGeocodeAddressForceCoordinates()
    {
        $geo = new GeocodedObject();
        $geo->setStreet('10 avenue Gambetta');
        $geo->setCity('Paris');
        $geo->setCountry('France');
        $geo->save();

        $this->assertEquals(48.863217, $geo->getLatitude());
        $this->assertEquals(2.388821, $geo->getLongitude());

        // If we force the values, we should bypass the geocoding process
        $geo->setLatitude(48.85693);
        $geo->setLongitude(2.3412);
        $geo->save();

        $this->assertEquals(48.85693, $geo->getLatitude());
        $this->assertEquals(2.3412, $geo->getLongitude());
    }

    public function testNoFieldUpdate()
    {
        $geo = new GeocodedObject();
        $geo->setStreet('10 avenue Gambetta');
        $geo->setCity('Paris');
        $geo->setCountry('France');
        $geo->setLatitude(48.123456);
        $geo->setLongitude(2.3412);
        $geo->save();

        $this->assertEquals(48.123456, $geo->getLatitude());
        $this->assertEquals(2.3412, $geo->getLongitude());

        // If fields are unchanged, we should bypass the geocoding process
        $geo->setLongitude(2.123456);
        $geo->save();

        $this->assertEquals(48.123456, $geo->getLatitude());
        $this->assertEquals(2.123456, $geo->getLongitude());
    }

    public function testNoAutoUpdate()
    {
        $geo = new GeocodedObjectNoAutoUpdate();
        $geo->setStreet('10 avenue Gambetta');
        $geo->setCity('Paris');
        $geo->setCountry('France');
        $geo->save();
        $this->assertEquals(null, $geo->getLatitude());
        $this->assertEquals(null, $geo->getLongitude());
    }

    public function testKeyProvider()
    {
        $geo = new GeocodedObjectKeyProvider();
        $geo->setStreet('10 avenue Gambetta');
        $geo->setCity('Paris');
        $geo->setCountry('France');
        $geo->save();
        $this->assertEquals(48.863217, $geo->getLatitude());
        $this->assertEquals(2.388821, $geo->getLongitude());
    }

    public function testKeyProviderStatic()
    {
        $geo = new GeocodedObjectKeyProviderStatic();
        $geo->setStreet('10 avenue Gambetta');
        $geo->setCity('Paris');
        $geo->setCountry('France');
        $geo->save();
        $this->assertEquals(48.863217, $geo->getLatitude());
        $this->assertEquals(2.388821, $geo->getLongitude());
    }

    public function testKeyProviderMethod()
    {
        $geo = new GeocodedObjectKeyProviderMethod();
        $geo->setStreet('10 avenue Gambetta');
        $geo->setCity('Paris');
        $geo->setCountry('France');
        $geo->save();
        $this->assertEquals(48.863217, $geo->getLatitude());
        $this->assertEquals(2.388821, $geo->getLongitude());
    }

    public function testGeocode()
    {
        $geo = new GeocodedObject();
        $this->assertNull($geo->geocode(), 'The method returns null as there is nothing to geocode');
        $this->assertFalse($geo->isModified());

        $geo->setCity('Paris');
        $result = $geo->geocode();

        $this->assertTrue($geo->isModified());
        $this->assertInstanceOf('Geocoder\Result\ResultInterface', $result);
        $this->assertEquals('Paris', $result->getCity());
        $this->assertEquals('France', $result->getCountry());
    }

    public function testGeocodeIsEffectLessIfGeocodingDisabled()
    {
        $geo = new SimpleGeocodedObject();

        $geo->geocode();
        $this->assertFalse($geo->isModified());
    }
}
Back to Top