From b4599ec67c53b405a79e6695e32643a6a2405004 Mon Sep 17 00:00:00 2001 From: Miroslav Trninic Date: Sun, 29 Apr 2018 15:55:54 +0200 Subject: [PATCH 1/3] enhance delegation example --- More/Delegation/JuniorDeveloper.php | 7 +++++++ More/Delegation/TeamLead.php | 12 ++++++++++++ More/Delegation/Tests/DelegationTest.php | 15 ++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) 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()); + } } From 63220c73c63ec1ac969892244c18e8bcf6f153b8 Mon Sep 17 00:00:00 2001 From: Miroslav Trninic Date: Mon, 30 Apr 2018 21:46:36 +0200 Subject: [PATCH 2/3] Fix formatting (new line before comment) --- More/Delegation/JuniorDeveloper.php | 1 + More/Delegation/TeamLead.php | 1 + More/Delegation/Tests/DelegationTest.php | 1 + 3 files changed, 3 insertions(+) diff --git a/More/Delegation/JuniorDeveloper.php b/More/Delegation/JuniorDeveloper.php index 22da577..c8da3ee 100644 --- a/More/Delegation/JuniorDeveloper.php +++ b/More/Delegation/JuniorDeveloper.php @@ -8,6 +8,7 @@ class JuniorDeveloper { return 'Some junior developer generated code...'; } + /** * Junior is authorized to call method on TeamLead (real delegation) */ diff --git a/More/Delegation/TeamLead.php b/More/Delegation/TeamLead.php index 90c04d3..1b990a1 100644 --- a/More/Delegation/TeamLead.php +++ b/More/Delegation/TeamLead.php @@ -26,6 +26,7 @@ class TeamLead //note that we are passing $this from teamLead context return $this->junior->writeReallyBadCode($this); } + /** * Junior can call this method */ diff --git a/More/Delegation/Tests/DelegationTest.php b/More/Delegation/Tests/DelegationTest.php index b521240..fc48048 100644 --- a/More/Delegation/Tests/DelegationTest.php +++ b/More/Delegation/Tests/DelegationTest.php @@ -17,6 +17,7 @@ class DelegationTest extends TestCase $this->assertEquals($junior->writeBadCode(), $teamLead->writeCode()); } + /** * @test */ From bb22ef0e0f16eda340880e2cd02be80bc78927a6 Mon Sep 17 00:00:00 2001 From: Miroslav Trninic Date: Sun, 13 May 2018 19:19:53 +0200 Subject: [PATCH 3/3] fix test annotation and one line separation issues --- More/Delegation/TeamLead.php | 1 + More/Delegation/Tests/DelegationTest.php | 10 ++-------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/More/Delegation/TeamLead.php b/More/Delegation/TeamLead.php index 1b990a1..8a515e6 100644 --- a/More/Delegation/TeamLead.php +++ b/More/Delegation/TeamLead.php @@ -21,6 +21,7 @@ class TeamLead { return $this->junior->writeBadCode(); } + public function writeBadCode(): string { //note that we are passing $this from teamLead context diff --git a/More/Delegation/Tests/DelegationTest.php b/More/Delegation/Tests/DelegationTest.php index fc48048..0dd8083 100644 --- a/More/Delegation/Tests/DelegationTest.php +++ b/More/Delegation/Tests/DelegationTest.php @@ -7,10 +7,7 @@ use PHPUnit\Framework\TestCase; class DelegationTest extends TestCase { - /** - * @test - */ - public function teamLeadCanBlameJuniorForBadCode() + public function testTeamLeadCanBlameJuniorForBadCode() { $junior = new Delegation\JuniorDeveloper(); $teamLead = new Delegation\TeamLead($junior); @@ -18,10 +15,7 @@ class DelegationTest extends TestCase $this->assertEquals($junior->writeBadCode(), $teamLead->writeCode()); } - /** - * @test - */ - public function teamLeadCanWriteBadCode() + public function testTeamLeadCanWriteBadCode() { $junior = new Delegation\JuniorDeveloper(); $teamLead = new Delegation\TeamLead($junior);