Backend Testing Phpunit

Unit testing, test structure, mocking.

valec3 Updated

File contents

Backend Testing PHPUnit

When to use this skill

  • Writing tests
  • TDD development
  • CI/CD pipelines
  • Code quality

Workflow

  • Write tests for critical logic
  • Test edge cases
  • Mock dependencies
  • Run before commits
  • Maintain tests

Instructions

Test Class

<?php

namespace Tests\Unit;

use CodeIgniter\Test\CIUnitTestCase;

class UserServiceTest extends CIUnitTestCase
{
    protected $service;

    protected function setUp(): void
    {
        parent::setUp();
        $this->service = new UserService();
    }

    public function testCreateUserWithValidData()
    {
        $data = ['name' => 'John', 'email' => 'john@example.com'];
        $user = $this->service->createUser($data);

        $this->assertInstanceOf(User::class, $user);
        $this->assertEquals('John', $user->name);
    }

    public function testCreateUserThrowsExceptionWithInvalidEmail()
    {
        $this->expectException(ValidationException::class);
        $this->service->createUser(['email' => 'invalid']);
    }
}

Run Tests

vendor/bin/phpunit

Resources

  • Test business logic
  • Mock external dependencies
  • Use descriptive test names

valec3/Agents.skills.md/tree/main/.agent/skills/backend-testing-phpunit commit fa9cf92bde

Frequently asked questions

npx skillmds@latest add valec3/backend-testing-phpunit