PHP code example of rockylars / faker

1. Go to this page and download the library: Download rockylars/faker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rockylars / faker example snippets


final class FakeUserRepository extends Faker implements UserRepositoryInterface
{
    public const FUNCTION_GET_USER_BY_ID = 'getUserById';
    public const FUNCTION_GET_USERS = 'getUsers';
    public const FUNCTION_UPDATE_LAST_LOGIN = 'updateLastLogin';
    public const FUNCTION_DELETE_USER = 'deleteUser';

    public function getUserById(int $userId): User
    {
        return $this->fakeCall(__FUNCTION__, [
            'userId' => $userId,
        ]);
    }

    /** @return array<int, User> */
    public function getUsers(): array
    {
        return $this->fakeCall(__FUNCTION__, [
            'a call was made',
        ]);
    }

    public function updateLastLogin(int $userId): void
    {
        $this->fakeCall(__FUNCTION__, [
            'userId' => $userId,
        ]);
    }

    public function deleteUser(int $userId): void
    {
        $this->fakeCall(__FUNCTION__, [
            'userId' => $userId,
        ]);
    }
}

final class DeleteUserServiceCest
{
    private DeleteUserService $deleteUserService;
    private FakeLogger $fakeLogger;
    private FakeUserRepository $fakeUserRepository;

    public function _before(UnitTester $tester): void
    {
        $this->deleteUserService = new DeleteUserService(
            $this->fakeLogger = new FakeLogger(),
            $this->fakeUserRepository = new FakeUserRepository()
        );
    }

    public function deleteUserWillCheckAndDeleteUserByIdIfNothingIsLinked(UnitTester $tester): void
    {
        $this->fakeUserRepository->setResponsesFor(FakeUserRepository::FUNCTION_GET_USER_BY_ID, [
            [Faker::ACTION_RETURN => new User(
                id: 1,
                name: 'Rocky',
                isAdmin: false,
                lastLogin: DateTimeImmutable::createFromFormat(
                    '!Y-m-d H:i:s',
                    '2023-02-17 12:13:14',
                    new \DateTimeZone('Europe/Amsterdam')
                ),
            )],
        ]);
        $this->fakeUserRepository->setResponsesFor(FakeUserRepository::FUNCTION_DELETE_USER, [
            [Faker::ACTION_VOID => null],
        ]);

        $this->deleteUserService->deleteUser(1);

        $tester->assertSame(
            [
                [
                    'level' => 'debug',
                    'message' => 'User 1 was deleted',
                    'context' => [],
                ],
            ],
            $this->fakeLogger->getLogs(),
        );
        $tester->assertSame(
            [
                FakeUserRepository::FUNCTION_DELETE_USER => [
                    [
                        'userId' => 1,
                    ],
                ],
                FakeUserRepository::FUNCTION_GET_USER => [
                    [
                        'userId' => 1,
                    ],
                ],
            ],
            $this->fakeUserRepository->getAllCallsInStyleSorted()
        );
    }
}

// Using expectException and expectExceptionMessage will stop the test at the error, so a try catch is used instead.
$exceptionWasCaught = false;
try {
    // Call that will throw an exception
} catch (\Throwable $throwable) {
    // Do not assert like this on natural exceptions as those generate traces and such you can't just replicate.
    self::assertEquals(
        new \RuntimeException(
            "something happened",
            301
        ),
        $throwable
    );
    $exceptionWasCaught = true;
}
self::assertTrue($exceptionWasCaught);