From 4405596ca1068cbad56c5137a1bf057006ec3b75 Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Thu, 10 Jan 2019 00:10:27 +0100 Subject: [PATCH] Make PHPStan passing --- phpstan.neon | 3 ++ .../ContextServiceEnvironmentHandler.php | 28 +++++++++++++------ src/Driver/SymfonyDriver.php | 13 ++++++++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 6802f22..d944ec3 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,4 +2,7 @@ parameters: reportUnmatchedIgnoredErrors: false ignoreErrors: + - '/Cannot access offset 0 on callable\./' + - '/Cannot access offset 1 on callable\./' + - '/Method FriendsOfBehat\\SymfonyExtension\\Context\\Environment\\InitialisedContextServiceEnvironment::bindCallee\(\) should return callable/' - '/Cannot call method [a-zA-Z0-9]+\(\) on Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface|null\./' diff --git a/src/Context/Environment/Handler/ContextServiceEnvironmentHandler.php b/src/Context/Environment/Handler/ContextServiceEnvironmentHandler.php index d7e5d35..8792d85 100644 --- a/src/Context/Environment/Handler/ContextServiceEnvironmentHandler.php +++ b/src/Context/Environment/Handler/ContextServiceEnvironmentHandler.php @@ -60,11 +60,12 @@ final class ContextServiceEnvironmentHandler implements EnvironmentHandler } /** + * @param UninitialisedContextServiceEnvironment $uninitializedEnvironment + * * @throws EnvironmentIsolationException */ public function isolateEnvironment(Environment $uninitializedEnvironment, $testSubject = null): Environment { - /** @var UninitialisedContextServiceEnvironment $uninitializedEnvironment */ $this->assertEnvironmentCanBeIsolated($uninitializedEnvironment, $testSubject); $environment = new InitialisedContextServiceEnvironment($uninitializedEnvironment->getSuite()); @@ -136,22 +137,31 @@ final class ContextServiceEnvironmentHandler implements EnvironmentHandler return $class; } - throw new \Exception('wtf?'); + throw new \DomainException(sprintf('There is no service or class "%s".', $contextId)); } private function getContext(string $contextId): Context { - if ($this->getContainer()->has($contextId)) { - return $this->getContainer()->get($contextId); - } - $class = '\\' . ltrim($contextId, '\\'); - if (class_exists($class)) { - return new $class(); + if ($this->getContainer()->has($contextId)) { + $context = $this->getContainer()->get($contextId); + } elseif (class_exists($class)) { + $context = new $class(); + } else { + throw new \DomainException(sprintf('There is no service or class "%s".', $contextId)); } - throw new \Exception('wtf?'); + if (!$context instanceof Context) { + throw new \DomainException(sprintf( + 'Context "%s" referenced as "%s" needs to implement "%s".', + get_class($context), + $contextId, + Context::class + )); + } + + return $context; } private function getContainer(): ContainerInterface diff --git a/src/Driver/SymfonyDriver.php b/src/Driver/SymfonyDriver.php index a44c6de..dbbc18a 100644 --- a/src/Driver/SymfonyDriver.php +++ b/src/Driver/SymfonyDriver.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace FriendsOfBehat\SymfonyExtension\Driver; use Behat\Mink\Driver\BrowserKitDriver; +use Symfony\Component\BrowserKit\Client; use Symfony\Component\HttpKernel\KernelInterface; final class SymfonyDriver extends BrowserKitDriver @@ -21,6 +22,16 @@ final class SymfonyDriver extends BrowserKitDriver )); } - parent::__construct($kernel->getContainer()->get('test.client'), $baseUrl); + $testClient = $kernel->getContainer()->get('test.client'); + + if (!$testClient instanceof Client) { + throw new \RuntimeException(sprintf( + 'Service "test.client" should be an instance of "%s", "%s" given.', + Client::class, + get_class($testClient) + )); + } + + parent::__construct($testClient, $baseUrl); } }