Тести

← Тести

Ashterix

docs(articles): Magento and Symfony examples side by side, request lifecycle as a Mermaid diagram

uk/articles/magento-to-symfony/quality/testing.md +6 −1

Тести

Magento · інтеграційний тест контролера

/**
 * @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');
    }
}

Magento · інтеграційний тест контролера

/**
 * @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: звичайний TestCase, як і в Magento.
  • KernelTestCase: справжній контейнер, self::getContainer()->get(Importer::class) дає навіть приватні сервіси, якщо їх десь використано.
  • WebTestCase: HTTP-запити без вебсервера, асерти на відповідь і DOM.
  • БД: окрема тестова база з .env.test, DAMA DoctrineTestBundle відкочує транзакцію після кожного тесту, Foundry створює дані фабриками замість файлів-фікстур.
  • Підміна сервісу: self::getContainer()->set(ErpClient::class, $mock) до першого використання.