-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/** | ||
* @phpVersion 8.0 | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\DI; | ||
use Nette\DI\Definitions\Statement; | ||
use Tester\Assert; | ||
|
||
|
||
class Factory | ||
{ | ||
public function createUnion(): stdClass|array | ||
{ | ||
return []; | ||
} | ||
} | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
Assert::exception(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createUnion']), 'next']); | ||
$container = createContainer($builder); | ||
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createUnion() is expected to not be nullable/built-in/complex, 'stdClass|array' given."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
<?php | ||
|
||
/** | ||
* Test of chained resolving | ||
* | ||
* services: | ||
* - Factory::createClass()::next() | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\DI; | ||
use Nette\DI\Definitions\Statement; | ||
use Tester\Assert; | ||
|
||
|
||
class Lorem | ||
{ | ||
public function next(): stdClass | ||
{ | ||
} | ||
} | ||
|
||
|
||
class Factory | ||
{ | ||
/** @return Lorem */ | ||
public function createClassPhpDoc() | ||
{ | ||
return []; | ||
} | ||
|
||
|
||
public function createClass(): Lorem | ||
{ | ||
return []; | ||
} | ||
|
||
|
||
/** @return Lorem|null */ | ||
public function createNullableClassPhpDoc() | ||
{ | ||
return []; | ||
} | ||
|
||
|
||
public function createNullableClass(): ?Lorem | ||
{ | ||
return []; | ||
} | ||
|
||
|
||
/** @return array */ | ||
public function createScalarPhpDoc() | ||
{ | ||
return []; | ||
} | ||
|
||
|
||
public function createScalar(): array | ||
{ | ||
return []; | ||
} | ||
|
||
|
||
/** @return object */ | ||
public function createObjectPhpDoc() | ||
{ | ||
return (object) null; | ||
} | ||
|
||
|
||
public function createObject(): object | ||
{ | ||
return (object) null; | ||
} | ||
|
||
|
||
public function createObjectNullable(): ?object | ||
{ | ||
return (object) null; | ||
} | ||
|
||
|
||
/** @return mixed */ | ||
public function createMixedPhpDoc() | ||
{ | ||
return (object) null; | ||
} | ||
|
||
|
||
public function createMixed(): mixed | ||
{ | ||
return (object) null; | ||
} | ||
|
||
|
||
/** | ||
* @template T | ||
* @return T | ||
*/ | ||
public function createGeneric() | ||
{ | ||
return (object) null; | ||
} | ||
} | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
Assert::noError(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createClassPhpDoc']), 'next']); | ||
$container = @createContainer($builder); // @return is deprecated | ||
}); | ||
|
||
Assert::noError(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createClass']), 'next']); | ||
$container = createContainer($builder); | ||
}); | ||
|
||
Assert::noError(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createNullableClassPhpDoc']), 'next']); | ||
$container = @createContainer($builder); // @return is deprecated | ||
}); | ||
|
||
Assert::exception(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createNullableClass']), 'next']); | ||
$container = createContainer($builder); | ||
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createNullableClass() is expected to not be nullable/built-in/complex, '?Lorem' given."); | ||
|
||
Assert::exception(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createScalarPhpDoc']), 'next']); | ||
$container = @createContainer($builder); // @return is deprecated | ||
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createScalarPhpDoc() is expected to not be nullable/built-in/complex, 'array' given."); | ||
|
||
Assert::exception(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createScalar']), 'next']); | ||
$container = createContainer($builder); | ||
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createScalar() is expected to not be nullable/built-in/complex, 'array' given."); | ||
|
||
Assert::exception(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createObjectPhpDoc']), 'next']); | ||
$container = createContainer($builder); | ||
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method."); | ||
|
||
Assert::exception(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createObject']), 'next']); | ||
$container = createContainer($builder); | ||
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method."); | ||
|
||
Assert::exception(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createObjectNullable']), 'next']); | ||
$container = createContainer($builder); | ||
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method."); | ||
|
||
Assert::exception(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createMixedPhpDoc']), 'next']); | ||
$container = createContainer($builder); | ||
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method."); | ||
|
||
Assert::exception(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createMixed']), 'next']); | ||
$container = createContainer($builder); | ||
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method."); | ||
|
||
Assert::exception(function () { | ||
$builder = new DI\ContainerBuilder; | ||
$builder->addDefinition('a') | ||
->setFactory([new Statement([Factory::class, 'createGeneric']), 'next']); | ||
$container = @createContainer($builder); // @return is deprecated | ||
}, Nette\DI\ServiceCreationException::class, "Service 'a': Class 'T' not found. | ||
Check the return type of Factory::createGeneric()."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ search: | |
third: | ||
in: fixtures | ||
files: foo* | ||
files: Foo* | ||
tags: | ||
- foo | ||
'); | ||
|