Created
April 4, 2012 13:35
-
-
Save skl/2301078 to your computer and use it in GitHub Desktop.
AssertionTuple with DataSetValue stack
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 DataSetValue | |
{ | |
const ALPHA_LOWER = 'abcdefghijklmnopqrstuvwxyz'; | |
const ALPHA_NUMERIC = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; | |
/* more constants */ | |
public function __construct($data = self::ALPHA_NUMERIC, $dataLength = 62) | |
{ | |
$this->setData($data) | |
->setDataLength($dataLength); | |
} | |
/* getters and setters */ | |
public function __toString() | |
{ | |
// e.g. echo new DataSetValue(DataSetValue::ALPHA_LOWER, 2) would output "ab" | |
return substr($this->getData(), 0, $this->getDataLength()); | |
} | |
} | |
class AssertionTuple | |
{ | |
protected $assertionMethod = ''; | |
protected $data; | |
public function __construct($assertionMethod, DataSetValue $data) | |
{ | |
$this->setAssertionMethod($assertionMethod) | |
->setData($data); | |
} | |
} | |
class AssertionStack implements Iterator | |
{ | |
private $position = 0; | |
protected $stack = array(); | |
public function push(AssertionTuple $tuple) | |
{ | |
$this->stack[] = $tuple; | |
} | |
public function rewind() { | |
$this->position = 0; | |
} | |
public function current() { | |
return $this->stack[$this->position]->getData(); | |
} | |
public function key() { | |
return $this->stack[$this->position]->getAssertionMethod(); | |
} | |
public function next() { | |
++$this->position; | |
} | |
public function valid() { | |
return isset($this->stack[$this->position]); | |
} | |
} | |
class UnitTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testAddress1() | |
{ | |
$stack = new AssertionStack; | |
$stack->push(new AssertionTuple('equals', new DataSetValue(DataSetValue::ALPHA_NUMERIC))) | |
->push(new AssertionTuple('notEquals', new DataSetValue(DataSetValue::SYMBOLS))); | |
foreach ($stack as $assertionMethod => $data) | |
{ | |
$this->instance->setAddress2($data); | |
$assertionMethod = 'assert' . ucfirst($assertionMethod); | |
$this->$assertionMethod($data, $this->instance->getAddress2()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment