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

Improvements to window test doubles #426

Merged
merged 2 commits into from
Nov 22, 2024
Merged

Conversation

XbNz
Copy link
Contributor

@XbNz XbNz commented Nov 21, 2024

Continuing some work left from #422

@simonhamp there were some quirky HTTP client issues which I discovered once I took it for a spin in a real test suite for an app I'm working on. Fixed most of them, but we still need an HTTP fake to make it play nice. I was torn as to whether the test double should do the HTTP faking, or leave it up to the user. I opted for the latter. This can be documented.

Here's a general happy-path test snippet, if you'd like to update the docs with a testing section at some point:

 #[\PHPUnit\Framework\Attributes\Test]
public function it_opens_a_new_nativephp_ping_window(): void
{
    // Arrange
    Http::fake(); // Native PHP API URL should be provided to avoid faking everything else
    Window::fake();
    Window::alwaysReturnWindows([
        $mockWindow = Mockery::mock(WindowImplementation::class)->makePartial(),
    ]);

    $mockWindow->shouldReceive('route')->once()->with('ping')->andReturnSelf();
    $mockWindow->shouldReceive('showDevTools')->once()->with(false)->andReturnSelf();
    $mockWindow->shouldReceive('titleBarHiddenInset')->once()->andReturnSelf();
    $mockWindow->shouldReceive('transparent')->once()->andReturnSelf();
    $mockWindow->shouldReceive('height')->once()->with(500)->andReturnSelf();
    $mockWindow->shouldReceive('width')->once()->with(775)->andReturnSelf();
    $mockWindow->shouldReceive('minHeight')->once()->with(500)->andReturnSelf();
    $mockWindow->shouldReceive('minWidth')->once()->with(775)->andReturnSelf();

    // Act
    Livewire::test(Search::class)->call('openPing');

    // Assert
    Window::assertOpened(fn (string $windowId) => Str::startsWith($windowId, ['ping']));
}

The mocks are a little disgusting, so perhaps a test double for the window implementation – not just the manager – would be helpful in the future. I'll release more of these as I come across the use case.

- Add support for optionally passing closures to methods that perform value assertions
- Add count assertions
- Enhancements to FakeWindowManagerTest::class
- Resolve test environment error caused FakeWindowManager::open() not returning a Window/PendingOpenWindow object. This now mimics the real WindowManager::open() behavior
- Remove usage of PHPUnit assertions in FakeWindowManager::class for internal assertions. Now only used for final assertions. This change fixes issue where PHPUnit was reporting 2 assertions per ::assertX() call on the test double
Copy link
Member

@simonhamp simonhamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super awesome! Thank you

@simonhamp simonhamp merged commit a6dea1f into NativePHP:main Nov 22, 2024
21 checks passed
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