Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finalize
StateResetter
, add more exceptions, add tests for 100% cov…
…erage (#282)
- Loading branch information
Showing
4 changed files
with
138 additions
and
5 deletions.
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
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
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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Di\Tests\Unit; | ||
|
||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
use Yiisoft\Di\StateResetter; | ||
use Yiisoft\Di\Tests\Support\Car; | ||
use Yiisoft\Test\Support\Container\SimpleContainer; | ||
|
||
final class StateResetterTest extends TestCase | ||
{ | ||
public function testNonStateResetterObject(): void | ||
{ | ||
$resetter = new StateResetter(new SimpleContainer()); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage( | ||
'State resetter object should be instance of "' . StateResetter::class . '", "stdClass" given.' | ||
); | ||
$resetter->setResetters([ | ||
new stdClass(), | ||
]); | ||
} | ||
|
||
public function testStateResetterObjectForService(): void | ||
{ | ||
$resetter = new StateResetter(new SimpleContainer()); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage( | ||
'Callback for state resetter should be closure in format ' . | ||
'`function (ContainerInterface $container): void`. ' . | ||
'Got "' . StateResetter::class . '".' | ||
); | ||
$resetter->setResetters([ | ||
Car::class => $resetter, | ||
]); | ||
} | ||
|
||
public function testResetNonObject(): void | ||
{ | ||
$resetter = new StateResetter( | ||
new SimpleContainer([ | ||
'value' => 42, | ||
]) | ||
); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage( | ||
'State resetter supports resetting objects only. Container returned integer.' | ||
); | ||
$resetter->setResetters([ | ||
'value' => static function () { | ||
}, | ||
]); | ||
} | ||
} |