Merge pull request #322 from carousel/master

enhance delegation example
This commit is contained in:
Dominik Liebler
2018-05-17 07:55:37 +02:00
committed by GitHub
3 changed files with 31 additions and 1 deletions

View File

@@ -8,4 +8,12 @@ class JuniorDeveloper
{ {
return 'Some junior developer generated code...'; return 'Some junior developer generated code...';
} }
/**
* Junior is authorized to call method on TeamLead (real delegation)
*/
public function writeReallyBadCode(TeamLead $teamLead): string
{
return $teamLead->writeReallyBadCode();
}
} }

View File

@@ -21,4 +21,18 @@ class TeamLead
{ {
return $this->junior->writeBadCode(); return $this->junior->writeBadCode();
} }
public function writeBadCode(): string
{
//note that we are passing $this from teamLead context
return $this->junior->writeReallyBadCode($this);
}
/**
* Junior can call this method
*/
public function writeReallyBadCode(): string
{
return 'Even team lead can write bad code...';
}
} }

View File

@@ -7,11 +7,19 @@ use PHPUnit\Framework\TestCase;
class DelegationTest extends TestCase class DelegationTest extends TestCase
{ {
public function testHowTeamLeadWriteCode() public function testTeamLeadCanBlameJuniorForBadCode()
{ {
$junior = new Delegation\JuniorDeveloper(); $junior = new Delegation\JuniorDeveloper();
$teamLead = new Delegation\TeamLead($junior); $teamLead = new Delegation\TeamLead($junior);
$this->assertEquals($junior->writeBadCode(), $teamLead->writeCode()); $this->assertEquals($junior->writeBadCode(), $teamLead->writeCode());
} }
public function testTeamLeadCanWriteBadCode()
{
$junior = new Delegation\JuniorDeveloper();
$teamLead = new Delegation\TeamLead($junior);
$this->assertEquals($junior->writeReallyBadCode($teamLead), $teamLead->writeBadCode());
}
} }