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

Fix autowiring null #1081

Merged
merged 6 commits into from Aug 6, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Fixed
* [#1081](https://github.com/Behat/Behat/pull/1081): Do not use isset to be
able to check for intentional `null` values when autowiring contexts

### Added
* [#1144](https://github.com/Behat/Behat/pull/1144) : Support for arrays as context parameters
Expand Down
35 changes: 35 additions & 0 deletions features/autowire.feature
Expand Up @@ -106,6 +106,41 @@ Feature: Helper services autowire
When I run "behat --no-colors -f progress features/autowire.feature"
Then it should pass

Scenario: Null arguments should be skipped
Given a file named "behat.yaml" with:
"""
default:
suites:
default:
contexts:
- FeatureContext:
name: null
services: ServiceContainer
autowire: true
"""
And a file named "features/autowire.feature" with:
"""
Feature:
Scenario:
Given a step
"""
And a file named "features/bootstrap/FeatureContext.php" with:
"""
<?php use Behat\Behat\Context\Context;

class FeatureContext implements Context {
public function __construct($name, Service1 $s1, Service3 $s3)
{
PHPUnit\Framework\Assert::assertNull($name);
}

/** @Given a step */
public function aStep() {}
}
"""
When I run "behat --no-colors -f progress features/autowire.feature"
Then it should pass

Scenario: Unregistered services as constructor arguments
Given a file named "features/autowire.feature" with:
"""
Expand Down
15 changes: 14 additions & 1 deletion src/Behat/Behat/HelperContainer/ArgumentAutowirer.php
Expand Up @@ -14,6 +14,7 @@
use Psr\Container\ContainerInterface;
use ReflectionFunctionAbstract;
use ReflectionParameter;
use ReflectionException;

/**
* Automatically wires arguments of a given function from inside the container by using type-hitns.
Expand Down Expand Up @@ -72,6 +73,18 @@ public function autowireArguments(ReflectionFunctionAbstract $reflection, array
*/
private function isArgumentWireable(array $arguments, $index, ReflectionParameter $parameter)
{
return !isset($arguments[$index]) && !isset($arguments[$parameter->getName()]) && $parameter->getClass();
if (isset($arguments[$index]) || array_key_exists($index, $arguments)) {
return false;
}

if (isset($arguments[$parameter->getName()]) || array_key_exists($parameter->getName(), $arguments)) {
return false;
}

try {
return (bool) $parameter->getClass();
} catch (ReflectionException $e) {
return false;
}
}
}
7 changes: 6 additions & 1 deletion src/Behat/Testwork/Argument/MixedArgumentOrganiser.php
Expand Up @@ -13,6 +13,7 @@
use ReflectionFunctionAbstract;
use ReflectionClass;
use ReflectionParameter;
use ReflectionException;

/**
* Organises function arguments using its reflection.
Expand Down Expand Up @@ -225,7 +226,11 @@ private function filterApplicableTypehintedParameters(array $parameters)
continue;
}

$reflectionClass = $parameter->getClass();
try {
$reflectionClass = $parameter->getClass();
} catch (ReflectionException $e) {
continue;
}

if (!$reflectionClass) {
continue;
Expand Down