Skip to content

Running Tests

Guide to running the automated test suite for the Expensis application.


Test Suite Overview

The Expensis test suite uses PHPUnit to validate application functionality:

  • Unit Tests - Test individual components in isolation
  • Integration Tests - Test component interactions
  • Service Tests - Test business logic services
  • Factory Tests - Test object creation and configuration
  • Validator Tests - Test validation rules

Quick Start

Run All Tests

cd /root/expensis
php bin/phpunit

Run Specific Test Directory

# Run service tests only
php bin/phpunit tests/AppBundle/Service

# Run validator tests only
php bin/phpunit tests/AppBundle/Validator

# Run factory tests only
php bin/phpunit tests/AppBundle/Factory

Run Specific Test File

php bin/phpunit tests/AppBundle/Service/SpecificServiceTest.php

Run Specific Test Method

php bin/phpunit --filter testMethodName tests/AppBundle/Service/SpecificServiceTest.php

Test Organization

tests/
└── AppBundle/
    ├── Factory/           # Tests for factory classes
    ├── Model/             # Tests for model/entity classes
    ├── Service/           # Tests for business logic services
    ├── Strategy/          # Tests for strategy pattern implementations
    ├── Validator/         # Tests for validation logic
    ├── ValueObjects/      # Tests for value objects
    └── bootstrap.php      # Test bootstrap configuration

Running Tests with Coverage

Generate HTML Coverage Report

php bin/phpunit --coverage-html var/test-coverage

View the report:

# Open in browser
firefox var/test-coverage/index.html

Generate Text Coverage Summary

php bin/phpunit --coverage-text

Generate Clover Coverage (for CI)

php bin/phpunit --coverage-clover var/coverage.xml

Test Output Options

Verbose Output

php bin/phpunit --verbose

Test Summary Only

php bin/phpunit --no-output

Stop on First Failure

php bin/phpunit --stop-on-failure

Color Output

# Enable colors
php bin/phpunit --colors=always

# Disable colors
php bin/phpunit --colors=never

Debugging Failed Tests

Run Failed Test with Debug Output

php bin/phpunit --verbose --debug tests/AppBundle/Service/FailingTest.php

Check Test Logs

# Application logs during tests
tail -f var/log/test.log

# PHPUnit output
cat var/test-output.log

Common Failure Causes

  1. Database State - Tests may fail if database is in unexpected state

    # Reset test database
    php bin/console doctrine:database:drop --env=test --force
    php bin/console doctrine:database:create --env=test
    php bin/console doctrine:migrations:migrate --env=test --no-interaction
    

  2. Cache Issues - Clear test cache

    php bin/console cache:clear --env=test
    

  3. Missing Dependencies - Update composer dependencies

    composer install
    


Writing New Tests

Test File Structure

<?php

namespace Tests\AppBundle\Service;

use PHPUnit\Framework\TestCase;

class ExampleServiceTest extends TestCase
{
    private $service;

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

    public function testExample(): void
    {
        $result = $this->service->doSomething();
        $this->assertEquals('expected', $result);
    }

    protected function tearDown(): void
    {
        parent::tearDown();
        $this->service = null;
    }
}

Test Naming Conventions

  • Test files: *Test.php
  • Test classes: ClassNameTest
  • Test methods: testMethodName() or test_method_name()

Assertions

// Equality
$this->assertEquals($expected, $actual);
$this->assertSame($expected, $actual); // Strict comparison

// Boolean
$this->assertTrue($condition);
$this->assertFalse($condition);

// Null checks
$this->assertNull($value);
$this->assertNotNull($value);

// Arrays
$this->assertArrayHasKey('key', $array);
$this->assertCount(5, $array);

// Exceptions
$this->expectException(ExceptionClass::class);
$this->expectExceptionMessage('error message');

Continuous Integration

GitLab CI Example

test:
  stage: test
  script:
    - composer install
    - php bin/console doctrine:database:create --env=test
    - php bin/console doctrine:migrations:migrate --env=test --no-interaction
    - php bin/phpunit --coverage-text --colors=never
  coverage: '/^\s*Lines:\s*\d+.\d+\%/'

Run Tests Before Commit

# Add to .git/hooks/pre-commit
#!/bin/bash
php bin/phpunit --stop-on-failure
if [ $? -ne 0 ]; then
    echo "Tests failed. Commit aborted."
    exit 1
fi

Performance Testing

Measure Test Execution Time

php bin/phpunit --log-junit var/test-results.xml

Identify Slow Tests

# Tests taking longer than 1 second
php bin/phpunit --verbose | grep -E "^\s+[0-9]\.[0-9]{3}\ss"

Test Database Management

Setup Test Database

# Create test database
php bin/console doctrine:database:create --env=test

# Run migrations
php bin/console doctrine:migrations:migrate --env=test --no-interaction

# Load fixtures (if available)
php bin/console doctrine:fixtures:load --env=test --no-interaction

Reset Test Database

# Drop and recreate
php bin/console doctrine:database:drop --env=test --force
php bin/console doctrine:database:create --env=test
php bin/console doctrine:migrations:migrate --env=test --no-interaction