Created
January 12, 2025 13:12
-
-
Save arif98741/bd38ed41ee9d82d5ac1bbfd9945aabe6 to your computer and use it in GitHub Desktop.
PostTestCase File
This file contains hidden or 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
<?php | |
namespace Tests\Unit; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
use Tests\TestCase; | |
use App\Models\Post; | |
class PostTest extends TestCase | |
{ | |
use RefreshDatabase; | |
public function test_store_validation_error() | |
{ | |
$response = $this->post(route('posts.store'), [ | |
// 'title' => '', // Empty title | |
// 'body' => '', // Empty body | |
]); | |
// Assert that the response has validation errors for both fields | |
$response->assertSessionHasErrors(['title', 'body']); | |
// Assert that the specific validation error message for 'title' is present | |
$response->assertSessionHasErrors(['title' => 'The title field is required.']); | |
// Assert that the specific validation error message for 'body' is present | |
$response->assertSessionHasErrors(['body' => 'The body field is required.']); | |
} | |
/** | |
* Test that a post is successfully created with valid data. | |
* | |
* @return void | |
*/ | |
public function test_store_success() | |
{ | |
// Valid post data to simulate a successful store operation | |
$data = [ | |
'title' => 'A Valid Post Title', // Valid title | |
'body' => 'This is a valid post body content.', // Valid body | |
]; | |
// Send a POST request to store the post | |
$response = $this->post(route('posts.store'), $data); | |
// Assert that the post was created in the database | |
$this->assertDatabaseHas('posts', [ | |
'title' => 'A Valid Post Title', // Check if the post title exists in the database | |
'body' => 'This is a valid post body content.', // Check if the post body exists in the database | |
]); | |
// Assert that the response redirects to the posts index page (or the appropriate route) | |
$response->assertRedirect(route('posts.index')); | |
} | |
/** | |
* Test that a post is successfully deleted. | |
* | |
* @return void | |
*/ | |
public function test_delete_success() | |
{ | |
// Create a post in the database to delete later | |
$post = Post::factory()->create([ | |
'title' => 'Post to be deleted', | |
'body' => 'This post will be deleted in the test.' | |
]); | |
// Send a DELETE request to the destroy route | |
$response = $this->delete(route('posts.destroy', $post->id)); | |
// Assert that the post was deleted from the database | |
$this->assertDatabaseMissing('posts', [ | |
'id' => $post->id, // Check if the post with the given ID is no longer in the database | |
]); | |
// Assert that the response redirects to the posts index page after deletion | |
$response->assertRedirect(route('posts.index')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment