Skip to content

Commit

Permalink
[WIP] Adds TagsController tests
Browse files Browse the repository at this point in the history
  • Loading branch information
syropian committed Mar 10, 2024
1 parent 31f3aa8 commit ad37e2d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 12 deletions.
12 changes: 1 addition & 11 deletions app/Http/Controllers/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@

class TagsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return auth()->user()->tags;
}

/**
* Store a newly created resource in storage.
*
Expand All @@ -29,7 +19,7 @@ public function store(Request $request)
{
if (auth()->user()->cannot('create', Tag::class)) {
return redirect()->back()->withErrors([
'sponsorship_required' => [Ability::CREATE_TAG],
'sponsorship_required' => [Ability::CREATE_TAG->value],
]);
}

Expand Down
66 changes: 66 additions & 0 deletions tests/Feature/Controllers/TagsController/StoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

use App\Models\Tag;
use App\Providers\RouteServiceProvider;

it('redirects guests to the login page')
->post('/tags')
->assertRedirect('/login');

it('creates a new tag', function () {
$this->login();

$this->assertDatabaseMissing('tags', ['name' => 'Laravel']);

$this
->post(route('tags.store'), ['name' => 'Laravel'])
->assertRedirect(RouteServiceProvider::HOME)
->assertSessionHas('success', "The 'Laravel' tag was added");

$this->assertDatabaseHas('tags', ['name' => 'Laravel']);
});

it('requires a valid name', function (array $badData, array|string $errors) {
$this
->login()
->post(route('tags.store'), [...$badData])
->assertInvalid($errors);
})->with([
[['name' => null], 'name'],
[['name' => true], 'name'],
[['name' => 12], 'name'],
[[], 'name'],
]);

it('requires a unique name per user', function () {
Tag::factory()->create(['name' => 'VueJS']);

$this
->login()
->post(route('tags.store'), ['name' => 'VueJS'])
->assertValid()
->assertRedirect(RouteServiceProvider::HOME);

expect(auth()->user()->tags()->count())->toBe(1);

$this
->post(route('tags.store'), ['name' => 'VueJS'])
->assertInvalid(['name' => 'You already have a tag with that name.']);

expect(auth()->user()->tags()->count())->toBe(1);
});

it('flashes an error if the user is not a sponsor and is at their tag limit', function () {
$this->login();

Tag::factory()->count(5)->create(['user_id' => auth()->id()]);

$this
->post(route('tags.store'), ['name' => 'VueJS'])
->assertRedirect(RouteServiceProvider::HOME)
->assertSessionHasErrors(['sponsorship_required' => 'create_tag']);

$this->assertDatabaseMissing('tags', ['user_id' => auth()->id(), 'name' => 'VueJS']);
});
56 changes: 56 additions & 0 deletions tests/Feature/Controllers/TagsController/UpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

use App\Models\Tag;
use App\Providers\RouteServiceProvider;

it('redirects guests to the login page')
->put('/tags/1', ['name' => 'TypeScript'])
->assertRedirect('/login');

it('updates a tag', function () {
$this->login();

$tag = Tag::factory()->create(['name' => 'SwypeScript', 'user_id' => auth()->id()]);

$this
->put(route('tags.update', $tag), ['name' => 'TypeScript'])
->assertRedirect(route('dashboard.show'));

$this->assertDatabaseHas('tags', ['id' => $tag->id, 'name' => 'TypeScript']);
});

it('requires a valid name', function (array $badData, array|string $errors) {
$this->login();

$tag = Tag::factory()->create(['user_id' => auth()->id()]);

$this
->put(route('tags.update', $tag), [...$badData])
->assertInvalid($errors);
})->with([
[['name' => null], 'name'],
[['name' => true], 'name'],
[['name' => 12], 'name'],
[[], 'name'],
]);

it('requires a unique name per user', function () {
// Create a tag called Laravel by someone else
Tag::factory()->create(['name' => 'Laravel']);

$this->login();

$tag = Tag::factory()->create(['name' => 'Laaravell', 'user_id' => auth()->id()]);
$tagB = Tag::factory()->create(['user_id' => auth()->id()]);

$this
->put(route('tags.update', $tag), ['name' => 'Laravel'])
->assertValid()
->assertRedirect(RouteServiceProvider::HOME);

$this
->put(route('tags.update', $tagB), ['name' => 'Laravel'])
->assertInvalid(['name' => 'You already have a tag with that name.']);
});
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests;

use App\Models\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Http;

Expand All @@ -23,7 +24,8 @@ protected function setUp(): void

protected function login(User $user = null)
{
$user ??= User::factory()->create()->first();
/** @var Authenticatable $user * */
$user ??= User::factory()->create();

$this->actingAs($user);

Expand Down

0 comments on commit ad37e2d

Please sign in to comment.