lundi 28 octobre 2019

Is it normal to combine methods of delivery dummy data in functional tests

It will be about the project written in Simony 4 and testing with the participation of the database. There is an opinion that the test data should be exactly the same in each test and should be delivered only through fixtures (I mean the extensions of Doctrine\Bundle\FixturesBundle\Fixture). But I think, dummy data could be dilvered to DB by different ways: by creation data inside test, by dataProvider method, and of course by fixtures. This is at the discretion of the developer

class UsersTest extends WebTestCase
{

    private EntityManagerInterface $entityManager;
    private UserManager $userService;

    public function setUp(): void
    {
        $this->entityManager = self::$container->get(EntityManagerInterface::class);
        $this->userService = self::$container->get(UserManager::class);
    }

    //
    //    DATA CREATION INSIDE TEST
    //
    public function testCountUsers(): void
    {
        $user = new User();
        $this->entityManager->persist($user);
        $this->entityManager->flush();
        $count = $this->userService->getAllUsersCount();
        $this->assertEquals(1, $count);
    }

    //
    // DELIVERY DATA BY DATAPROVIDER
    //
    public function testCountUsers2(array $users, int $expectedCount): void
    {
        foreach ($users as $user) {
            $this->entityManager->persist($user);
        }
        $this->entityManager->flush();
        $count = $this->userService->getAllUsersCount();
        $this->assertEquals($expectedCount, $count);
    }

    public static function usersCountDataProvider(): array
    {
        return [
            [
                'users' => [new User(), new User(), new User()],
                'expectedCount' => 3
            ],
            [
                'users' => [],
                'expectedCount' => 0
            ],
        ];
    }

    //
    // DELIVERY DATA BY FIXTURE
    //
    public function testCountUsers3(): void
    {
        $this->loadFixtures([UsersFixtures::class]);
        $count = $this->userService->getAllUsersCount();
        $this->assertEquals(4, $count);
    }
}

How do you think, is that normal to make tests in such ways?

Aucun commentaire:

Enregistrer un commentaire