Finish re-working tests to run on test database

This commit is contained in:
Jonny Barnes 2021-08-31 12:28:00 +01:00
parent 09fc211623
commit 1abca77bdc
50 changed files with 535 additions and 265 deletions

View file

@ -4,55 +4,74 @@ declare(strict_types=1);
namespace Tests\Unit;
use App\Models\Note;
use App\Models\Place;
use App\Services\PlaceService;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use Tests\TestCase;
class PlacesTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
/** @test */
public function canRetrieveAssociatedNotes(): void
{
$place = Place::find(1);
$place = Place::factory()->create();
Note::factory(5)->create([
'place_id' => $place->id,
]);
$this->assertInstanceOf(Collection::class, $place->notes);
$this->assertCount(5, $place->notes);
}
/** @test */
public function nearMethodReturnsCollection(): void
{
$nearby = Place::near((object) ['latitude' => 53.5, 'longitude' => -2.38], 1000)->get();
Place::factory()->create([
'name' => 'The Bridgewater Pub',
'latitude' => 53.4983,
'longitude' => -2.3805,
]);
$nearby = Place::near((object) ['latitude' => 53.5, 'longitude' => -2.38])->get();
$this->assertEquals('the-bridgewater-pub', $nearby[0]->slug);
}
/** @test */
public function getLongurl(): void
{
$place = Place::find(1);
$place = Place::factory()->create([
'name' => 'The Bridgewater Pub',
]);
$this->assertEquals(config('app.url') . '/places/the-bridgewater-pub', $place->longurl);
}
/** @test */
public function getShorturl()
{
$place = Place::find(1);
$place = Place::factory()->create([
'name' => 'The Bridgewater Pub',
]);
$this->assertEquals(config('app.shorturl') . '/places/the-bridgewater-pub', $place->shorturl);
}
/** @test */
public function getUri(): void
{
$place = Place::find(1);
$place = Place::factory()->create([
'name' => 'The Bridgewater Pub',
]);
$this->assertEquals(config('app.url') . '/places/the-bridgewater-pub', $place->uri);
}
/** @test */
public function placeServiceReturnsExistingPlaceBasedOnExternalUrlsSearch(): void
{
Place::factory(10)->create();
$place = new Place();
$place->name = 'Temp Place';
$place->latitude = 37.422009;
@ -65,8 +84,8 @@ class PlacesTest extends TestCase
'url' => ['https://www.openstreetmap.org/way/1234'],
]
]);
$this->assertInstanceOf('App\Models\Place', $ret); // a place was returned
$this->assertCount(12, Place::all()); // still 12 places
$this->assertInstanceOf('App\Models\Place', $ret);
$this->assertCount(11, Place::all());
}
/** @test */
@ -90,10 +109,23 @@ class PlacesTest extends TestCase
}
/** @test */
public function placeServcieCanupdateExternalUrls(): void
public function placeServiceCanUpdateExternalUrls(): void
{
$place = Place::find(1);
$place = Place::factory()->create([
'name' => 'The Bridgewater Pub',
'latitude' => 53.4983,
'longitude' => -2.3805,
'external_urls' => '',
]);
$place->external_urls = 'https://www.openstreetmap.org/way/987654';
$place->external_urls = 'https://foursquare.com/v/123435/the-bridgewater-pub';
$place->save();
$place->external_urls = 'https://bridgewater.pub';
$this->assertEquals('{"osm":"https:\/\/www.openstreetmap.org\/way\/987654","foursquare":"https:\/\/foursquare.com\/v\/123435\/the-bridgewater-pub","default":"https:\/\/bridgewater.pub"}', $place->external_urls);
$this->assertEquals(json_encode([
'default' => 'https://bridgewater.pub',
'osm' => 'https://www.openstreetmap.org/way/987654',
'foursquare' => 'https://foursquare.com/v/123435/the-bridgewater-pub',
]), $place->external_urls);
}
}