Skip to content

PHPUnit Backend Testing

The Symfony backend is the heart of the Yappa Knowledge Hub. We use PHPUnit to validate Entities, Services, Controllers, and Orchestrators in absolute isolation and against an SQLite in-memory database.

Test Directory Structure

plaintext
backend/tests/
 ├── Entity/            # Unit testing Doctrine Entities
 │    ├── CategoryTest.php
 │    ├── KnowledgeTest.php
 ├── Controller/        # Integration/API tests for the Symfony Web API
 │    ├── CategoryControllerTest.php
 │    ├── KnowledgeControllerTest.php
 ├── Service/           # Unit and Integration testing for Domain logic
 │    ├── NotionSyncServiceTest.php
 └── bootstrap.php      # Environment configuration for PHPUnit

Running the Tests

To execute the backend testing suite, you must first SSH into the backend environment directory. The tests utilize the standard Symfony phpunit bridge.

bash
cd backend
./vendor/bin/phpunit

Running Specific Suites

If you are iterating aggressively on a single layer (e.g., Domain logic), restrict the execution path for speed:

bash
# Run only Entity unit tests
./vendor/bin/phpunit tests/Entity

# Run only Controller API checks
./vendor/bin/phpunit tests/Controller

Coverage Reports

Generate an intricate HTML trace of exactly which functions have been validated:

bash
./vendor/bin/phpunit --coverage-html coverage

NOTE

You can view the output by checking backend/coverage/index.html in your browser.

Writing a WebTestCase (API Test)

When testing API Controllers, we skip unit isolation to explicitly test HTTP responses. These extend Symfony's WebTestCase.

php
namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class KnowledgeControllerTest extends WebTestCase
{
    public function testCreateKnowledgeEndpoint(): void
    {
        $client = static::createClient();
        
        // POST to the API endpoint with serialized JSON
        $client->request('POST', '/api/knowledge', [], [], [
            'CONTENT_TYPE' => 'application/json',
        ], json_encode(['title' => 'Test', 'url' => 'https://example.com']));

        $this->assertResponseStatusCodeSame(201);
    }
}

Database Reset Strategy

During API (WebTestCase) or Repository tests, your assertions will write data. To prevent test bleed, tests execute against an isolated test environment (typically an in-memory SQLite database as defined in backend/.env.test).

If you run into database locking or stale migration issues, violently reset the test schema:

bash
cd backend
php bin/console doctrine:database:drop --force --env=test
php bin/console doctrine:database:create --env=test
php bin/console doctrine:migrations:migrate --no-interaction --env=test