0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【PHPフレームワークFlow】FunctionalテストでRepositoryのテストをする

Posted at

はじめに

今まで紹介したFunctionalテストは全てAPI単位で実行を行っていました。
今回は、Repositoryのメソッドに対し、実際にDBアクセスのあるFunctionalテストを書いてみようと思います。

Functionalテストとは

Functionalテストとは、Flowが提供する「アプリケーションの機能単位」を対象としたテスト機能です。
実際のリクエストやレスポンス、データベースを通して動作を確認できます。詳しくはこちらの記事をご参照ください。

FunctionalテストでRepositoryのテストを書く

以前はAPI単位でFunctionalTestを実行しましたが、もう少し小さい単位で書くことが可能です。
今回はRepositoryのメソッド単位で実行してみましょう。

ということで、以下のRepositoryクラスのfindByEmailメソッドのテストを書いてみようと思います。

<?php

namespace Neos\Welcome\Domain\Repository;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\Repository;
use Neos\Welcome\Domain\Model\Users;

/**
 * @Flow\Scope("singleton")
 */
class UsersRepository extends Repository
{
    public function createUser(string $name, string $email, string $password): void
    {
        $user = new Users($name, $email, $password);
        $this->add($user);
    }

    public function findByEmail(string $email): ?Users
    {
        $query = $this->createQuery();
        $query->matching(
            $query->equals('email', $email)
        );
        return $query->execute()->getFirst();
    }
}

テスト作成

実際に作成したテストがこちらです。
見た目はUnitテストとほぼ同じですね。

Arrangeでテストデータを登録しておくところだけ少し違うくらいですね。

<?php

namespace Neos\Welcome\Tests\Functional\Domain\Repository;

use Neos\Flow\Tests\FunctionalTestCase;
use Neos\Welcome\Domain\Model\Functional;
use Neos\Welcome\Domain\Repository\FunctionalRepository;

class UsersRepositoryTest extends FunctionalTestCase
{

    /**
     * @var boolean
     */
    protected static $testablePersistenceEnabled = true;

    /**
     * @var \Neos\Welcome\Domain\Repository\UsersRepository
     */
    protected $usersRepository;


    public function setUp(): void
    {
        parent::setUp();
        $this->usersRepository = $this->objectManager->get(\Neos\Welcome\Domain\Repository\UsersRepository::class);
    }

    /**
     * ユーザ参照のテスト
     * @test
     */
    public function createUserTest()
    {
        // Arrange
        $name = "testUser";
        $email = "aaa@example.com";
        $password = "pass";
        $this->usersRepository->createUser($name, $email, $password);
        $this->persistenceManager->persistAll();

        // Act
        $user = $this->usersRepository->findByEmail($email);

        // Assert
        self::assertSame($name, $user->getName());
        self::assertSame($email, $user->getEmail());
        self::assertSame($password, $user->getPassword());
    }
}

実行

実行してみました。
一回のテストで5秒かかっています。APIの時と同様、実行速度がネックですね。
とはいえ、Repositoryのテストができるのは嬉しいです。

> .\bin\phpunit -c .\Build\BuildEssentials\PhpUnit\FunctionalTests.xml .\Packages\Application\Neos.Welcome\Tests\Functional\Domain\Repository\UsersRepositoryTest.php
PHPUnit 9.6.22 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 00:05.456, Memory: 90.00 MB

OK (1 test, 3 assertions)

おわりに

AIコーディングエージェントにTDDを進めてもらうことが増えましたが、Repositoryのテストができるようになればさらに幅が広がりそうです。

ここまでご覧いただきありがとうございました!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?