Symfony 與 Testcontainers 的整合測試

WBOY
發布: 2024-09-12 10:23:00
原創
377 人瀏覽過

免責聲明:我不是神聖實體。我所說的並不是絕對的真理。甚至不要害怕質疑世界,因為它可能是錯的,而不是你。

今天對任何人來說,自動化測試對於保持軟體品質和完整性的重要性都不是秘密,通常我們會談論單元測試,但是今天,我們將更多地關注到 Symfony 框架中的整合測試。

我沒有耐心,給我看程式碼!

好吧好吧!如果你沒有耐心閱讀本文,我在下面的連結中有一個實現本文的測試項目。

https://github.com/joubertredrat/symfony-testcontainers

Symfony 框架和整合測試

如今,Symfony 框架是 PHP 領域最成熟、最穩定的框架之一,它擁有各種良好的實施解決方案,例如整合測試。但是,我個人一直認為,雖然整合測試本身很容易,但為測試提供外部依賴項並不總是那麼容易,例如資料庫。

即使使用像Docker 這樣的解決方案,我仍然意識到有必要以某種方式提供外部依賴項以進行測試,但是,存在一個非常有趣的解決方案,可以使這一步變得更加容易,即Testcontainers。

測試容器

Testcontainers 是一個開源框架,可讓您以簡單的方式提供使用 Docker 所需的任何外部依賴項,例如資料庫、訊息代理程式、快取系統或容器中的依賴項。

Testcontainers 相對於Docker compose 或其他容器編排方式的一大優勢是您可以對容器的配置進行編碼,並且現在已經支援Golang、Java、.NET、Node.js、Python、Rust 等各種語言其他語言,當然還有PHP!

我第一次接觸 Testcontainers 是在 Golang 的一個專案中,我非常喜歡配置 MongoDB 容器來進行儲存庫測試的功能,之後,我決定使用 Symfony 框架在我的個人 PHP 專案中做同樣的事情。

Symfony + 測試容器 = ❤️

Symfony 最大的優勢之一是支援與 PHPUnit 整合進行測試,並使用功能核心來執行測試所需的引導程式。

雖然 Testcontainers 支援 PHP,但它們的實作是最近才實現的,您可以在 https://github.com/testcontainers/testcontainers-php 查看它。

下面我們有一個MySQL 8.0容器的實現,它是這個專案的外部依賴,以及Symfony核心的啟動,資料庫和模式的建立。

class IntegrationTestCase extends KernelTestCase
{
    protected static ?MySQLContainer $container = null;

    public static function setUpBeforeClass(): void
    {
        parent::setUpBeforeClass();

        if (!static::$container) {
            static::$container = MySQLContainer::make('8.0', 'password');
            static::$container->withPort('19306', '3306');
            static::$container->run();

            $kernel = self::bootKernel();
            $container = $kernel->getContainer();

            $application = new Application($kernel);
            $application->setAutoExit(false);

            $application->run(
                new ArrayInput(['command' => 'doctrine:database:create', '--if-not-exists' => true])
            );

            $entityManager = $container->get('doctrine')->getManager();
            $metadata = $entityManager->getMetadataFactory()->getAllMetadata();
            $schemaTool = new SchemaTool($entityManager);
            $schemaTool->dropSchema($metadata);
            $schemaTool->createSchema($metadata);
        }
    }

    public static function tearDownAfterClass(): void
    {
        parent::tearDownAfterClass();

        if (static::$container instanceof MySQLContainer) {
            static::$container->remove();
        }
    }
登入後複製

有了這個,我們就有了將執行測試本身的類別的基礎類,如下例所示。

class UserRepositoryTest extends IntegrationTestCase
{
    public function testSave(): void
    {
        $user = new User();
        $user->setName('John Doe');
        $user->setEmail('john@doe.local');

        $repo = $this->getRepository();
        $repo->save($user, true);

        self::assertNotNull($user->getId());
        self::assertIsInt($user->getId());
        self::assertTrue($user->getId() > 0);
    }

    public function testGetByEmail(): void
    {
        $user = new User();
        $user->setName('John Doe');
        $user->setEmail('john2@doe.local');

        $repo = $this->getRepository();
        $userNotFound = $repo->getByEmail($user->getEmail());
        self::assertNull($userNotFound);

        $repo->save($user, true);
        $userFound = $repo->getByEmail($user->getEmail());

        self::assertEquals($user->getEmail(), $userFound->getEmail());
    }

    protected function tearDown(): void
    {
        parent::tearDown();

        $connection = $this
            ->getContainer()
            ->get('doctrine')
            ->getManager()
            ->getConnection()
        ;
        $connection->executeStatement('TRUNCATE TABLE users');
    }

    protected function getRepository(): UserRepository
    {
        return $this->getContainer()->get(UserRepository::class);
    }
}
登入後複製

運行測試套件時,您可以看到與僅具有模擬行為的測試的單元測試相比,測試執行速度較慢,但這是正常的,因為在此過程中,Testcontainers 正在配置您定義在測試中使用的容器.

Integration tests on Symfony with Testcontainers

Integration tests on Symfony with Testcontainers

最後,有了這個設施,就可以做一些瘋狂的事情,例如 100% 的覆蓋率。不相信嗎?您可以在 https://joubertredrat.github.io/symfony-testcontainers 親自查看。

就這樣,下期見!

以上是Symfony 與 Testcontainers 的整合測試的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!