diff --git a/More/Delegation/JuniorDeveloper.php b/More/Delegation/JuniorDeveloper.php index dbaa30a..c8da3ee 100644 --- a/More/Delegation/JuniorDeveloper.php +++ b/More/Delegation/JuniorDeveloper.php @@ -8,4 +8,12 @@ class JuniorDeveloper { 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(); + } } diff --git a/More/Delegation/TeamLead.php b/More/Delegation/TeamLead.php index 67d9bdf..8a515e6 100644 --- a/More/Delegation/TeamLead.php +++ b/More/Delegation/TeamLead.php @@ -21,4 +21,18 @@ class TeamLead { 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...'; + } } diff --git a/More/Delegation/Tests/DelegationTest.php b/More/Delegation/Tests/DelegationTest.php index 888555a..0dd8083 100644 --- a/More/Delegation/Tests/DelegationTest.php +++ b/More/Delegation/Tests/DelegationTest.php @@ -7,11 +7,19 @@ use PHPUnit\Framework\TestCase; class DelegationTest extends TestCase { - public function testHowTeamLeadWriteCode() + public function testTeamLeadCanBlameJuniorForBadCode() { $junior = new Delegation\JuniorDeveloper(); $teamLead = new Delegation\TeamLead($junior); $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()); + } }