Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

spatie/freek.dev

636 stars
1 code files
View spatie/freek.dev on GitHub

tests/Feature/Controllers/CreateLinkControllerTest.php

Open in GitHub
use App\Http\Controllers\Links\CreateLinkController;
use App\Mail\LinkSubmittedMail;
use App\Models\Link;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
 
class CreateLinkControllerTest extends TestCase
{
private User $user;
 
protected function setUp(): void
{
parent::setUp();
 
$this->user = User::factory()->create();
 
$this->actingAs($this->user);
 
Mail::fake();
}
 
/** @test */
public function it_can_create_a_link()
{
$attributes = [
'title' => 'my title',
'text' => 'my text',
'url' => 'https://freek.dev',
];
 
$this
->post(action([CreateLinkController::class, 'store'], $attributes))
->assertRedirect(route('links.thanks'));
 
$expectedAttributes = array_merge([
'user_id' => $this->user->id,
'status' => Link::STATUS_SUBMITTED,
], $attributes);
 
$this->assertDatabaseHas('links', $expectedAttributes);
 
Mail::assertQueued(LinkSubmittedMail::class);
}
 
/** @test */
public function it_will_not_accept_a_link_that_was_already_submitted()
{
$this->withExceptionHandling();
 
$attributes = [
'title' => 'my title',
'text' => 'my text',
'url' => 'https://freek.dev',
];
 
$this
->post(action([CreateLinkController::class, 'store'], $attributes))
->assertRedirect(route('links.thanks'));
 
$this
->post(action([CreateLinkController::class, 'store'], $attributes))
->assertSessionHasErrors('url');
}
 
/** @test */
public function it_can_create_a_link_without_text()
{
$attributes = [
'title' => 'my title',
'url' => 'https://freek.dev',
];
 
$this
->post(action([CreateLinkController::class, 'store'], $attributes))
->assertRedirect(route('links.thanks'));
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.