aboutsummaryrefslogtreecommitdiffstats
path: root/AssertionChecker
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2013-11-10 15:07:40 +0100
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2013-11-10 15:07:40 +0100
commit131da654c830efc6d8ed96ad116ad878937bc77e (patch)
treef7f4817aaeec6a65009f7319825cb4e08e7088cf /AssertionChecker
parentce3a6b9abb7c9cd283ad3f67e6a84382f5e3290d (diff)
downloadPraspel-131da654c830efc6d8ed96ad116ad878937bc77e.zip
Praspel-131da654c830efc6d8ed96ad116ad878937bc77e.tar.gz
Praspel-131da654c830efc6d8ed96ad116ad878937bc77e.tar.bz2
Add “preambler” support.
Diffstat (limited to 'AssertionChecker')
-rw-r--r--AssertionChecker/AssertionChecker.php20
-rw-r--r--AssertionChecker/Preambler.php190
2 files changed, 210 insertions, 0 deletions
diff --git a/AssertionChecker/AssertionChecker.php b/AssertionChecker/AssertionChecker.php
index 3d91e7d..e808b89 100644
--- a/AssertionChecker/AssertionChecker.php
+++ b/AssertionChecker/AssertionChecker.php
@@ -39,6 +39,11 @@ namespace {
from('Hoa')
/**
+ * \Hoa\Praspel\AssertionChecker\Preambler
+ */
+-> import('Praspel.AssertionChecker.Preambler')
+
+/**
* \Hoa\Praspel\Visitor\Praspel
*/
-> import('Praspel.Visitor.Praspel');
@@ -110,6 +115,21 @@ abstract class AssertionChecker {
}
/**
+ * Preamble: put the system under test into a specific state in order to be
+ * able to execute the test.
+ *
+ * @access public
+ * @param mixed $preamble Preamble callable.
+ * @return void
+ */
+ public function preamble ( $preamble ) {
+
+ $preamble(new Preambler($this->getCallable()));
+
+ return;
+ }
+
+ /**
* Assertion checker.
*
* @access public
diff --git a/AssertionChecker/Preambler.php b/AssertionChecker/Preambler.php
new file mode 100644
index 0000000..8bad26e
--- /dev/null
+++ b/AssertionChecker/Preambler.php
@@ -0,0 +1,190 @@
+<?php
+
+/**
+ * Hoa
+ *
+ *
+ * @license
+ *
+ * New BSD License
+ *
+ * Copyright © 2007-2013, Ivan Enderlin. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Hoa nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+namespace {
+
+from('Hoa')
+
+/**
+ * \Hoa\Praspel\Exception\AssertionChecker
+ */
+-> import('Praspel.Exception.AssertionChecker');
+
+}
+
+namespace Hoa\Praspel\AssertionChecker {
+
+/**
+ * Class \Hoa\Praspel\AssertionChecker\Preambler.
+ *
+ * Helpers for the preamble.
+ *
+ * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
+ * @copyright Copyright © 2007-2013 Ivan Enderlin.
+ * @license New BSD License
+ */
+
+class Preambler {
+
+ /**
+ * Callable to validate and verify.
+ *
+ * @var \Hoa\Core\Consistency\Xcallable object
+ */
+ protected $__callable = null;
+
+
+
+ /**
+ * Construct.
+ *
+ * @access public
+ * @param \Hoa\Core\Consistency\Xcallable $callable Callable.
+ * @return void
+ */
+ public function __construct ( \Hoa\Core\Consistency\Xcallable $callable ) {
+
+ $this->__setCallable($callable);
+
+ return;
+ }
+
+ /**
+ * Get reflection object.
+ *
+ * @access public
+ * @param object &$object Object.
+ * @return \ReflectionObject
+ * @throw \Hoa\Praspel\Exception\AssertionChecker
+ */
+ public function __getReflectionObject ( &$object ) {
+
+ static $_out = null;
+ static $_object = null;
+
+ if(null === $_out) {
+
+ $callback = $this->__getCallable()->getValidCallback();
+
+ if(!is_object($callback[0]))
+ throw new \Hoa\Praspel\Exception\AssertionChecker(
+ 'Callable %s is not an object.', 0, $this->getCallable());
+
+ $_object = $callback[0];
+ $_out = new \ReflectionObject($_object);
+ }
+
+ $object = $_object;
+
+ return $_out;
+ }
+
+ /**
+ * Set an attribute.
+ *
+ * @access public
+ * @param string $name Name.
+ * @param mixed $value Value.
+ * @return \Hoa\Praspel\AssertionChecker\Preambler
+ * @throw \Hoa\Praspel\Exception\AssertionChecker
+ */
+ public function __set ( $name, $value ) {
+
+ $reflectionObject = $this->__getReflectionObject($object);
+
+ if(false === $reflectionObject->hasProperty($name))
+ throw new \Hoa\Praspel\Exception\AssertionChecker(
+ 'Attribute %s on object %s does not exist, cannot set it.',
+ 1, array($name, $reflectionObject->getName()));
+
+ $attribute = $reflectionObject->getProperty($name);
+ $attribute->setAccessible(true);
+ $attribute->setValue($object, $value);
+
+ return $this;
+ }
+
+ /**
+ * Get an attribute.
+ *
+ * @access public
+ * @param string $name Name.
+ * @return mixed
+ * @throw \Hoa\Praspel\Exception\AssertionChecker
+ */
+ public function __get ( $name ) {
+
+ $reflectionObject = $this->__getReflectionObject($object);
+
+ if(false === $reflectionObject->hasProperty($name))
+ throw new \Hoa\Praspel\Exception\AssertionChecker(
+ 'Attribute %s on object %s does not exist, cannot get it.',
+ 2, array($name, $reflectionObject->getName()));
+
+ $attribute = $reflectionObject->getProperty($name);
+ $attribute->setAccessible(true);
+
+ return $attribute->getValue($object);
+ }
+
+ /**
+ * Set callable.
+ *
+ * @access protected
+ * @param \Hoa\Core\Consistency\Xcallable $callable Callable.
+ * @return \Hoa\Core\Consistency\Xcallable
+ */
+ protected function __setCallable ( \Hoa\Core\Consistency\Xcallable $callable ) {
+
+ $old = $this->__callable;
+ $this->__callable = $callable;
+
+ return $old;
+ }
+
+ /**
+ * Get callable.
+ *
+ * @access public
+ * @return \Hoa\Core\Consistency\Xcallable
+ */
+ public function __getCallable ( ) {
+
+ return $this->__callable;
+ }
+}
+
+}