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

Allow to use Psr\Container\ContainerInterface in ->use callback #129

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ parameters:
path: src/Browser/Assertion/SameUrlAssertion.php

-
message: "#^Cannot call method getToken\\(\\) on object\\|null\\.$#"
message: "#^Call to an undefined method object\\:\\:getToken\\(\\)\\.$#"
count: 1
path: src/Browser/KernelBrowser.php

Expand Down
2 changes: 2 additions & 0 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Zenstruck;

use Behat\Mink\Element\NodeElement;
use Psr\Container\ContainerInterface;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\DomCrawler\Crawler;
Expand Down Expand Up @@ -494,6 +495,7 @@ protected function useParameters(): array
Parameter::typed(Crawler::class, Parameter::factory(fn() => $this->client()->getCrawler())),
Parameter::typed(CookieJar::class, Parameter::factory(fn() => $this->client()->getCookieJar())),
Parameter::typed(AbstractBrowser::class, Parameter::factory(fn() => $this->client())),
Parameter::typed(ContainerInterface::class, Parameter::factory(fn() => method_exists($this->client(), 'getContainer') ? $this->client()->getContainer() : null))->optional(),
];
}
}
16 changes: 16 additions & 0 deletions tests/KernelBrowserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Zenstruck\Browser\Tests;

use PHPUnit\Framework\AssertionFailedError;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
use Zenstruck\Assert;
use Zenstruck\Browser\HttpOptions;
Expand Down Expand Up @@ -39,6 +40,21 @@ public function can_use_kernel_browser_as_typehint(): void
;
}

/**
* @test
*/
public function can_use_container_as_typehint(): void
{
$browser = $this->browser();
$c=$browser->client()->getContainer();

$browser
->use(function(ContainerInterface $container) use ($c) {
$this->assertSame($c, $container);
})
;
}

/**
* @test
*/
Expand Down