1
0
mirror of https://github.com/Kovah/LinkAce.git synced 2025-04-20 23:11:56 +02:00

Add missing test for the fetch controller

This commit is contained in:
Kovah 2020-10-21 11:41:12 +02:00
parent 1a9312b7e1
commit c8696dcb8d
No known key found for this signature in database
GPG Key ID: AAAA031BA9830D7B

View File

@ -0,0 +1,118 @@
<?php
namespace Tests\Controller;
use App\Models\Link;
use App\Models\LinkList;
use App\Models\Tag;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class FetchControllerTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$user = factory(User::class)->create();
$this->actingAs($user);
}
public function testTagsQuery(): void
{
factory(Tag::class)->create(['name' => 'testTag']); // must be found
factory(Tag::class)->create(['name' => 'test*Tag']); // must be found
factory(Tag::class)->create(['name' => 'blablaTag']); // must not be found
$response = $this->post('fetch/tags', [
'query' => 'test',
]);
$response
->assertOk()
->assertJson([
['text' => 'test*Tag'],
['text' => 'testTag'],
])
->assertJsonMissing(['text' => 'blablaTag']);
}
public function testListsQuery(): void
{
factory(LinkList::class)->create(['name' => 'testList']); // must be found
factory(LinkList::class)->create(['name' => 'test*List']); // must be found
factory(LinkList::class)->create(['name' => 'blablaList']); // must not be found
$response = $this->post('fetch/lists', [
'query' => 'test',
]);
$response
->assertOk()
->assertJson([
['text' => 'test*List'],
['text' => 'testList'],
])
->assertJsonMissing(['text' => 'blablaList']);
}
public function testExistingUrlSearch(): void
{
factory(Link::class)->create(['url' => 'https://duckduckgo.com']);
$response = $this->post('fetch/existing-links', [
'query' => 'https://duckduckgo.com',
]);
$response->assertOk()->assertJson(['linkFound' => true]);
}
public function testExistingUrlSearchWithoutResult(): void
{
factory(Link::class)->create(['url' => 'https://duckduckgo.com']);
$response = $this->post('fetch/existing-links', [
'query' => 'https://microsoft.com',
]);
$response->assertOk()->assertJson(['linkFound' => false]);
}
public function testGetHtmlForUrl(): void
{
$testHtml = '<!DOCTYPE html><head>' .
'<title>Example Title</title>' .
'<meta name="description" content="This an example description">' .
'</head></html>';
Http::fake([
'example.com' => Http::response($testHtml, 200),
]);
$response = $this->post('fetch/html-for-url', [
'url' => 'https://example.com',
]);
$response->assertOk();
$responseHtml = $response->content();
$this->assertEquals($testHtml, $responseHtml);
}
public function testGetHtmlForUrlWithFailure(): void
{
Http::fake([
'example.com' => Http::response('', 500),
]);
$response = $this->post('fetch/html-for-url', [
'url' => 'https://example.com',
]);
$response->assertOk()->assertSee('');
}
}