Skip to content

Commit

Permalink
Ensured DottedAccessConfigAbstractFactory resolves null values when a…
Browse files Browse the repository at this point in the history
…ll steps in key are found
  • Loading branch information
acelaya committed Dec 10, 2020
1 parent 78c947a commit 13883e3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ cache:

jobs:
fast_finish: true
allow_failures:
- php: 'nightly'
include:
- name: "CI - 8.0"
php: 'nightly'
php: '8.0'
env:
- COMPOSER_FLAGS='--ignore-platform-req=php'
- name: "CI - 7.4"
php: '7.4'
env:
- COMPOSER_FLAGS=''

before_install:
- phpenv config-rm xdebug.ini || return 0

install:
- composer self-update
- composer install --no-interaction --prefer-dist
- composer install --no-interaction --prefer-dist $COMPOSER_FLAGS

script:
- mkdir build
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"laminas/laminas-servicemanager": "^3.4"
},
"require-dev": {
"infection/infection": "^0.19.2",
"infection/infection": "^0.20",
"phpstan/phpstan": "^0.12.52",
"phpunit/phpunit": "^9.4",
"roave/security-advisories": "dev-master",
Expand Down
3 changes: 2 additions & 1 deletion src/Factory/DottedAccessConfigAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Laminas\ServiceManager\Factory\AbstractFactoryInterface;
use Shlinkio\Shlink\Config\Exception\InvalidArgumentException;

use function array_key_exists;
use function array_shift;
use function explode;
use function is_array;
Expand Down Expand Up @@ -57,7 +58,7 @@ private function readKeysFromArray(array $keys, $array)
$key = array_shift($keys);

// As soon as one of the provided keys is not found, throw an exception
if (! isset($array[$key])) {
if (! array_key_exists($key, $array)) {
throw new InvalidArgumentException(sprintf(
'The key "%s" provided in the dotted notation could not be found in the array service',
$key,
Expand Down
26 changes: 19 additions & 7 deletions test/Factory/DottedAccessConfigAbstractFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,32 @@ public function throwsExceptionWhenFirstPartOfTheServiceIsNotRegistered(): void
$this->factory->__invoke(new ServiceManager(), 'foo.bar');
}

/** @test */
public function dottedNotationIsRecursivelyResolvedUntilLastValueIsFoundAndReturned(): void
{
$expected = 'this is the result';

/**
* @test
* @dataProvider provideDotValues
*/
public function dottedNotationIsRecursivelyResolvedUntilLastValueIsFoundAndReturned(
string $serviceName,
?string $expected
): void {
$result = $this->factory->__invoke(new ServiceManager(['services' => [
'foo' => [
'bar' => ['baz' => $expected],
'bar' => [
'baz' => 'this is the result',
'nullable_baz' => null,
],
],
]]), 'foo.bar.baz');
]]), $serviceName);

$this->assertEquals($expected, $result);
}

public function provideDotValues(): iterable
{
yield 'non-null value' => ['foo.bar.baz', 'this is the result'];
yield 'null value' => ['foo.bar.nullable_baz', null];
}

/** @test */
public function exceptionIsThrownIfAnyStepCannotBeResolved(): void
{
Expand Down

0 comments on commit 13883e3

Please sign in to comment.