diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 2809162..38c6bd8 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 diff --git a/src/Browser.php b/src/Browser.php index 81e991d..544bf51 100644 --- a/src/Browser.php +++ b/src/Browser.php @@ -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; @@ -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(), ]; } } diff --git a/tests/KernelBrowserTests.php b/tests/KernelBrowserTests.php index bdcae69..9c6610d 100644 --- a/tests/KernelBrowserTests.php +++ b/tests/KernelBrowserTests.php @@ -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; @@ -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 */