Skip to content

Commit

Permalink
Merge branch 'inherit-auto-declare'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vegner committed Feb 14, 2018
2 parents 57d94f3 + 3ac414d commit 3556a9c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/ContainerAwareTrait.php
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;
}
}
15 changes: 15 additions & 0 deletions tests/ContainerAwareTraitTest.php
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');
}
}
12 changes: 12 additions & 0 deletions tests/ParentConsumer.php
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;
}
10 changes: 10 additions & 0 deletions tests/SubclassConsumer.php
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();
}
}

0 comments on commit 3556a9c

Please sign in to comment.