Skip to content

Feat: Adds faking ability to facade#538

Merged
freekmurze merged 5 commits intospatie:mainfrom
chikondikamwendo:feat/add-faking-to-facade
Apr 28, 2025
Merged

Feat: Adds faking ability to facade#538
freekmurze merged 5 commits intospatie:mainfrom
chikondikamwendo:feat/add-faking-to-facade

Conversation

@chikondikamwendo
Copy link
Contributor

This PR adds the ability to fake internal method calls in the package from other projects.

Before

In a project using the package, it is hard to fake what the package does, which means project tests will be slow because they will have to rely on network calls which will have to hit the actual google servers. Its even harder on CI environments as you have to setup credentials for that too.

Unless there's something I'm missing, however I did look and was not able to find something helpful as in my use case.

After

This PR introduces a fake method on the Analytics facade, the fake method swaps the actual Analytics class with a fake class. The fake class uses the __call magic method to intercept all method calls intended for the actual Analytics instance.

The fake method also allows for an optional argument to be passed that will be returned whenever an interception has been made.

Usage example:

<?php

use Spatie\Analytics\Facades\Analaytics;

test('analytics page returns success response', function() {
    // Arrange
    Analytics::fake();

    // ....
});

With an expected result:

<?php

use Spatie\Analytics\Facades\Analaytics;

test('analytics page returns success response', function() {
    // Arrange
    Analytics::fake(collect([
        [
            'pageTitle' => 'Test Page',
            'activeUsers' => 10,
            'screenPageViews' => 20,
        ]
    ]));

    // ....
});

Further Improvements

As of now, during the interception, it just spits out what it gets, if this sounds like something you would allow to have in the package then I would have to complete the PR by making it so that its smarter about what it returns, specifically in regards to the relationship between the method called and the result to return.

@chikondikamwendo chikondikamwendo changed the title Feat/add faking to facade Feat: Adds faking ability to facade Apr 22, 2025
@freekmurze
Copy link
Member

The tests seem to fail, feel free to drop older Laravel / PHP versions

@chikondikamwendo
Copy link
Contributor Author

I will work on that.

@chikondikamwendo
Copy link
Contributor Author

@freekmurze, I figured what was causing issues was illuminate's Fake interface, so I just removed the dependency on it entirely as it was only there for testing and changed the corresponding assertions. I also updated the fake classes argument to be more general (mixed) as opposed to what it was before (array/collection), and then set the default to a collection.

I did not bother dropping the versions as you suggested, I believe this will work, instead I can just create a new PR specifically for that if you would still want it done.

@freekmurze freekmurze merged commit 2bcae67 into spatie:main Apr 28, 2025
19 checks passed
@freekmurze
Copy link
Member

Thanks!

@chikondikamwendo chikondikamwendo deleted the feat/add-faking-to-facade branch April 28, 2025 16:34
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