enhance delegation example

This commit is contained in:
Miroslav Trninic 2018-04-29 15:55:54 +02:00
parent 0ea1593c1d
commit b4599ec67c
3 changed files with 33 additions and 1 deletions

View File

@ -8,4 +8,11 @@ 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,16 @@ 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,24 @@ use PHPUnit\Framework\TestCase;
class DelegationTest extends TestCase class DelegationTest extends TestCase
{ {
public function testHowTeamLeadWriteCode() /**
* @test
*/
public function teamLeadCanBlameJuniorForBadCode()
{ {
$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());
} }
/**
* @test
*/
public function teamLeadCanWriteBadCode()
{
$junior = new Delegation\JuniorDeveloper();
$teamLead = new Delegation\TeamLead($junior);
$this->assertEquals($junior->writeReallyBadCode($teamLead), $teamLead->writeBadCode());
}
} }