No one in my team knows how to write BDD tests, so I started writing some and it's quite working well. I think the time has come to improve the code quality.
This contains a lot of duplicated code and unused features:
<?php
require_once ('core/v3/engine.php');
require_once 'PHPUnit/Extensions/Story/TestCase.php';
class cHelperValidationSpec extends PHPUnit_Extensions_Story_TestCase {
/**
* Check all the output for the right cases
* @scenario
*/
public function specForJobId() {
$this->given('For a mValue (mixedValue) and we want to validate if it is an integer')
->when('sJobId is a positive integer', 123)
->then('return of positive integer should be', 'TRUE')
->when('sJobId is a negative integer', -123)
->then('return of a negative integer should be', 'FALSE')
->when('sJobId is a float', 1.23)
->then('return of a float should be', 'FALSE')
->when('sJobId is a string containing only digits', '123')
->then('return of a string (containing only digits) should be', 'TRUE')
->when('sJobId is a string containing not only digits', '123abc-?')
->then('return of a string (containing not only digits) should be', 'TRUE');
}
public function runGiven(&$world, $action, $arguments) {
switch($action) {
case 'For a mValue (mixedValue) and we want to validate if it is an integer': {
$world['helper'] = new cHelperValidation();
}
break;
default: {
return $this->notImplemented($action);
}
}
}
public function runWhen(&$world, $action, $arguments) {
switch($action) {
case 'sJobId is a positive integer': {}
case 'sJobId is a negative integer': {}
case 'sJobId is a float': {}
case 'sJobId is a string containing only digits': {}
case 'sJobId is a string containing not only digits': {}
break;
default: {
return $this->notImplemented($action);
}
}
}
public function runThen(&$world, $action, $arguments) {
switch($action) {
case 'return of positive integer should be': {
$this->assertEquals(TRUE, $world['helper']->isId(123));
}
case 'return of a negative integer should be': {
$this->assertEquals(FALSE, $world['helper']->isId(-123));
}
case 'return of a float should be': {
$this->assertEquals(FALSE, $world['helper']->isId(1.23));
}
case 'return of a string (containing only digits) should be': {
$this->assertEquals(TRUE, $world['helper']->isId('123'));
}
case 'return of a string (containing not only digits) should be': {
$this->assertEquals(FALSE, $world['helper']->isId('123abc-?'));
}
break;
default: {
return $this->notImplemented($action);
}
}
}
}
The only reference I'm using for BDD is here, so there is much potential for improving this code.