From: Chuck Hagenbuch Date: Sat, 26 Sep 2009 03:15:35 +0000 (-0400) Subject: add new Horde_Constraint package for building up constraint sets X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=8824912b1f15b4ab9a43478adb9e9cd698e70786;p=horde.git add new Horde_Constraint package for building up constraint sets --- diff --git a/framework/Constraint/lib/Horde/Constraint.php b/framework/Constraint/lib/Horde/Constraint.php new file mode 100644 index 000000000..359856a3e --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint.php @@ -0,0 +1,10 @@ + + */ +interface Horde_Constraint +{ + public function evaluate($value); +} diff --git a/framework/Constraint/lib/Horde/Constraint/AlwaysFalse.php b/framework/Constraint/lib/Horde/Constraint/AlwaysFalse.php new file mode 100644 index 000000000..2dcc0e19e --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/AlwaysFalse.php @@ -0,0 +1,13 @@ + + */ +class Horde_Constraint_AlwaysFalse implements Horde_Constraint +{ + public function evaluate($value) + { + return false; + } +} diff --git a/framework/Constraint/lib/Horde/Constraint/AlwaysTrue.php b/framework/Constraint/lib/Horde/Constraint/AlwaysTrue.php new file mode 100644 index 000000000..d4848e509 --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/AlwaysTrue.php @@ -0,0 +1,13 @@ + + */ +class Horde_Constraint_AlwaysTrue implements Horde_Constraint +{ + public function evaluate($value) + { + return true; + } +} diff --git a/framework/Constraint/lib/Horde/Constraint/And.php b/framework/Constraint/lib/Horde/Constraint/And.php new file mode 100644 index 000000000..2f199a44d --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/And.php @@ -0,0 +1,21 @@ + + */ +class Horde_Constraint_And extends Horde_Constraint_Compound +{ + public function evaluate($value) + { + foreach ($this->_constraints as $c) { + if (!$c->evaluate($value)) { + return false; + } + } + return true; + } +} diff --git a/framework/Constraint/lib/Horde/Constraint/Compound.php b/framework/Constraint/lib/Horde/Constraint/Compound.php new file mode 100644 index 000000000..48d80274c --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/Compound.php @@ -0,0 +1,29 @@ + + */ +abstract class Horde_Constraint_Compound implements Horde_Constraint +{ + protected $_constraints = array(); + + public function __construct() + { + $constraints = func_get_args(); + foreach ($constraints as $c) { + if (! $c instanceof Horde_Constraint) { + throw new IllegalArgumentException("$c does not implement Horde_Constraint"); + } + } + $this->_constraints = $constraints; + } + + public function addConstraint(Horde_Constraint $constraint) + { + $this->_constraints[] = $constraint; + return $this; + } + + abstract public function evaluate($value); +} diff --git a/framework/Constraint/lib/Horde/Constraint/IsEqual.php b/framework/Constraint/lib/Horde/Constraint/IsEqual.php new file mode 100644 index 000000000..3c06dc708 --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/IsEqual.php @@ -0,0 +1,22 @@ + + */ +class Horde_Constraint_IsEqual implements Horde_Constraint +{ + private $_value; + + public function __construct($value) + { + $this->_value = $value; + } + + public function evaluate($value) + { + return $this->_value == $value; + } +} diff --git a/framework/Constraint/lib/Horde/Constraint/IsInstanceOf.php b/framework/Constraint/lib/Horde/Constraint/IsInstanceOf.php new file mode 100644 index 000000000..43d43e743 --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/IsInstanceOf.php @@ -0,0 +1,22 @@ + + */ +class Horde_Constraint_IsInstanceOf implements Horde_Constraint +{ + private $_type; + + public function __construct($type) + { + $this->_type = $type; + } + + public function evaluate($value) + { + return $value instanceof $this->_type; + } +} diff --git a/framework/Constraint/lib/Horde/Constraint/Not.php b/framework/Constraint/lib/Horde/Constraint/Not.php new file mode 100644 index 000000000..69addc40c --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/Not.php @@ -0,0 +1,22 @@ + + */ +class Horde_Constraint_Not implements Horde_Constraint +{ + private $_constraint; + + public function __construct(Horde_Constraint $constraint) + { + $this->_constraint = $constraint; + } + + public function evaluate($value) + { + return !$this->_constraint->evaluate($value); + } +} diff --git a/framework/Constraint/lib/Horde/Constraint/Null.php b/framework/Constraint/lib/Horde/Constraint/Null.php new file mode 100644 index 000000000..c385900a2 --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/Null.php @@ -0,0 +1,15 @@ + + */ +class Horde_Constraint_Null implements Horde_Constraint +{ + public function evaluate($value) + { + return is_null($value); + } +} diff --git a/framework/Constraint/lib/Horde/Constraint/Or.php b/framework/Constraint/lib/Horde/Constraint/Or.php new file mode 100644 index 000000000..0882fedcd --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/Or.php @@ -0,0 +1,19 @@ + + * @author Chuck Hagenbuch + */ +class Horde_Constraint_Or extends Horde_Constraint_Compound +{ + public function evaluate($value) + { + foreach ($this->_constraints as $c) { + if ($c->evaluate($value)) { + return true; + } + } + return false; + } +} diff --git a/framework/Constraint/lib/Horde/Constraint/PregMatch.php b/framework/Constraint/lib/Horde/Constraint/PregMatch.php new file mode 100644 index 000000000..cc148e2aa --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/PregMatch.php @@ -0,0 +1,22 @@ + + */ +class Horde_Constraint_PregMatch implements Horde_Constraint +{ + private $_regex; + + public function __construct($regex) + { + $this->_regex = $regex; + } + + public function evaluate($value) + { + return preg_match($this->_regex, $value) > 0; + } +} diff --git a/framework/Constraint/package.xml b/framework/Constraint/package.xml new file mode 100644 index 000000000..f07079797 --- /dev/null +++ b/framework/Constraint/package.xml @@ -0,0 +1,81 @@ + + + Constraint + pear.horde.org + Horde Constraint library + This package provides a programmatic way of building constraints that evaluate to true or false + + + Chuck Hagenbuch + chuck + chuck@horde.org + yes + + + James Pepin + james + james@jamespepin + yes + + 2009-09-25 + + 0.1.0 + 0.1.0 + + + beta + beta + + BSD + * Initial release. + + + + + + + + + + + + + + + + + + + + + + + + + + 5.2.0 + + + 1.7.0 + + + + + + + + + + + + + + + + + + + diff --git a/framework/Constraint/test/Horde/Constraint/AllTests.php b/framework/Constraint/test/Horde/Constraint/AllTests.php new file mode 100644 index 000000000..c1968084c --- /dev/null +++ b/framework/Constraint/test/Horde/Constraint/AllTests.php @@ -0,0 +1,47 @@ +isFile() && preg_match('/Test.php$/', $file->getFilename())) { + $pathname = $file->getPathname(); + require $pathname; + + $class = str_replace(DIRECTORY_SEPARATOR, '_', + preg_replace("/^$baseregexp(.*)\.php/", '\\1', $pathname)); + $suite->addTestSuite('Horde_Constraint_' . $class); + } + } + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'Horde_Constraint_AllTests::main') { + Horde_Constraint_AllTests::main(); +} diff --git a/framework/Constraint/test/Horde/Constraint/AlwaysFalseTest.php b/framework/Constraint/test/Horde/Constraint/AlwaysFalseTest.php new file mode 100644 index 000000000..cd6af3145 --- /dev/null +++ b/framework/Constraint/test/Horde/Constraint/AlwaysFalseTest.php @@ -0,0 +1,22 @@ +assertFalse($const->evaluate($value)); + } +} diff --git a/framework/Constraint/test/Horde/Constraint/AlwaysTrueTest.php b/framework/Constraint/test/Horde/Constraint/AlwaysTrueTest.php new file mode 100644 index 000000000..aa69e65e9 --- /dev/null +++ b/framework/Constraint/test/Horde/Constraint/AlwaysTrueTest.php @@ -0,0 +1,22 @@ +assertTrue($const->evaluate($value)); + } +} diff --git a/framework/Constraint/test/Horde/Constraint/AndTest.php b/framework/Constraint/test/Horde/Constraint/AndTest.php new file mode 100644 index 000000000..6be9f03f7 --- /dev/null +++ b/framework/Constraint/test/Horde/Constraint/AndTest.php @@ -0,0 +1,63 @@ +assertFalse($and->evaluate('test_string')); + } + + public function testAndEvaluatesFalseWhenBothConstraintsAreFalse() + { + $c1 = new Horde_Constraint_AlwaysFalse(); + $c2 = new Horde_Constraint_AlwaysFalse(); + $and = new Horde_Constraint_And($c1, $c2); + + $this->assertFalse($and->evaluate('test_string')); + } + + public function testAndEvaluatesTrueWhenBothConstraintsAreTrue() + { + $c1 = new Horde_Constraint_AlwaysTrue(); + $c2 = new Horde_Constraint_AlwaysTrue(); + $and = new Horde_Constraint_And($c1, $c2); + + $this->assertTrue($and->evaluate('test_string')); + } + + public function testAndEvaluatesFalseWhenFalseConstraintIsAddedViaSetter() + { + $c1 = new Horde_Constraint_AlwaysTrue(); + $c2 = new Horde_Constraint_AlwaysTrue(); + $and = new Horde_Constraint_And($c1, $c2); + + $and->addConstraint(new Horde_Constraint_AlwaysFalse()); + + $this->assertFalse($and->evaluate('test_string')); + } + + public function testAndaddConstraintReturnsAndConstraint() + { + $c1 = new Horde_Constraint_AlwaysTrue(); + $c2 = new Horde_Constraint_AlwaysTrue(); + $and = new Horde_Constraint_And($c1, $c2); + + $returnConst = $and->addConstraint(new Horde_Constraint_AlwaysFalse()); + + $this->assertType('Horde_Constraint_And', $returnConst); + } + + public function testReturnedAndEvaluatesFalseWhenFalseConstraintIsAddedViaSetter() + { + $c1 = new Horde_Constraint_AlwaysTrue(); + $c2 = new Horde_Constraint_AlwaysTrue(); + $and = new Horde_Constraint_And($c1, $c2); + + $and = $and->addConstraint(new Horde_Constraint_AlwaysFalse()); + + $this->assertFalse($and->evaluate('test_string')); + } +} diff --git a/framework/Constraint/test/Horde/Constraint/IsInstanceOfTest.php b/framework/Constraint/test/Horde/Constraint/IsInstanceOfTest.php new file mode 100644 index 000000000..66a21f42e --- /dev/null +++ b/framework/Constraint/test/Horde/Constraint/IsInstanceOfTest.php @@ -0,0 +1,19 @@ +assertFalse($const->evaluate($foo)); + } + + public function testConstraintReturnsTrueWhenInstanceIsCorrectClass() + { + $foo = new StdClass(); + $const = new Horde_Constraint_IsInstanceOf('StdClass'); + + $this->assertTrue($const->evaluate($foo)); + } +} diff --git a/framework/Constraint/test/Horde/Constraint/NotTest.php b/framework/Constraint/test/Horde/Constraint/NotTest.php new file mode 100644 index 000000000..919cb8c66 --- /dev/null +++ b/framework/Constraint/test/Horde/Constraint/NotTest.php @@ -0,0 +1,15 @@ +assertTrue($not->evaluate('foo')); + } + + public function testNotMakesTrueConstraintFalse() + { + $not = new Horde_Constraint_Not(new Horde_Constraint_AlwaysTrue()); + $this->assertFalse($not->evaluate('foo')); + } +} diff --git a/framework/Constraint/test/Horde/Constraint/NullTest.php b/framework/Constraint/test/Horde/Constraint/NullTest.php new file mode 100644 index 000000000..22efcab3c --- /dev/null +++ b/framework/Constraint/test/Horde/Constraint/NullTest.php @@ -0,0 +1,15 @@ +assertTrue($const->evaluate(null)); + } + + public function testNullReturnsFalseWhenValue_IsNot_Null() + { + $const = new Horde_Constraint_Null(); + $this->assertFalse($const->evaluate('not null value')); + } +} diff --git a/framework/Constraint/test/Horde/Constraint/OrTest.php b/framework/Constraint/test/Horde/Constraint/OrTest.php new file mode 100644 index 000000000..2b3ce6159 --- /dev/null +++ b/framework/Constraint/test/Horde/Constraint/OrTest.php @@ -0,0 +1,63 @@ +assertTrue($or->evaluate('test_string')); + } + + public function testOrEvaluatesFalseWhenBothConstraintsAreFalse() + { + $c1 = new Horde_Constraint_AlwaysFalse(); + $c2 = new Horde_Constraint_AlwaysFalse(); + $or = new Horde_Constraint_Or($c1, $c2); + + $this->assertFalse($or->evaluate('test_string')); + } + + public function testOrEvaluatesTrueWhenBothConstraintsAreTrue() + { + $c1 = new Horde_Constraint_AlwaysTrue(); + $c2 = new Horde_Constraint_AlwaysTrue(); + $or = new Horde_Constraint_Or($c1, $c2); + + $this->assertTrue($or->evaluate('test_string')); + } + + public function testOrEvaluatesTrueWhenTrueConstraintIsAddedViaSetter() + { + $c1 = new Horde_Constraint_AlwaysFalse(); + $c2 = new Horde_Constraint_AlwaysFalse(); + $or = new Horde_Constraint_Or($c1, $c2); + + $or->addConstraint(new Horde_Constraint_AlwaysTrue()); + + $this->assertTrue($or->evaluate('test_string')); + } + + public function testOraddConstraintReturnsOrConstraint() + { + $c1 = new Horde_Constraint_AlwaysTrue(); + $c2 = new Horde_Constraint_AlwaysTrue(); + $or = new Horde_Constraint_Or($c1, $c2); + + $returnConst = $or->addConstraint(new Horde_Constraint_AlwaysFalse()); + + $this->assertType('Horde_Constraint_Or', $returnConst); + } + + public function testReturnedOrEvaluatesTrueWhenTrueConstraintIsAddedViaSetter() + { + $c1 = new Horde_Constraint_AlwaysFalse(); + $c2 = new Horde_Constraint_AlwaysFalse(); + $or = new Horde_Constraint_Or($c1, $c2); + + $or = $or->addConstraint(new Horde_Constraint_AlwaysTrue()); + + $this->assertTrue($or->evaluate('test_string')); + } +} diff --git a/framework/Constraint/test/Horde/Constraint/PregMatchTest.php b/framework/Constraint/test/Horde/Constraint/PregMatchTest.php new file mode 100644 index 000000000..816836597 --- /dev/null +++ b/framework/Constraint/test/Horde/Constraint/PregMatchTest.php @@ -0,0 +1,15 @@ +assertTrue($preg->evaluate('somestring')); + } + + public function testPregReturnsFalseWhenRegex_DoesNot_Match() + { + $preg = new Horde_Constraint_PregMatch('/somestring/'); + $this->assertFalse($preg->evaluate('some other string')); + } +}