Skip to content

Commit 03a1d35

Browse files
committed
fix: issue on ContainerAware when theres no Container instance assigned
Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
1 parent af32689 commit 03a1d35

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/Container/ContainerAware.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
trait ContainerAware
88
{
99
/**
10-
* @var ContainerInterface
10+
* @var null|ContainerInterface
1111
*/
1212
protected $container = null;
1313

@@ -22,9 +22,9 @@ public function setContainer(ContainerInterface $container): void
2222
/**
2323
* @see ContainerAwareInterface::getContainer()
2424
*/
25-
public function getContainer(?string $name = null)
25+
public function getContainer(?string $name = null): ?object
2626
{
27-
if ($name) {
27+
if ($this->container && $name) {
2828
return $this->container->get($name);
2929
}
3030

src/Container/ContainerAwareInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ public function setContainer(ContainerInterface $container): void;
4444
* @param null|string $name Optionally pass a container name, if needed.
4545
* @return null|mixed|ContainerInterface
4646
*/
47-
public function getContainer(?string $name = null);
47+
public function getContainer(?string $name = null): ?object;
4848
}

test/spec/ContainerAware.spec.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Projek\Container;
4+
use Projek\Container\{ContainerAware, ContainerAwareInterface, ContainerInterface};
5+
6+
use function Kahlan\{describe, expect, given, it};
7+
8+
describe(ContainerAware::class, function () {
9+
given('container', function () {
10+
return new Container;
11+
});
12+
13+
given('stub', function () {
14+
return new class implements ContainerAwareInterface {
15+
use ContainerAware;
16+
};
17+
});
18+
19+
it('should returns null if no container assigned', function () {
20+
expect($this->stub->getContainer())->toBeNull();
21+
});
22+
23+
it('should assign container', function () {
24+
$this->stub->setContainer($this->container);
25+
26+
expect($this->stub->getContainer())->toBeAnInstanceOf(ContainerInterface::class);
27+
});
28+
});

0 commit comments

Comments
 (0)