Last active
August 10, 2018 09:01
-
-
Save recca0120/47dc37bea630074678f2f7776d3ddaa1 to your computer and use it in GitHub Desktop.
聖佑愛吃蛋
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class BadEgg implements Egg | |
| { | |
| public function eaten() | |
| { | |
| return 'Unsatisfy'; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| interface Egg | |
| { | |
| public function eaten(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Shengyoufan | |
| { | |
| public $stomach = []; | |
| public function eat(Egg $egg) | |
| { | |
| $this->stomach[] = $egg->eaten(); | |
| return $this; | |
| } | |
| public function use(Toilet $toilet) | |
| { | |
| foreach ($this->stomach as $index => $feeling) { | |
| if ($feeling === 'Unsatisfy') { | |
| $toilet->occupy(); | |
| unset($this->stomach[$index]); | |
| } | |
| } | |
| return $this; | |
| } | |
| public function feeling() | |
| { | |
| return count(array_filter($this->stomach, function ($feeling) { | |
| return $feeling === 'Unsatisfy'; | |
| })) > 0 ? 'bad' : 'good'; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Mockery as m; | |
| use PHPUnit\Framework\TestCase; | |
| use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | |
| class ShengyoufanTest extends TestCase | |
| { | |
| use MockeryPHPUnitIntegration; | |
| public function test_shengyoufan_eat_tea_egg_then_feel_satisfy() | |
| { | |
| $egg = m::mock(new TeaEgg()); | |
| $egg->shouldReceive('eaten')->times(10)->passthru(); | |
| $shengyoufan = new Shengyoufan(); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $counts = array_count_values($shengyoufan->stomach); | |
| $this->assertEquals(10, $counts['Satisfy']); | |
| $this->assertSame('good', $shengyoufan->feeling()); | |
| return $shengyoufan; | |
| } | |
| /** | |
| * @depends test_shengyoufan_eat_tea_egg_then_feel_satisfy | |
| */ | |
| public function test_shengyoufan_should_not_occupy_toile(Shengyoufan $shengyoufan) | |
| { | |
| $toilet = m::mock(new Toilet()); | |
| $toilet->shouldReceive('occupy')->times(0)->passthru(); | |
| $shengyoufan->use($toilet); | |
| $this->assertSame('good', $shengyoufan->feeling()); | |
| } | |
| public function test_shengyoufan_eat_bad_egg_then_feel_unsatisfy() | |
| { | |
| $egg = m::mock(new BadEgg()); | |
| $egg->shouldReceive('eaten')->times(10)->passthru(); | |
| $shengyoufan = new Shengyoufan(); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $shengyoufan->eat($egg); | |
| $counts = array_count_values($shengyoufan->stomach); | |
| $this->assertEquals(10, $counts['Unsatisfy']); | |
| $this->assertSame('bad', $shengyoufan->feeling()); | |
| return $shengyoufan; | |
| } | |
| /** | |
| * @depends test_shengyoufan_eat_bad_egg_then_feel_unsatisfy | |
| */ | |
| public function test_shengyoufan_occupy_toilet(Shengyoufan $shengyoufan) | |
| { | |
| $toilet = m::mock(new Toilet()); | |
| $toilet->shouldReceive('occupy')->times(10)->passthru(); | |
| $shengyoufan->use($toilet); | |
| $this->assertSame('good', $shengyoufan->feeling()); | |
| } | |
| public function test_shengyoufan_eat_egg_then_feel_satisfy_or_not() | |
| { | |
| $teaEgg = m::mock(new TeaEgg()); | |
| $teaEgg->shouldReceive('eaten')->times(5)->passthru(); | |
| $badEgg = m::mock(new BadEgg()); | |
| $badEgg->shouldReceive('eaten')->times(5)->passthru(); | |
| $shengyoufan = new Shengyoufan(); | |
| $shengyoufan->eat($teaEgg); | |
| $shengyoufan->eat($teaEgg); | |
| $shengyoufan->eat($teaEgg); | |
| $shengyoufan->eat($teaEgg); | |
| $shengyoufan->eat($teaEgg); | |
| $shengyoufan->eat($badEgg); | |
| $shengyoufan->eat($badEgg); | |
| $shengyoufan->eat($badEgg); | |
| $shengyoufan->eat($badEgg); | |
| $shengyoufan->eat($badEgg); | |
| $counts = array_count_values($shengyoufan->stomach); | |
| $this->assertEquals(5, $counts['Satisfy']); | |
| $this->assertEquals(5, $counts['Unsatisfy']); | |
| $this->assertSame('bad', $shengyoufan->feeling()); | |
| return $shengyoufan; | |
| } | |
| /** | |
| * @depends test_shengyoufan_eat_egg_then_feel_satisfy_or_not | |
| */ | |
| public function test_shengyoufan_should_occupy_toilet_when_stomach_has_bad_eggs(Shengyoufan $shengyoufan) | |
| { | |
| $toilet = m::mock(new Toilet()); | |
| $toilet->shouldReceive('occupy')->times(5)->passthru(); | |
| $shengyoufan->use($toilet); | |
| $this->assertSame('good', $shengyoufan->feeling()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class TeaEgg implements Egg | |
| { | |
| public function eaten() | |
| { | |
| return 'Satisfy'; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Toilet | |
| { | |
| public function occupy() | |
| { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment