php
111 lines · 2 tabs
Carlos Mendez
Jan 2026
2 tabs
<?php
namespace Tests\Feature;
use App\Models\Post;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PostTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_view_posts()
{
$posts = Post::factory()->count(3)->create();
$response = $this->get('/posts');
$response->assertStatus(200);
$response->assertSee($posts[0]->title);
}
public function test_authenticated_user_can_create_post()
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post('/posts', [
'title' => 'Test Post',
'body' => 'This is a test post body that is long enough.',
]);
$response->assertRedirect();
$this->assertDatabaseHas('posts', [
'title' => 'Test Post',
'user_id' => $user->id,
]);
}
public function test_guest_cannot_create_post()
{
$response = $this->post('/posts', [
'title' => 'Test Post',
'body' => 'Test body',
]);
$response->assertRedirect('/login');
}
public function test_post_requires_title()
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post('/posts', [
'body' => 'Test body',
]);
$response->assertSessionHasErrors('title');
}
public function test_user_can_only_delete_own_posts()
{
$user = User::factory()->create();
$otherUser = User::factory()->create();
$post = Post::factory()->create(['user_id' => $otherUser->id]);
$response = $this->actingAs($user)->delete("/posts/{$post->id}");
$response->assertForbidden();
$this->assertDatabaseHas('posts', ['id' => $post->id]);
}
}
<?php
namespace Tests\Unit;
use App\Models\Post;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PostTest extends TestCase
{
use RefreshDatabase;
public function test_post_belongs_to_user()
{
$post = Post::factory()->create();
$this->assertInstanceOf(User::class, $post->author);
}
public function test_post_has_read_time()
{
$post = Post::factory()->create([
'body' => str_repeat('word ', 200), // 200 words
]);
$this->assertEquals(1, $post->readTime()); // 1 minute
}
public function test_published_scope_only_returns_published_posts()
{
Post::factory()->create(['published_at' => now()]);
Post::factory()->create(['published_at' => null]);
$published = Post::published()->get();
$this->assertCount(1, $published);
}
}
2 files · php
Explain with highlit
Laravel's testing suite built on PHPUnit makes writing tests straightforward. Feature tests simulate HTTP requests and assert responses, while unit tests focus on individual methods. I use database transactions or RefreshDatabase to reset the database after each test. Factories generate test data consistently. The actingAs() method authenticates users for protected routes. Assertions like assertStatus(), assertSee(), assertDatabaseHas() verify behavior. I mock external services with facades or the Mock class. Test organization follows the AAA pattern—Arrange, Act, Assert. Running php artisan test executes the suite with pretty output. Comprehensive tests catch regressions and enable confident refactoring.
Related snips
ruby
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
System test: asserting Turbo Stream responses
rails
hotwire
turbo
by codesnips
4 tabs
php
<?php
namespace App\Providers;
use App\Contracts\PaymentGateway;
use App\Services\StripePaymentGateway;
Laravel service container and dependency injection
laravel
dependency-injection
service-container
by Carlos Mendez
2 tabs
rust
use my_crate::add;
#[test]
fn test_public_api() {
assert_eq!(add(3, 4), 7);
}
Integration tests in tests/ directory
rust
testing
integration
by Marcus Chen
1 tab
json
{
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build"
},
Laravel mix/Vite for asset compilation
laravel
vite
assets
by Carlos Mendez
4 tabs
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Laravel database migrations for schema management
laravel
migrations
database
by Carlos Mendez
3 tabs
kotlin
package com.example.myapp.data.repository
import com.example.myapp.data.local.PostDao
import com.example.myapp.data.local.PostEntity
import com.example.myapp.data.remote.ApiService
import com.example.myapp.models.Post
Unit testing with JUnit and MockK
kotlin
android
testing
by Alex Chen
2 tabs
Share this code
Here's the card — post it anywhere.