Symfony testing

Updated: 10 December 2024

See https://symfony.com/doc/current/testing.html

Unit Tests

  1. Test a Class.
  2. Test a method in a Class.
  3. Equivalent to standard PHPUnit unit tests.
  4. Does not hit the database – use mocks instead.

Integration Tests

  1. Tests a larger part of the app than Unit tests e.g. a combination of Services.
  2. Might use the Kernel to fetch services from the dependency injection container.
  3. Integration test classes should extend KernelTestCase.
  4. May involve mocking of Class dependencies.
  5. May interact with a test database, populated via fixtures.

Application Tests

  1. Checks integration of all layers of the app, from routing to views, excluding on page javascript.
  2. Has a specific workflow:
    1. Make request.
    2. Interact with page.
    3. Test response.
  3. Probably won’t involve mocking dependencies.
  4. Probably will interact with a test database.
  5. Integration test classes should extend WebTestCase.

End to End Tests

  1. Test the application as a whole, including on page JavaScript code.
  2. Involves a real browser instead of the test client.
  3. See Symfony Panther component.

The output of make:test supplies a useful summary

root@5b8333e33939:/var/www/html# bin/console make:test

 Which test type would you like?:
  [TestCase       ] basic PHPUnit tests
  [KernelTestCase ] basic tests that have access to Symfony services
  [WebTestCase    ] to run browser-like scenarios, but that do not execute JavaScript code
  [ApiTestCase    ] to run API-oriented scenarios
  [PantherTestCase] to run e2e scenarios, using a real-browser or HTTP client and a real web server

Composer tool itself

Updated: 27 July 2024

Install Composer phar

curl -sS https://getcomposer.org/installer | php

Circumvent php memory limit

php -d memory_limit=-1 composer.phar install

Up / downgrade Composer itself

composer self-update --help
composer self-update --1
composer self-update --2

Composer in Docker

Updated: 17 January 2023

Require a new project as a dependency

docker run --rm \
    -v $(pwd):/app \
    composer/composer:latest \
    require slim/slim "^3.0"

Install all dependencies in a project

docker run --rm --interactive --tty --volume $PWD:/app composer install

PHP in Docker

Updated: 17 May 2024

Pre-configured Docker images (including Xdebug) from Jetbrains https://github.com/JetBrains/phpstorm-docker-images


Run a php script

docker run -it --rm --name my-running-script \
-v "$PWD":/usr/src/myapp \
-w /usr/src/myapp \
php:7.4-cli php script.php

Run the php development server

docker run -it --rm --name php-server \
-v "$PWD":/usr/src/myapp \
-w /usr/src/myapp \
-p 8000:8000 \
php:8.2-cli php -S 0.0.0.0:8000

PHP deprecations, notices, warnings & errors

Updated: 26 November 2024

Attempt to activate and display all errors, warnings, deprecations and notices

error_reporting(E_ALL);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', 'php-error.log')

// check the settings above
trigger_error('This is E_USER_DEPRECATED', E_USER_DEPRECATED);
trigger_error('This is E_USER_NOTICE', E_USER_NOTICE);
trigger_error('This is E_USER_WARNING', E_USER_WARNING);
trigger_error('This is E_USER_ERROR', E_USER_ERROR);

This script demonstrates how to temporarily adjust which PHP errors are reported, to avoid displaying a deprecation notice to users

// Activate every error, warning, deprecation & notice.
error_reporting(E_ALL);

// Get the current error reporting value.
$error_reporting = error_reporting();

// Reset the error reporting value but now excluding E_DEPRECATED.
error_reporting($error_reporting & ~E_DEPRECATED);

// With PHP 7.4, trigger an E_DEPRECATED.
get_magic_quotes_gpc();
// No deprecation message.

// Reset error reporting to original value.
error_reporting($error_reporting);

// Attempt to trigger an E_DEPRECATED.
get_magic_quotes_gpc();
// Deprecation message here.

PHP

Updated: 23 June 2025

PHP supported versions https://www.php.net/supported-versions
PHP Local Security Checker https://github.com/fabpot/local-php-security-checker


Check all extensions

php -m

Disable the xdebug module for the fpm sapi and php version 5.6

sudo phpdismod -v 5.6 -s fpm -m xdebug

Enable the xdebug module for the fpm sapi and php version 7.0

sudo phpenmod -v 7.0 -s fpm -m xdebug

Check the status of the xdebug module for the fpm sapi and php 7.4

phpquery -v 7.4 -s fpm -m xdebug

Restart the PHP 5.6 Fast Process Manager

sudo systemctl restart php5.6-fpm

Enable strict mode on a per-file basis

<?php declare(strict_types=1);