diff --git a/More/Delegation/JuniorDeveloper.php b/More/Delegation/JuniorDeveloper.php index dbaa30a..22da577 100644 --- a/More/Delegation/JuniorDeveloper.php +++ b/More/Delegation/JuniorDeveloper.php @@ -8,4 +8,11 @@ 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..90c04d3 100644 --- a/More/Delegation/TeamLead.php +++ b/More/Delegation/TeamLead.php @@ -21,4 +21,16 @@ 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..b521240 100644 --- a/More/Delegation/Tests/DelegationTest.php +++ b/More/Delegation/Tests/DelegationTest.php @@ -7,11 +7,24 @@ use PHPUnit\Framework\TestCase; class DelegationTest extends TestCase { - public function testHowTeamLeadWriteCode() + /** + * @test + */ + public function teamLeadCanBlameJuniorForBadCode() { $junior = new Delegation\JuniorDeveloper(); $teamLead = new Delegation\TeamLead($junior); $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()); + } }