Creating unit tests for a CodeIgniter4 CRUD application Emman, October 2, 2023October 2, 2023 Let’s quickly walk through creating unit tests for a CRUD (Create, Read, Update, Delete) application using CodeIgniter4. We’ll use a simple example of a “tasks” application where you can manage tasks. In this example, we’ll create unit tests for the following actions: Creating a new task Retrieving a task Updating a task Deleting a task Assuming you have already set up a CodeIgniter 4 project and created the necessary controllers, models, and views for the CRUD operations, let’s proceed to create unit tests. Creating a New Task (Create) Test: Create a new file CreateTaskTest.php in your tests directory: namespace Tests\Feature; use CodeIgniter\Test\FeatureTestCase; class CreateTaskTest extends FeatureTestCase { public function testCreateTask() { $data = [ 'title' => 'Sample Task', 'description' => 'This is a sample task', ]; $result = $this->post('/tasks/create', $data); $result->assertStatus(200); $result->assertSee('Task created successfully'); // You can also add more assertions to check the data in the database } } Retrieving a Task (Read) Test: Create a new file RetrieveTaskTest.php in your tests directory: namespace Tests\Feature; use CodeIgniter\Test\FeatureTestCase; class RetrieveTaskTest extends FeatureTestCase { public function testRetrieveTask() { $result = $this->get('/tasks/1'); // Assuming the task with ID 1 exists $result->assertStatus(200); $result->assertSee('Sample Task'); $result->assertSee('This is a sample task'); } } Updating a Task (Update) Test: Create a new file UpdateTaskTest.php in your tests directory: namespace Tests\Feature; use CodeIgniter\Test\FeatureTestCase; class UpdateTaskTest extends FeatureTestCase { public function testUpdateTask() { $data = [ 'title' => 'Updated Task Title', 'description' => 'This task has been updated', ]; $result = $this->put('/tasks/update/1', $data); // Assuming the task with ID 1 exists $result->assertStatus(200); $result->assertSee('Task updated successfully'); // You can also add more assertions to check the updated data in the database } } Deleting a Task (Delete) Test: Create a new file DeleteTaskTest.php in your tests directory: namespace Tests\Feature; use CodeIgniter\Test\FeatureTestCase; class DeleteTaskTest extends FeatureTestCase { public function testDeleteTask() { $result = $this->delete('/tasks/delete/1'); // Assuming the task with ID 1 exists $result->assertStatus(200); $result->assertSee('Task deleted successfully'); // You can also add assertions to check that the task is no longer in the database } } Remember to adjust the URLs and data based on your actual application routes and database structure. To run the tests, navigate to your project directory and execute: php spark test This will execute all the tests in your tests directory. Please note that this is a basic example of unit testing in CodeIgniter 4. In a real-world application, you might need to mock dependencies, set up test databases, and perform more advanced testing scenarios. Share this:FacebookX Related Discover more from Code Concepts Snippets Subscribe to get the latest posts sent to your email. Type your email… Subscribe General