Tests

← Tests

Ashterix

docs(articles): SEO titles, intros and a section preview image for From Magento to Symfony

en/articles/magento-to-symfony/quality/testing.md +6 −0

Tests

In Symfony, KernelTestCase and WebTestCase replace Magento integration tests: a real container and HTTP requests without a web server, with test data from Foundry factories.

Magento · controller integration test

/**
 * @magentoDataFixture Vendor_Faq::Test/_files/question.php
 * @magentoAppArea frontend
 */
public function testView(): void
{
    $this->dispatch('faq/question/view/id/1');
    $this->assertStringContainsString(
        'How to return',
        $this->getResponse()->getBody()
    );
}

Symfony · WebTestCase + Foundry

final class FaqControllerTest extends WebTestCase
{
    use Factories, ResetDatabase;

    public function testShowsQuestion(): void
    {
        $client = static::createClient();
        QuestionFactory::createOne([
            'title' => 'How to return',
            'slug' => 'how-to-return',
        ]);

        $client->request('GET', '/faq/how-to-return');

        self::assertResponseIsSuccessful();
        self::assertSelectorTextContains('h1', 'How to return');
    }
}
  • Unit: a plain TestCase, just like in Magento.
  • KernelTestCase: the real container; self::getContainer()->get(Importer::class) returns even private services, as long as they are used somewhere.
  • WebTestCase: HTTP requests without a web server, assertions on the response and the DOM.
  • Database: a separate test database from .env.test; DAMA DoctrineTestBundle rolls back the transaction after each test, and Foundry creates data with factories instead of fixture files.
  • Replacing a service: self::getContainer()->set(ErpClient::class, $mock) before its first use.