Skip to content

Instantly share code, notes, and snippets.

@edorian
Created October 31, 2011 09:45
Show Gist options
  • Save edorian/1327198 to your computer and use it in GitHub Desktop.
Save edorian/1327198 to your computer and use it in GitHub Desktop.
A sample test for the bowling game kata with PHPUnitStandard issues
<?php
/*
phpcs --standard=PHPUnitStandard TestExample.php
52 | ERROR | Private methods are prohibited; found rollSpare
57 | ERROR | Private methods are prohibited; found rollMany
57 | ERROR | Function's cyclomatic complexity (2) exceeds allowed maximum of
| | 1
57 | ERROR | Function's nesting level (1) exceeds allowed maximum of 0
I can't see how to get rid of those errors within the standard or without cluttering up the test immensely. Whats your take on that?
*/
class GameTest extends PHPUnit_Framework_TestCase {
/**
* @var Game
*/
private $game;
protected function setUp() {
$this->game = new Game();
}
public function testGutterGame() {
$this->rollMany(20, 0);
$this->assertSame(0, $this->game->score());
}
public function testRollAllOnes() {
$this->rollMany(20, 1);
$this->assertSame(20, $this->game->score());
}
public function testRollSpare() {
$this->rollSpare();
$this->game->roll(1);
$this->rollMany(17, 0);
$this->assertSame(12, $this->game->score());
}
public function testRollStrike() {
$this->game->roll(10);
$this->game->roll(1);
$this->game->roll(3);
$this->rollMany(16, 0);
$this->assertSame(18, $this->game->score());
}
public function testPerfectGame() {
$this->rollMany(12, 10);
$this->assertSame(300, $this->game->score());
}
public function testThenStrikesThenTwoFives() {
$this->rollMany(10, 10);
$this->rollMany(2, 5);
$this->assertSame(285, $this->game->score());
}
private function rollSpare() {
$this->game->roll(5);
$this->game->roll(5);
}
private function rollMany($rolls, $pins) {
for (; $rolls; --$rolls) {
$this->game->roll($pins);
}
}
/**
* @expectedException InvalidArgumentException
*/
public function testRollingNegativeAmountOfPinsFails() {
$this->game->roll(-1);
}
/**
* @expectedException InvalidArgumentException
*/
public function testRollingMoreThan10PinsFails() {
$this->game->roll(11);
}
}
@elblinkin
Copy link

The private methods (IMO) need to either have the contents copy/paste into each test case they are called b/c frankly it is only two lines, or they need to be extracted to a helper, or in this case you are probably better off expanding the interface to the object under test to have those two methods.

The latter seems a good fit because right now this is suggesting that you will probably have many occurrences where you want to represent a strike or a spare just in your "production" usage and that you are not properly encapsulating the behavior in the object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment