Skip to content

Instantly share code, notes, and snippets.

@paulosborne
Forked from skl/gist:2301078
Created April 4, 2012 13:38
Show Gist options
  • Save paulosborne/2301100 to your computer and use it in GitHub Desktop.
Save paulosborne/2301100 to your computer and use it in GitHub Desktop.
AssertionTuple with DataSetValue stack
<?php
class DataSetValue
{
const ALPHA_LOWER = 'abcdefghijklmnopqrstuvwxyz';
const ALPHA_NUMERIC = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const ALPHA_SYMBOLS = '!@£$%^&*()_+';
const EMAIL_VALID = 'john.smith@testemail.com';
/* more constants */
public function __construct($dataType = self::ALPHA_NUMERIC, $dataLength = 62)
{
$this->setDataType($dataType)
->setDataLength($dataLength);
}
/* getters and setters */
public function __toString()
{
// e.g. echo new DataSetValue(DataSetValue::ALPHA_LOWER, 2) would output "ab"
return substr(constant(self::$this->getDataType()), 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 pop(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->pop(new AssertionTuple('equals', new DataSetValue(DataSetValue::ALPHA_NUMERIC)))
->pop(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