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¶
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¶
Run Specific Test Method¶
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¶
View the report:
Generate Text Coverage Summary¶
Generate Clover Coverage (for CI)¶
Test Output Options¶
Verbose Output¶
Test Summary Only¶
Stop on First Failure¶
Color Output¶
Debugging Failed Tests¶
Run Failed Test with Debug Output¶
Check Test Logs¶
Common Failure Causes¶
-
Database State - Tests may fail if database is in unexpected state
-
Cache Issues - Clear test cache
-
Missing Dependencies - Update composer dependencies
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()ortest_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¶
Identify Slow Tests¶
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
Related Documentation¶
- Common Issues - Troubleshooting test failures
- Debugging Guide - Debug failing tests
- System Architecture - Understanding what's being tested