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

@ -5,12 +5,13 @@ declare(strict_types=1);
namespace Tests\Unit;
use App\Models\Article;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
class ArticlesTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
/** @test */
public function titleSlugIsGeneratedAutomatically(): void
@ -64,6 +65,12 @@ class ArticlesTest extends TestCase
/** @test */
public function dateScopeReturnsExpectedArticles(): void
{
Article::factory()->create([
'created_at' => Carbon::now()->subYear()->toDateTimeString(),
'updated_at' => Carbon::now()->subYear()->toDateTimeString(),
]);
Article::factory()->create();
$yearAndMonth = Article::date(date('Y'), date('m'))->get();
$this->assertTrue(count($yearAndMonth) === 1);

View file

@ -19,7 +19,7 @@ class ProcessBookmarkJobTest extends TestCase
/** @test */
public function screenshotAndArchiveLinkAreSavedByJob(): void
{
$bookmark = Bookmark::find(1);
$bookmark = Bookmark::factory()->create();
$uuid = Uuid::uuid4();
$service = $this->createMock(BookmarkService::class);
$service->method('saveScreenshot')
@ -40,7 +40,7 @@ class ProcessBookmarkJobTest extends TestCase
/** @test */
public function archiveLinkSavedAsNullWhenExceptionThrown(): void
{
$bookmark = Bookmark::find(1);
$bookmark = Bookmark::factory()->create();
$uuid = Uuid::uuid4();
$service = $this->createMock(BookmarkService::class);
$service->method('saveScreenshot')

View file

@ -44,7 +44,7 @@ class ProcessWebMentionJobTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(1);
$note = Note::factory()->create();
$source = 'https://example.org/mention/1/';
$job = new ProcessWebMention($note, $source);
@ -70,7 +70,7 @@ class ProcessWebMentionJobTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(1);
$note = Note::factory()->create();
$source = 'https://example.org/mention/1/';
$job = new ProcessWebMention($note, $source);
@ -103,7 +103,7 @@ class ProcessWebMentionJobTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(14);
$note = Note::factory()->create();
$source = 'https://aaronpk.localhost/reply/1';
$job = new ProcessWebMention($note, $source);
@ -135,7 +135,7 @@ class ProcessWebMentionJobTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(14);
$note = Note::factory()->create();
$source = 'https://example.org/reply/1';
$webmention = new WebMention();
$webmention->source = $source;
@ -172,7 +172,7 @@ class ProcessWebMentionJobTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(14);
$note = Note::factory()->create();
$source = 'https://example.org/reply/1';
$webmention = new WebMention();
$webmention->source = $source;
@ -209,7 +209,7 @@ class ProcessWebMentionJobTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(14);
$note = Note::factory()->create();
$source = 'https://example.org/reply/1';
$webmention = new WebMention();
$webmention->source = $source;

View file

@ -10,18 +10,20 @@ use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SyndicateBookmarkToTwitterJobTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
/** @test */
public function weSendBookmarksToTwitter(): void
{
$faker = \Faker\Factory::create();
$randomNumber = $faker->randomNumber();
$json = json_encode([
'url' => 'https://twitter.com/123'
'url' => 'https://twitter.com/' . $randomNumber
]);
$mock = new MockHandler([
new Response(201, ['Content-Type' => 'application/json'], $json),
@ -29,13 +31,12 @@ class SyndicateBookmarkToTwitterJobTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$bookmark = Bookmark::find(1);
$bookmark = Bookmark::factory()->create();
$job = new SyndicateBookmarkToTwitter($bookmark);
$job->handle($client);
$this->assertDatabaseHas('bookmarks', [
'id' => 1,
'syndicates' => '{"twitter": "https://twitter.com/123"}',
'syndicates' => '{"twitter": "https://twitter.com/' . $randomNumber . '"}',
]);
}
}

View file

@ -8,18 +8,20 @@ use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SyndicateNoteToTwitterJobTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
/** @test */
public function weSyndicateNotesToTwitter(): void
{
$faker = \Faker\Factory::create();
$randomNumber = $faker->randomNumber();
$json = json_encode([
'url' => 'https://twitter.com/i/web/status/123'
'url' => 'https://twitter.com/i/web/status/' . $randomNumber,
]);
$mock = new MockHandler([
new Response(201, ['Content-Type' => 'application/json'], $json),
@ -27,13 +29,12 @@ class SyndicateNoteToTwitterJobTest extends TestCase
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(1);
$note = Note::factory()->create();
$job = new SyndicateNoteToTwitter($note);
$job->handle($client);
$this->assertDatabaseHas('notes', [
'id' => 1,
'tweet_id' => '123',
'tweet_id' => $randomNumber,
]);
}
}

View file

@ -5,16 +5,20 @@ declare(strict_types=1);
namespace Tests\Unit;
use App\Models\Media;
use App\Models\Note;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class MediaTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function getTheNoteThatMediaInstanceBelongsTo(): void
{
$media = Media::find(1);
$note = $media->note;
$this->assertInstanceOf('App\Models\Note', $note);
$media = Media::factory()->for(Note::factory())->create();
$this->assertInstanceOf(Note::class, $media->note);
}
/** @test */

View file

@ -6,14 +6,18 @@ namespace Tests\Unit;
use App\Models\MicropubClient;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class MicropubClientsTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function weCanGetNotesRelatingToClient(): void
{
$client = MicropubClient::find(1);
$client = MicropubClient::factory()->make();
$this->assertInstanceOf(Collection::class, $client->notes);
}
}

View file

@ -4,18 +4,18 @@ declare(strict_types=1);
namespace Tests\Unit;
use App\Models\{Contact, Media, Note, Place, Tag};
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use App\Models\{Media, Note, Tag};
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class NotesTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;
/**
* Test the getNoteAttribute method. This will then also call the
@ -28,7 +28,9 @@ class NotesTest extends TestCase
{
// phpcs:ignore
$expected = '<p>Having a <a rel="tag" class="p-category" href="/notes/tagged/beer">#beer</a> at the local. 🍺</p>' . PHP_EOL;
$note = Note::find(2);
$note = Note::factory([
'note' => 'Having a #beer at the local. 🍺',
])->create();
$this->assertEquals($expected, $note->note);
}
@ -42,7 +44,16 @@ class NotesTest extends TestCase
{
// phpcs:ignore
$expected = '<p>Hi <span class="u-category h-card mini-h-card"><a class="u-url p-name" href="http://tantek.com">Tantek Çelik</a><span class="hovercard"> <a class="u-url" href="https://twitter.com/t"><img class="social-icon" src="/assets/img/social-icons/twitter.svg"> t</a><img class="u-photo" alt="" src="/assets/profile-images/default-image"></span></span></p>' . PHP_EOL;
$note = Note::find(4);
Contact::factory()->create([
'nick' => 'tantek',
'name' => 'Tantek Çelik',
'homepage' => 'http://tantek.com',
'twitter' => 't',
'facebook' => null,
]);
$note = Note::factory()->create([
'note' => 'Hi @tantek',
]);
$this->assertEquals($expected, $note->note);
}
@ -56,7 +67,16 @@ class NotesTest extends TestCase
{
// phpcs:ignore
$expected = '<p>Hi <span class="u-category h-card mini-h-card"><a class="u-url p-name" href="https://aaronparecki.com">Aaron Parecki</a><span class="hovercard"><a class="u-url" href="https://www.facebook.com/123456"><img class="social-icon" src="/assets/img/social-icons/facebook.svg"> Facebook</a> <img class="u-photo" alt="" src="/assets/profile-images/aaronparecki.com/image"></span></span></p>' . PHP_EOL;
$note = Note::find(5);
Contact::factory()->create([
'nick' => 'aaron',
'name' => 'Aaron Parecki',
'homepage' => 'https://aaronparecki.com',
'twitter' => null,
'facebook' => 123456,
]);
$note = Note::factory()->create([
'note' => 'Hi @aaron',
]);
$this->assertEquals($expected, $note->note);
}
@ -69,13 +89,16 @@ class NotesTest extends TestCase
public function twitterLinkIsCreatedWhenNoContactFound(): void
{
$expected = '<p>Hi <a href="https://twitter.com/bob">@bob</a></p>' . PHP_EOL;
$note = Note::find(6);
$note = Note::factory()->create([
'note' => 'Hi @bob',
]);
$this->assertEquals($expected, $note->note);
}
/** @test */
public function shorturlMethodReturnsExpectedValue(): void
{
Note::factory(14)->create();
$note = Note::find(14);
$this->assertEquals(config('app.shorturl') . '/notes/E', $note->shorturl);
}
@ -83,7 +106,13 @@ class NotesTest extends TestCase
/** @test */
public function weGetLatitudeLongitudeValuesOfAssociatedPlaceOfNote(): void
{
$note = Note::find(2); // should be having beer at bridgewater note
$place = Place::factory()->create([
'latitude' => '53.4983',
'longitude' => '-2.3805',
]);
$note = Note::factory()->create([
'place_id' => $place->id,
]);
$this->assertEquals('53.4983', $note->latitude);
$this->assertEquals('-2.3805', $note->longitude);
}
@ -91,7 +120,7 @@ class NotesTest extends TestCase
/** @test */
public function whenNoAssociatedPlaceWeGetNullForLatitudeLongitudeValues(): void
{
$note = Note::find(5);
$note = Note::factory()->create();
$this->assertNull($note->latitude);
$this->assertNull($note->longitude);
}
@ -99,7 +128,14 @@ class NotesTest extends TestCase
/** @test */
public function weCanGetAddressAttributeForAssociatedPlace(): void
{
$note = Note::find(2);
$place = Place::factory()->create([
'name' => 'The Bridgewater Pub',
'latitude' => '53.4983',
'longitude' => '-2.3805',
]);
$note = Note::factory()->create([
'place_id' => $place->id,
]);
$this->assertEquals('The Bridgewater Pub', $note->address);
}
@ -254,13 +290,13 @@ class NotesTest extends TestCase
/** @test */
public function addImageElementToNoteContentWhenMediaAssociated(): void
{
$media = new Media([
$media = Media::factory()->create([
'type' => 'image',
'path' => 'test.png',
]);
$media->save();
$note = new Note(['note' => 'A nice image']);
$note->save();
$note = Note::factory()->create([
'note' => 'A nice image',
]);
$note->media()->save($media);
$expected = "<p>A nice image</p>
@ -271,13 +307,13 @@ class NotesTest extends TestCase
/** @test */
public function addVideoElementToNoteContentWhenMediaAssociated(): void
{
$media = new Media([
$media = Media::factory()->create([
'type' => 'video',
'path' => 'test.mkv',
]);
$media->save();
$note = new Note(['note' => 'A nice video']);
$note->save();
$note = Note::factory()->create([
'note' => 'A nice video',
]);
$note->media()->save($media);
$expected = "<p>A nice video</p>
@ -288,13 +324,13 @@ class NotesTest extends TestCase
/** @test */
public function addAudioElementToNoteContentWhenMediaAssociated(): void
{
$media = new Media([
$media = Media::factory()->create([
'type' => 'audio',
'path' => 'test.flac',
]);
$media->save();
$note = new Note(['note' => 'Some nice audio']);
$note->save();
$note = Note::factory()->create([
'note' => 'Some nice audio',
]);
$note->media()->save($media);
$expected = "<p>Some nice audio</p>
@ -326,7 +362,7 @@ class NotesTest extends TestCase
/** @test */
public function markdownContentGetsConverted(): void
{
$note = Note::create([
$note = Note::factory()->create([
'note' => 'The best search engine? https://duckduckgo.com',
]);
@ -348,24 +384,21 @@ class NotesTest extends TestCase
];
Cache::put('933662564587855877', $tempContent);
$note = Note::find(1);
$note = Note::factory()->create([
'in_reply_to' => 'https://twitter.com/someRando/status/933662564587855877'
]);
$this->assertSame($tempContent, $note->twitter);
}
/** @test */
public function latitudeCanBeParsedFromPlainLocation(): void
public function latitudeAndLongitudeCanBeParsedFromPlainLocation(): void
{
$note = Note::find(18);
$note = Note::factory()->create([
'location' => '1.23,4.56',
]);
$this->assertSame(1.23, $note->latitude);
}
/** @test */
public function longitudeCanBeParsedFromPlainLocation(): void
{
$note = Note::find(18);
$this->assertSame(4.56, $note->longitude);
}
@ -374,7 +407,9 @@ class NotesTest extends TestCase
{
Cache::put('1.23,4.56', '<span class="p-country-name">Antarctica</span>');
$note = Note::find(18);
$note = Note::factory()->create([
'location' => '1.23,4.56',
]);
$this->assertSame('<span class="p-country-name">Antarctica</span>', $note->address);
}

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);
}
}

View file

@ -4,22 +4,31 @@ declare(strict_types=1);
namespace Tests\Unit;
use App\Models\Bookmark;
use App\Models\Note;
use App\Models\Tag;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class TagsTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function canGetAssociatedNotes(): void
{
$tag = Tag::find(1); // should be beer tag
$note = Note::factory()->create();
$tag = Tag::factory()->create();
$note->tags()->save($tag);
$this->assertCount(1, $tag->notes);
}
/** @test */
public function canGetAssociatedBookmarks(): void
{
$tag = Tag::find(5); //should be first random tag for bookmarks
$bookmark = Bookmark::factory()->create();
$tag = Tag::factory()->create();
$bookmark->tags()->save($tag);
$this->assertCount(1, $tag->bookmarks);
}

View file

@ -4,19 +4,27 @@ declare(strict_types=1);
namespace Tests\Unit;
use App\Models\Note;
use App\Models\WebMention;
use Codebird\Codebird;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
class WebMentionTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function commentableMethodLinksToNotes(): void
{
$webmention = WebMention::find(1);
$this->assertInstanceOf('App\Models\Note', $webmention->commentable);
$note = Note::factory()->create();
$webmention = WebMention::factory()->make([
'commentable_id' => $note->id,
'commentable_type' => Note::class,
]);
$this->assertInstanceOf(Note::class, $webmention->commentable);
}
/** @test */