Created
September 30, 2015 08:39
-
-
Save tcz/4015f9d212cdcd5f68ab to your computer and use it in GitHub Desktop.
This file contains 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 DrinkingGame | |
{ | |
const TAIL = 'tail'; | |
const DRUNKEST = 'zoltan'; | |
public function __construct(array $names, Coin $coin = null) { | |
$this->names = array_fill_keys($names, 0); | |
$this->coin = $coin; | |
} | |
public function getDrinkList($tosses) | |
{ | |
for ($i = 0; $i < $tosses; $i++) { | |
$coinflips = $this->coin->toss(); | |
if (self::TAIL === $coinflips[0] && self::TAIL === $coinflips[1]) { | |
foreach ($this->names as $name => $count) { | |
$this->names[$name]++; | |
} | |
} else if ($coinflips[0] !== $coinflips[1]) { | |
$this->names[self::DRUNKEST]++; | |
} | |
} | |
return $this->names; | |
} | |
} |
This file contains 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 | |
require_once "DrinkingGame.php"; | |
class DrinkingGameTest extends \PHPUnit_Framework_TestCase | |
{ | |
private $coin; | |
public function setUp() | |
{ | |
$this->coin = $this->getMock('Coin', ['toss']); | |
} | |
public function testNoTossesAndNoPeopleGetEmptyList() { | |
$drinkingGame = $this->getDrinkingGame([]); | |
$drinkList = $drinkingGame->getDrinkList(0); | |
$this->assertEquals([], $drinkList); | |
} | |
public function testWithPeopleButNoTossesGetsPeopleListWithZeroDrinks() | |
{ | |
$drinkingGame = $this->getDrinkingGame(['lassi']); | |
$drinkList = $drinkingGame->getDrinkList(0); | |
$this->assertEquals(['lassi' => 0], $drinkList); | |
} | |
public function testTwoTailsEveryoneDrinksOne() | |
{ | |
$this->givenCoinsAlwaysTailAndTail(); | |
$drinkingGame = $this->getDrinkingGame(['lassi', 'eralp']); | |
$drinkList = $drinkingGame->getDrinkList(1); | |
$this->assertEquals(['lassi' => 1, 'eralp' => 1], $drinkList); | |
} | |
public function testTwoDifferentZoltanDrinksOne() | |
{ | |
$this->givenCoinsAlwaysTailAndHeads(); | |
$drinkingGame = $this->getDrinkingGame(['zoltan', 'eralp']); | |
$drinkList = $drinkingGame->getDrinkList(1); | |
$this->assertEquals(['zoltan' => 1, 'eralp' => 0], $drinkList); | |
} | |
public function testHeadsAndHeadsNoOneDrinks() | |
{ | |
$this->givenCoinsAlwaysHeadsAndHeads(); | |
$drinkingGame = $this->getDrinkingGame(['zoltan', 'eralp']); | |
$drinkList = $drinkingGame->getDrinkList(1); | |
$this->assertEquals(['zoltan' => 0, 'eralp' => 0], $drinkList); | |
} | |
public function testTwoTailsTwiceEveryoneDrinksTwice() | |
{ | |
$this->givenCoinsAlwaysTailAndTail(); | |
$drinkingGame = $this->getDrinkingGame(['lassi', 'eralp']); | |
$drinkList = $drinkingGame->getDrinkList(2); | |
$this->assertEquals(['lassi' => 2, 'eralp' => 2], $drinkList); | |
} | |
public function testO() | |
{ | |
$this->givenCoinflipOrder([['tail', 'tail'], ['tail', 'heads']]); | |
$drinkingGame = $this->getDrinkingGame(['zoltan', 'eralp']); | |
$drinkList = $drinkingGame->getDrinkList(2); | |
$this->assertEquals(['zoltan' => 2, 'eralp' => 1], $drinkList); | |
} | |
private function getDrinkingGame(array $names) | |
{ | |
return new DrinkingGame($names, $this->coin); | |
} | |
private function givenCoinflipOrder(array $coinflips) | |
{ | |
$this->coin->expects($this->any()) | |
->method('toss') | |
->will(new \PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls($coinflips)); | |
} | |
private function givenCoinsAlwaysTailAndHeads() | |
{ | |
$this->coin->expects($this->any()) | |
->method('toss') | |
->will($this->returnValue(['tail', 'heads'])); | |
} | |
private function givenCoinsAlwaysTailAndTail() | |
{ | |
$this->coin->expects($this->any()) | |
->method('toss') | |
->will($this->returnValue(['tail', 'tail'])); | |
} | |
private function givenCoinsAlwaysHeadsAndHeads() | |
{ | |
$this->coin->expects($this->any()) | |
->method('toss') | |
->will($this->returnValue(['heads', 'heads'])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment