Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common user chat in tests #325

Merged
merged 8 commits into from
Feb 16, 2023
Merged

Common user chat in tests #325

merged 8 commits into from
Feb 16, 2023

Conversation

Lukasss93
Copy link
Member

This PR adds 2 new method to the testing system:

  • setCommonUser
  • setCommonChat

Handlers

$bot->onCommand('start', function (Nutgram $bot) {
    assignCreditsToUserIfNotExists(userId: $bot->userId(), credit: 1);    // Assign 1 credit to the user if the user credit is not set. Otherwise, do nothing.
    $credits = getUserCredits(userId: $bot->userId(), default: 0);
    $bot->sendMessage("Credits: $credits");                               // It will return: "Credits: 1".
});

$bot->onCommand('credits', function (Nutgram $bot) {
    $credits = getUserCredits(userId: $bot->userId(), default: 0);
    $bot->sendMessage("Credits: $credits");                              // It should return: "Credits: 1".
});
    

Wrong tests

it('sends /start command', function () {
    $this->bot
        ->hearText('/start')
        ->reply()
        ->assertReplyText('Credits: 1');  // It will return: "Credits: 1". ✅
});

it('sends /credits command', function () {
    $this->bot
        ->hearText('/credits')
        ->reply()
        ->assertReplyText('Credits: 1');  // It will return: "Credits: 0". ❌
});

Why the last test fails? Because the user is not the same between tests.

It's different because the user is generated randomly if not explicitly set.

Valid tests

Before this PR

Version 1

it('sends /start command', function () {
    $this->bot
        ->hearMessage([
            'text' => '/start',
            'from' => [
                'id' => 123456789,
                'is_bot' => false,
                'first_name' => 'Tony',
                'last_name' => 'Stark',
                'username' => 'tonystark',
                'language_code' => 'en',
            ],
            'chat' => [
                'id' => 123456789,
                'first_name' => 'Tony',
                'last_name' => 'Stark',
                'username' => 'tonystark',
                'type' => 'private',
            ]
        ])
        ->reply()
        ->assertReplyText('Credits: 1');    // It will return: "Credits: 1". ✅
});

it('sends /credits command', function () {
    $this->bot
        ->hearMessage([
            'text' => '/credits',
            'from' => [
                'id' => 123456789,
                'is_bot' => false,
                'first_name' => 'Tony',
                'last_name' => 'Stark',
                'username' => 'tonystark',
                'language_code' => 'en',
            ],
            'chat' => [
                'id' => 123456789,
                'first_name' => 'Tony',
                'last_name' => 'Stark',
                'username' => 'tonystark',
                'type' => 'private',
            ]
        ])
        ->reply()
        ->assertReplyText('Credits: 1');   // It will return: "Credits: 1". ✅
});

Version 2

function useMockUser(array $values = []): array {
    return array_merge([
        'from' => [
            'id' => 123456789,
            'is_bot' => false,
            'first_name' => 'Tony',
            'last_name' => 'Stark',
            'username' => 'tonystark',
            'language_code' => 'en',
        ],
        'chat' => [
            'id' => 123456789,
            'first_name' => 'Tony',
            'last_name' => 'Stark',
            'username' => 'tonystark',
            'type' => 'private',
        ],
    ], $values);
}

it('sends /start command', function () {
    $this->bot
        ->hearMessage(useMockUser(['text' => '/start']))
        ->reply()
        ->assertReplyText('Credits: 1');    // It will return: "Credits: 1". ✅
});

it('sends /credits command', function () {
    $this->bot
        ->hearMessage(useMockUser(['text' => '/credits']))
        ->reply()
        ->assertReplyText('Credits: 1');   // It will return: "Credits: 1". ✅
});

After this PR

beforeEach(function () {
    // $this->bot = get nutgram instance
    
    $this->bot->setCommonUser(User::make(
        id: 123456789,
        is_bot: false,
        first_name: 'Tony',
        last_name: 'Stark',
        username: 'IronMan',
        language_code: 'en',
    ));

    $this->bot->setCommonChat(Chat::make(
        id: 123456789,
        type: 'private',
        username: 'IronMan',
        first_name: 'Tony',
        last_name: 'Stark',
    ));
});

it('sends /start command', function () {
    $this->bot
        ->hearText('/start')
        ->reply()
        ->assertReplyText('Credits: 1');    // It will return: "Credits: 1". ✅
});

it('sends /credits command', function () {
    $this->bot
        ->hearText('/credits')
        ->reply()
        ->assertReplyText('Credits: 1');    // It will return: "Credits: 1". ✅
});

src/Telegram/Types/Chat/Chat.php Show resolved Hide resolved
src/Telegram/Types/User/User.php Show resolved Hide resolved
src/Telegram/Types/Chat/Chat.php Show resolved Hide resolved
@codeclimate
Copy link

codeclimate bot commented Feb 15, 2023

Code Climate has analyzed commit 243882a and detected 4 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 4

The test coverage on the diff in this pull request is 100.0% (60% is the threshold).

This pull request will bring the total coverage in the repository to 95.7% (0.0% change).

View more on Code Climate.

@Lukasss93 Lukasss93 marked this pull request as ready for review February 15, 2023 18:25
@sergix44 sergix44 merged commit 546842e into master Feb 16, 2023
@sergix44 sergix44 deleted the common-user-chat-in-tests branch February 16, 2023 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants