-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?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']) | ||
->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']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters