kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
995 stars 161 forks source link

Implement Test with FirebaseFactory. #53

Closed mahdiaslami closed 1 year ago

mahdiaslami commented 3 years ago

I Searched in kreait\firebase-php package i found it use Kreait\Firebase\Factory in IntegerationTestCase class and used it to test. (For example test subscriteToTopic method in FirebaseMessaging class).

Is it possible to adding fake method to FirebaseMessaging facade and other facades for mocking and used it in test like MailFake class in laravel.

Example change in FirebaseMessaging facade:

/**
 * @see \Kreait\Firebase\Messaging
 */
final class FirebaseMessaging extends Facade
{
    /**
     * Replace the bound instance with a fake.
     *
     * @return \Illuminate\Support\Testing\Fakes\MailFake
     */
    public static function fake()
    {
        static::swap($fake = (new FirebaseFactory())->createMessaging());

        return $fake;
    }

    protected static function getFacadeAccessor()
    {
        return 'firebase.messaging';
    }
}

Example test for an API:

/** @test */
public function subscribe_temp_topic()
{
    FirebaseMessaging::fake();

    $body = [
        'topic' => 'temp',
        'fcmToken' => 'token'
    ];

    $this->post('/subscribe', $body, $this->defaultHeaders)
        ->seeStatusCode(200)
        ->seeJsonContains([
            "results" => [[]]
        ]);
}

I test and it work prefect. Is it good way for creating test??

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

jeromegamez commented 3 years ago

Hey, and sorry for the late reply! I haven't really worked with Laravel's fakes in the past, so I understand the principle, but could you please explain a little more in detail how $fake = (new FirebaseFactory())->createMessaging() is a fake? For me it looks as if this still creates the Messaging component from an uncounfigured Factory (and so would still make "real" requests to the Firebase APIs... 🤔