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

Leave a comment