Tests

โ† Tests

Ashterix

docs(articles): English translation of From Magento to Symfony

en/articles/magento-to-symfony/quality/testing.md new file +47 โˆ’0

1 + # Tests
2 +
3 + **Magento** ยท controller integration test
4 +
5 + ```php
6 + /**
7 + * @magentoDataFixture Vendor_Faq::Test/_files/question.php
8 + * @magentoAppArea frontend
9 + */
10 + public function testView(): void
11 + {
12 + $this->dispatch('faq/question/view/id/1');
13 + $this->assertStringContainsString(
14 + 'How to return',
15 + $this->getResponse()->getBody()
16 + );
17 + }
18 + ```
19 +
20 + **Symfony** ยท WebTestCase + Foundry
21 +
22 + ```php
23 + final class FaqControllerTest extends WebTestCase
24 + {
25 + use Factories, ResetDatabase;
26 +
27 + public function testShowsQuestion(): void
28 + {
29 + $client = static::createClient();
30 + QuestionFactory::createOne([
31 + 'title' => 'How to return',
32 + 'slug' => 'how-to-return',
33 + ]);
34 +
35 + $client->request('GET', '/faq/how-to-return');
36 +
37 + self::assertResponseIsSuccessful();
38 + self::assertSelectorTextContains('h1', 'How to return');
39 + }
40 + }
41 + ```
42 +
43 + - **Unit:** a plain `TestCase`, just like in Magento.
44 + - **KernelTestCase:** the real container; `self::getContainer()->get(Importer::class)` returns even private services, as long as they are used somewhere.
45 + - **WebTestCase:** HTTP requests without a web server, assertions on the response and the DOM.
46 + - **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.
47 + - **Replacing a service:** `self::getContainer()->set(ErpClient::class, $mock)` before its first use.