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

Symfony console

Updated: 23 November 2024

Generate a migration by comparing your current database to your mapping information

bin/console doctrine:migrations:diff

Create a new migration based on database changes

bin/console make:migration

List all configured routes in application

bin/console debug:router

Display environment variables used in the container

bin/console debug:container --env-vars

Dump the default configuration for security

bin/console config:dump-reference security

Symfony – scaffold app from existing database

Updated: 04 April 2024

Prepare for a new run

rm -r src/Entity/*
rm -r src/Form/*
rm -r src/Controller/*
rm -r src/Repository/*
rm -r templates/*

Copy in a backup of base twig template

cp base.html.twig templates/

Generate entities from an existing database

bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
bin/console make:entity --regenerate App

Make CRUD for each new entity

bin/console make:crud MyEntity
bin/console make:crud AnotherEntity
...

Adjust file permissions

sudo chown -R chris:chris templates/
sudo chown -R chris:chris src/Entity/
sudo chown -R chris:chris src/Controller/
sudo chown -R chris:chris src/Form/

Now, sql insert data fixtures

...