Permalink
Browse files

is_array assertion added

  • Loading branch information...
Jakobo committed Dec 3, 2010
1 parent 7107b75 commit 6290bbec6d8537dddee733721ef91d326f0efb8c
Showing with 119 additions and 31 deletions.
  1. +65 −18 core/exceptions/exceptions.php
  2. +29 −13 core/testcase/testcase.php
  3. +25 −0 tests/testcase/testcase.stest.php
@@ -44,6 +44,23 @@ protected function setUserMessage($message) {
public function getUserMessage() {
return $this->user_message;
}
/**
* Extracts the variable information from the outcome
* @param mixed $outcome
* @return string
*/
protected function captureVariable($outcome) {
ob_start();
var_dump($outcome);
$explain = ob_get_contents();
ob_end_clean();
$explain = trim(str_replace(array("\r\n", "\r"), "\n", $explain));
$explain = (strpos($explain, "\n") === FALSE) ? $explain : trim(substr($explain, 0, strpos($explain, "\n"))).'...';
return $explain;
}
}
@@ -165,6 +182,22 @@ public function __construct($code, $message, $value, $regex) {
}
}
/**
* Used for testing when assertions was an array
*/
class Snap_AssertIsArrayUnitTestException extends Snap_AssertTypeUnitTestException {
/**
* Constructor
* @param string $code the short code message
* @param string $message the longer dev-created message
* @param mixed $outcome the actual outcome
* @param mixed $should_be the result that it should have been
*/
public function __construct($code, $message, $outcome) {
parent::__construct($code, $message, $outcome, 'is_array');
}
}
/**
* Used for testing when assertions were supposed to be the same
@@ -235,6 +268,38 @@ public function __construct($code, $message, $outcome, $should_be) {
}
}
/**
* Used for all type comparison assertions
*/
class Snap_AssertTypeUnitTestException extends Snap_UnitTestException {
/**
* Constructor
* @param string $code the short code message
* @param string $message the longer dev-created message
* @param mixed $outcome the actual outcome
* @param string $test the operation
*/
public function __construct($code, $message, $outcome, $test) {
$outcome = $this->captureVariable($outcome);
$type = getType($outcome);
switch (strtolower($code)) {
case 'assert_is_array':
$prefix = 'Is Array assertion failed';
break;
default:
$prefix = 'Unkown assertion';
}
if ($message) {
$message = ' with user message: '.$message;
}
$message = $prefix.', is of type '.$type.' ['.$outcome.']'.$message;
parent::__construct($code, $message);
}
}
/**
* Used for all basic comparisson assertions
@@ -291,24 +356,6 @@ public function __construct($code, $message, $outcome, $should_be, $operator) {
parent::__construct($code, $message);
}
/**
* Extracts the variable information from the outcome
* @param mixed $outcome
* @return string
*/
protected function captureVariable($outcome) {
ob_start();
var_dump($outcome);
$explain = ob_get_contents();
ob_end_clean();
$explain = trim(str_replace(array("\r\n", "\r"), "\n", $explain));
$explain = (strpos($explain, "\n") === FALSE) ? $explain : trim(substr($explain, 0, strpos($explain, "\n"))).'...';
return $explain;
}
}
View
@@ -47,7 +47,7 @@ protected function getTallyFromMock($object, $method, $params) {
* @param $mock a mock object to test for
* @param array a set of parameteres to create a signature with
* @param string $msg user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws Snap_AssertCallCountException
**/
protected function assertCallCount($object, $method_name, $expected_count, $method_params = array(), $msg = '') {
@@ -66,7 +66,7 @@ protected function assertCallCount($object, $method_name, $expected_count, $meth
* @param $mock a mock object to test for
* @param array a set of parameteres to create a signature with
* @param string $msg user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws Snap_AssertCallCountException
**/
protected function assertMinimumCallCount($object, $method_name, $expected_count, $method_params = array(), $msg = '') {
@@ -85,7 +85,7 @@ protected function assertMinimumCallCount($object, $method_name, $expected_count
* @param $mock a mock object to test for
* @param array a set of parameteres to create a signature with
* @param string $msg user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws Snap_AssertCallCountException
**/
protected function assertMaximumCallCount($object, $method_name, $expected_count, $method_params = array(), $msg = '') {
@@ -103,7 +103,7 @@ protected function assertMaximumCallCount($object, $method_name, $expected_count
* assert that the incoming value is TRUE
* @param mixed $value
* @param string $msg user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws AssertIdenticalUnitTestException
**/
protected function assertTrue($value, $msg = '') {
@@ -118,7 +118,7 @@ protected function assertTrue($value, $msg = '') {
* assert that the incoming value is FALSE
* @param mixed $value
* @param string $msg user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws AssertIdenticalUnitTestException
**/
protected function assertFalse($value, $msg = '') {
@@ -129,12 +129,28 @@ protected function assertFalse($value, $msg = '') {
return new Snap_PassedTestAssertion();
}
/**
* assert that the incoming value is an array
* @param mixed $value
* @param string $msg user message on failure
* @return Snap_PassedTestAssertion
* @throws AssertIsArrayUnitTestException
**/
protected function assertIsArray($value, $msg = '') {
if (!is_array($value)) {
throw new Snap_AssertIsArrayUnitTestException('assert_is_array', $msg, $value);
}
return new Snap_PassedTestAssertion();
}
/**
* assert that the incoming value is equal to the incomming expectation
* @param mixed $expected the value it should be
* @param mixed $actual the value actually testing
* @param string $msg user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws AssertEqualUnitTestException
**/
protected function assertEqual($actual, $expected, $msg = '') {
@@ -150,7 +166,7 @@ protected function assertEqual($actual, $expected, $msg = '') {
* @param mixed $expected the value it should be
* @param mixed $actual the value actually testing
* @param string $msg user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws AssertNotEqualUnitTestException
**/
protected function assertNotEqual($actual, $expected, $msg = '') {
@@ -166,7 +182,7 @@ protected function assertNotEqual($actual, $expected, $msg = '') {
* @param mixed $expected the value it should be
* @param mixed $actual the value actually testing
* @param string $msg user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws AssertIdenticalUnitTestException
**/
protected function assertIdentical($actual, $expected, $msg = '') {
@@ -182,7 +198,7 @@ protected function assertIdentical($actual, $expected, $msg = '') {
* @param mixed $expected the value it should be
* @param mixed $actual the value actually testing
* @param string $msg user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws AssertIdenticalUnitTestException
**/
protected function assertNotIdentical($actual, $expected, $msg = '') {
@@ -197,7 +213,7 @@ protected function assertNotIdentical($actual, $expected, $msg = '') {
* assert that the incoming value is NULL
* @param mixed $value the value to test
* @param string $msg user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws AssertIdenticalUnitTestException
**/
protected function assertNull($value, $msg = '') {
@@ -212,7 +228,7 @@ protected function assertNull($value, $msg = '') {
* assert that the incoming value is not NULL
* @param mixed $value the value to test
* @param string $msg user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws AssertNotIdenticalUnitTestException
**/
protected function assertNotNull($value, $msg = '') {
@@ -228,7 +244,7 @@ protected function assertNotNull($value, $msg = '') {
* @param object $object the object to test
* @param mixed $classname the class or class name to test
* @param string $msg the user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws AssertInstanceOfUnitTestException
**/
protected function assertIsA($object, $classname, $msg = '') {
@@ -244,7 +260,7 @@ protected function assertIsA($object, $classname, $msg = '') {
* @param string $regex the regulr expression to test
* @param mixed $value the value to test
* @param string $msg user message on failure
* @return boolean TRUE
* @return Snap_PassedTestAssertion
* @throws AssertRegexUnitTestException
**/
protected function assertRegex($value, $regex, $msg = '') {
@@ -146,6 +146,31 @@ public function testFailingAssertFalseThrowsException() {
return $this->assertTrue(FALSE);
}
}
class Snap_UnitTestCase_AssertIsArray_Test extends Snap_UnitTestCase {
public function setUp() {}
public function tearDown() {}
/**
* assert testing can now use IsA tests
*/
public function testAssertIsArrayReturnsPassedTest() {
$test_item = array();
$result = $this->assertIsArray($test_item);
return $this->assertIsA($result, 'Snap_PassedTestAssertion');
}
public function testFailingAssertEqualThrowsException() {
try {
$this->assertIsArray('string');
}
catch (Snap_AssertIsArrayUnitTestException $e) {
return $this->assertTrue(TRUE);
}
return $this->assertTrue(FALSE, 'is_array() assertion failed');
}
}
class Snap_UnitTestCase_AssertEqual_Test extends Snap_UnitTestCase {

0 comments on commit 6290bbe

Please sign in to comment.