diff --git a/tests/Controller/API/ListLinksTest.php b/tests/Controller/API/ListLinksTest.php index 0936fcee..f86c6db7 100644 --- a/tests/Controller/API/ListLinksTest.php +++ b/tests/Controller/API/ListLinksTest.php @@ -5,26 +5,51 @@ namespace Tests\Controller\API; use App\Models\Link; use App\Models\LinkList; use Illuminate\Foundation\Testing\RefreshDatabase; +use Tests\Controller\Traits\PreparesTestData; class ListLinksTest extends ApiTestCase { + use PreparesTestData; use RefreshDatabase; public function testLinksRequest(): void { - $link = Link::factory()->create(); - $list = LinkList::factory()->create(); + $this->createTestLists(); + [$link, $link2, $link3] = $this->createTestLinks(); + $link->lists()->sync([1, 2]); + $link2->lists()->sync([1, 2]); + $link3->lists()->sync([1, 2]); - $link->lists()->sync([$list->id]); - - $response = $this->getJsonAuthorized('api/v1/lists/1/links'); - - $response->assertOk() + $this->getJsonAuthorized('api/v1/lists/1/links') + ->assertOk() ->assertJson([ 'data' => [ ['url' => $link->url], + ['url' => $link2->url], + ], + ]) + ->assertJsonMissing([ + 'data' => [ + ['url' => $link3->url], ], ]); + + $this->getJsonAuthorized('api/v1/lists/2/links') + ->assertOk() + ->assertJson([ + 'data' => [ + ['url' => $link->url], + ['url' => $link2->url], + ], + ]) + ->assertJsonMissing([ + 'data' => [ + ['url' => $link3->url], + ], + ]); + + $this->getJsonAuthorized('api/v1/lists/3/links') + ->assertForbidden(); } public function testLinksRequestWithoutLinks(): void @@ -37,16 +62,10 @@ class ListLinksTest extends ApiTestCase ->assertJson([ 'data' => [], ]); - - $responseBody = json_decode($response->content()); - - $this->assertEmpty($responseBody->data); } public function testShowRequestNotFound(): void { - $response = $this->getJsonAuthorized('api/v1/lists/1/links'); - - $response->assertNotFound(); + $this->getJsonAuthorized('api/v1/lists/1/links')->assertNotFound(); } } diff --git a/tests/Controller/App/SearchControllerTest.php b/tests/Controller/App/SearchControllerTest.php index 55c2ea5c..ca7aa85b 100644 --- a/tests/Controller/App/SearchControllerTest.php +++ b/tests/Controller/App/SearchControllerTest.php @@ -13,7 +13,7 @@ class SearchControllerTest extends TestCase { use RefreshDatabase; - private $user; + private User $user; protected function setUp(): void { @@ -27,23 +27,35 @@ class SearchControllerTest extends TestCase public function testValidSearchResponse(): void { - $response = $this->get('search'); - - $response->assertOk() + $this->get('search') + ->assertOk() ->assertSee('Search'); } public function testValidSearchResult(): void { - $response = $this->post('search', [ + $this->post('search', [ 'query' => 'example', - ]); - - $response->assertOk() + ]) + ->assertOk() ->assertSee('https://example.com') ->assertDontSee('https://test.com'); } + public function testValidSearchWithOrdering(): void + { + $response = $this->post('search', [ + 'query' => 'example', + 'order_by' => 'title:asc', + ]); + + $body = $response->content(); + $posLink1 = strpos($body, 'https://empty-test.com'); + $posLink2 = strpos($body, 'https://example.com'); + + $this->assertTrue($posLink1 < $posLink2); + } + public function testValidUrlSearchResult(): void { $response = $this->post('search', [ diff --git a/tests/Controller/Models/LinkControllerTest.php b/tests/Controller/Models/LinkControllerTest.php index 24f2e6eb..c2802899 100644 --- a/tests/Controller/Models/LinkControllerTest.php +++ b/tests/Controller/Models/LinkControllerTest.php @@ -242,9 +242,9 @@ class LinkControllerTest extends TestCase { $this->createTestLinks(); - $this->get('links/1')->assertOk()->assertSee('https://public-link.com'); - $this->get('links/2')->assertOk()->assertSee('https://internal-link.com'); - $this->get('links/3')->assertForbidden(); + $this->get('links/1/edit')->assertOk()->assertSee('https://public-link.com'); + $this->get('links/2/edit')->assertOk()->assertSee('https://internal-link.com'); + $this->get('links/3/edit')->assertForbidden(); } public function testUpdateResponse(): void