Skip to content

Symfony 7: Integration Tests

  • PHPUnit: Integration Testing with Live Services
  • use real data / external services;
  • only reads .env.test (put db connection data there)
  • database will have "_test" suffix (check config/packages/doctrine.yaml, when@test);
  • if using Doctrine, in the app, use Foundry in the tests to help;

KernelTestCase

  • extend instead of TestCase when need to access services from the Symfony container (instead of dependency injection and autowiring which is only available in the application);
class Test extends KernelTestCase
{
    public funciton testSomething(): void
    {
        self::bootKernel();

        // Get a service from the container
        $service = self::getContainer()->get(SERVICE_ID);
        assert($service instanceof SERVICE);
    }
}

Partial mocking

  • use a mock among real services.
  • create mock, then pass it to the Symfony container: self::getContainer()->set(...);