Skip to content

Commit

Permalink
Merge pull request #4 from moufmouf/extend_service_not_found
Browse files Browse the repository at this point in the history
Extending a service that does not exist always fails
  • Loading branch information
moufmouf committed Nov 29, 2017
2 parents bce447a + e9e89ba commit 00ecab4
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 40 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -20,7 +20,7 @@
"phpstan/phpstan": "^0.8.5",
"thecodingmachine/phpstan-strict-rules": "^0.8.4",
"satooshi/php-coveralls": "^1.0.1",
"mnapoli/simplex": "^0.3",
"mnapoli/simplex": "^0.4.1",
"bamarni/composer-bin-plugin": "^1.1"
},
"autoload": {
Expand Down
3 changes: 3 additions & 0 deletions src/ExtensionDefinition.php
Expand Up @@ -60,6 +60,9 @@ public function buildExtensionCode(string $functionName) : string
} else {
$previousTypeCode = '\\'.(string) $previousType.' ';
}
if ($previousParameter->allowsNull()) {
$previousTypeCode = '?'.$previousTypeCode;
}
}
$previousParameterCode = ', '.$previousTypeCode.'$previous';
if ($previousParameter->isDefaultValueAvailable()) {
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceProvider.php
Expand Up @@ -321,7 +321,7 @@ private function buildTagsCode(string $tagMethodName, array $taggedServices): st

return sprintf(
<<<EOF
public static function %s(ContainerInterface \$container, ?\SplPriorityQueue \$queue): \SplPriorityQueue
public static function %s(ContainerInterface \$container, ?\SplPriorityQueue \$queue = null): \SplPriorityQueue
{
\$queue = \$queue ?: new \SplPriorityQueue();
%s
Expand Down
2 changes: 1 addition & 1 deletion tests/ExtensionDefinitionTest.php
Expand Up @@ -86,7 +86,7 @@ public function testBuildExtension2()
$code = $extensionDefinition->buildExtensionCode('foo');

$this->assertSame(<<<EOF
public static function foo(ContainerInterface \$container, \DateTimeInterface \$previous = NULL): \DateTimeInterface
public static function foo(ContainerInterface \$container, ?\DateTimeInterface \$previous = NULL): \DateTimeInterface
{
return \TheCodingMachine\Funky\Fixtures\TestServiceProvider::testExtension2(\$previous);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Fixtures/TestServiceProvider.php
Expand Up @@ -69,4 +69,14 @@ public static function taggedService3() : string
{
return 'bar';
}

/**
* @Extension(
* name="extendNonExistent"
* )
*/
public static function extendNotExistent(string $value = 'foo') : string
{
return $value;
}
}
13 changes: 11 additions & 2 deletions tests/ServiceProviderTest.php
Expand Up @@ -149,8 +149,7 @@ public function testTags()
$this->assertArrayHasKey('mytag2', $extensions);
$this->assertArrayHasKey('mytag3', $extensions);

$simplex = new Container();
$simplex->register($sp);
$simplex = new Container([$sp]);

$mytag1 = $simplex->get('mytag1');
/* @var $mytag1 \SplPriorityQueue */
Expand All @@ -160,4 +159,14 @@ public function testTags()
$this->assertSame('foo', $mytag1array[0]);
$this->assertSame('bar', $mytag1array[1]);
}

public function testExtendNonExistentService()
{
$sp = new TestServiceProvider();

$simplex = new Container([$sp]);

$value = $simplex->get('extendNonExistent');
$this->assertSame('foo', $value);
}
}
70 changes: 35 additions & 35 deletions vendor-bin/infection/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 00ecab4

Please sign in to comment.