Skip to content

Instantly share code, notes, and snippets.

Created December 8, 2011 14:36
PHPUnitSample
<?php
class Entry {
const STATUS_DRAFT = 'draft';
const STATUS_PUBLISHED = 'published';
const STATUS_DELETED = 'deleted';
var $status = self::STATUS_DRAFT;
var $title;
public function publish() {
$this->status = self::STATUS_PUBLISHED;
}
public function delete() {
$this->status = self::STATUS_DELETED;
}
}
?>
<?php
spl_autoload_register(function ($className) {
include "$className.php";
});
class EntrySpec extends PHPUnit_Extensions_Story_TestCase {
/**
* @scenario
*/
public function statusForNewEntryIsDraft() {
$this->given('New Entry')
->then('Status should be', Entry::STATUS_DRAFT);
}
/**
* @scenario
*/
public function statusAfterPublishEntryIsPublished() {
$this->given('Some Entry')
->when('publish Entry')
->then('Status should be', Entry::STATUS_PUBLISHED);
}
/**
* @scenario
*/
public function statusAfterDeleteEntryIsDeleted() {
$this->given('Some Entry')
->when('delete Entry')
->then('Status should be', Entry::STATUS_DELETED);
}
public function runGiven(&$world, $action, $arguments) {
switch($action) {
case 'New Entry':
case 'Some Entry':
$world['entry'] = new Entry();
break;
default:
return $this->notImplemented($action);
}
}
public function runWhen(&$world, $action, $arguments) {
switch($action) {
case 'delete Entry':
$world['entry']->delete();
break;
case 'publish Entry':
$world['entry']->publish();
break;
default:
return $this->notImplemented($action);
}
}
public function runThen(&$world, $action, $arguments) {
switch($action) {
case 'Status should be':
$this->assertEquals($arguments[0], $world['entry']->status);
break;
default:
return $this->notImplemented($action);
}
}
}
?>
<?php
spl_autoload_register(function ($className) {
include "$className.php";
});
class EntryTest extends PHPUnit_Framework_TestCase {
public function testStatus() {
$entry = new Entry();
$this->assertEquals(Entry::STATUS_DRAFT, $entry->status);
$entry->publish();
$this->assertEquals(Entry::STATUS_PUBLISHED, $entry->status);
$entry->delete();
$this->assertEquals(Entry::STATUS_DELETED, $entry->status);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment