-
Notifications
You must be signed in to change notification settings - Fork 0
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
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
<?php | ||
|
||
namespace SD\DependencyInjection; | ||
|
||
trait ContainerAwareTrait { | ||
private $autoDeclareContainer = 'container'; | ||
trait ContainerAwareTrait | ||
{ | ||
protected $autoDeclareContainer = 'container'; | ||
private $container; | ||
|
||
public function setContainer(Container $container) { | ||
public function setContainer(Container $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
private function getContainer(): Container { | ||
protected function getContainer(): Container | ||
{ | ||
return $this->container; | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
namespace tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SD\DependencyInjection\Container; | ||
|
||
class ContainerAwareTraitTest extends TestCase | ||
{ | ||
public function testInheritAutoDeclare() | ||
{ | ||
$container = new Container(); | ||
$subclassConsumer = $container->produce(SubclassConsumer::class); | ||
$this->assertSame($container, $subclassConsumer->getService(), 'Subclass must inherit auto declare trait'); | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
namespace tests; | ||
|
||
use SD\DependencyInjection\AutoDeclarerInterface; | ||
use SD\DependencyInjection\AutoDeclarerTrait; | ||
use SD\DependencyInjection\ContainerAwareTrait; | ||
|
||
class ParentConsumer implements AutoDeclarerInterface | ||
{ | ||
use AutoDeclarerTrait; | ||
use ContainerAwareTrait; | ||
} |
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,10 @@ | ||
<?php | ||
namespace tests; | ||
|
||
class SubclassConsumer extends ParentConsumer | ||
{ | ||
public function getService() | ||
{ | ||
return $this->getContainer(); | ||
} | ||
} |