|
6 | 6 |
|
7 | 7 | use Gacela\Framework\Container\Container; |
8 | 8 | use Gacela\Framework\Container\Locator; |
| 9 | +use Gacela\Framework\Exception\ServiceNotFoundException; |
9 | 10 | use GacelaTest\Fixtures\StringValue; |
10 | 11 | use PHPUnit\Framework\TestCase; |
11 | 12 |
|
@@ -55,4 +56,49 @@ public function test_get_existing_singleton_from_container(): void |
55 | 56 |
|
56 | 57 | self::assertEquals(new StringValue('str'), $singleton); |
57 | 58 | } |
| 59 | + |
| 60 | + public function test_get_required_throws_when_not_found(): void |
| 61 | + { |
| 62 | + $this->expectException(ServiceNotFoundException::class); |
| 63 | + $this->expectExceptionMessage('Service "GacelaTest\Unit\Framework\Container\NonExisting" not found in the container.'); |
| 64 | + |
| 65 | + Locator::getInstance()->getRequired(NonExisting::class); |
| 66 | + } |
| 67 | + |
| 68 | + public function test_get_required_singleton_throws_when_not_found(): void |
| 69 | + { |
| 70 | + $this->expectException(ServiceNotFoundException::class); |
| 71 | + $this->expectExceptionMessage('Service "GacelaTest\Unit\Framework\Container\NonExisting" not found in the container.'); |
| 72 | + |
| 73 | + Locator::getRequiredSingleton(NonExisting::class); |
| 74 | + } |
| 75 | + |
| 76 | + public function test_get_required_returns_existing_service(): void |
| 77 | + { |
| 78 | + Locator::addSingleton(StringValue::class, new StringValue('required')); |
| 79 | + |
| 80 | + $singleton = Locator::getInstance()->getRequired(StringValue::class); |
| 81 | + |
| 82 | + self::assertEquals(new StringValue('required'), $singleton); |
| 83 | + } |
| 84 | + |
| 85 | + public function test_get_required_singleton_returns_existing_service(): void |
| 86 | + { |
| 87 | + Locator::addSingleton(StringValue::class, new StringValue('required')); |
| 88 | + |
| 89 | + $singleton = Locator::getRequiredSingleton(StringValue::class); |
| 90 | + |
| 91 | + self::assertEquals(new StringValue('required'), $singleton); |
| 92 | + } |
| 93 | + |
| 94 | + public function test_get_required_singleton_from_container(): void |
| 95 | + { |
| 96 | + $container = new Container(bindings: [ |
| 97 | + StringValue::class => new StringValue('from-container'), |
| 98 | + ]); |
| 99 | + |
| 100 | + $singleton = Locator::getRequiredSingleton(StringValue::class, $container); |
| 101 | + |
| 102 | + self::assertEquals(new StringValue('from-container'), $singleton); |
| 103 | + } |
58 | 104 | } |
0 commit comments