Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert() improvements #1088

Closed
wants to merge 14 commits into from
Closed
12 changes: 12 additions & 0 deletions Zend/tests/assert/expect_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
test passing assertion
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php
assert(true);
var_dump(true);
?>
--EXPECTF--
bool(true)
16 changes: 16 additions & 0 deletions Zend/tests/assert/expect_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
test failing assertion
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php
assert(false);
var_dump(true);
?>
--EXPECTF--
Fatal error: Uncaught exception 'AssertionException' with message 'assert(false)' in %sexpect_002.php:%d
Stack trace:
#0 %sexpect_002.php(%d): assert(false, 'assert(false)')
#1 {main}
thrown in %sexpect_002.php on line %d
15 changes: 15 additions & 0 deletions Zend/tests/assert/expect_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
test catching failed assertion
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php
try {
assert(false);
} catch (AssertionException $ex) {
var_dump($ex->getMessage());
}
?>
--EXPECT--
string(13) "assert(false)"
15 changes: 15 additions & 0 deletions Zend/tests/assert/expect_004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
test providing reason (fail)
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php
try {
assert(false, "I require this to succeed");
} catch (AssertionException $ex) {
var_dump($ex->getMessage());
}
?>
--EXPECT--
string(25) "I require this to succeed"
17 changes: 17 additions & 0 deletions Zend/tests/assert/expect_005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
test providing reason (pass)
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php
try {
/* by passing we test there are no leaks upon success */
assert(true, "I require this to succeed");
} catch (AssertionException $ex) {
var_dump($ex->getMessage());
}
var_dump(true);
?>
--EXPECT--
bool(true)
14 changes: 14 additions & 0 deletions Zend/tests/assert/expect_006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
test looping assert (pass)
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php
for($i=0; $i<100000; $i++) {
assert ($i < 100000, "The universe should make sense");
}
var_dump(true);
?>
--EXPECT--
bool(true)
22 changes: 22 additions & 0 deletions Zend/tests/assert/expect_007.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
test compiled reason
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php
$next = 1;
$data = array(
"key" => "X-HTTP ",
"value" => "testing"
);

class HeaderMalfunctionException extends AssertionException {}

assert (preg_match("~^([a-zA-Z0-9-]+)$~", $data["key"]), new HeaderMalfunctionException("malformed key found at {$next} \"{$data["key"]}\""));
?>
--EXPECTF--
Fatal error: Uncaught exception 'HeaderMalfunctionException' with message 'malformed key found at 1 "X-HTTP "' in %sexpect_007.php:10
Stack trace:
#0 {main}
thrown in %sexpect_007.php on line 10
34 changes: 34 additions & 0 deletions Zend/tests/assert/expect_008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
test disabled expectations have no ill side effects
--INI--
zend.assertions=0
assert.exception=1
--FILE--
<?php
class One {
public function __construct() {
assert($this || 0);
}
}
class Two extends One {}

class OdEar extends AssertionException {}

function blah(){ return 1; }

$variable = 1;
assert(true, "constant message");
assert(($variable && $variable) || php_sapi_name(), new OdEar("constant message"));
assert(false);
assert(blah(), blah());

new Two();
new Two();
new Two();

assert (blah() || blah() || blah(), blah() || blah() || blah() || blah());

var_dump(true);
?>
--EXPECT--
bool(true)
25 changes: 25 additions & 0 deletions Zend/tests/assert/expect_009.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
test stack trace is correct from failed exception in extended class
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php
class One {
public function __construct() {
}
}
class Two extends One {
public function __construct() {
assert(false);
}
}
new Two();
?>
--EXPECTF--
Fatal error: Uncaught exception 'AssertionException' with message 'assert(false)' in %sexpect_009.php:%d
Stack trace:
#0 %sexpect_009.php(%d): assert(false, 'assert(false)')
#1 %sexpect_009.php(%d): Two->__construct()
#2 {main}
thrown in %sexpect_009.php on line %d
23 changes: 23 additions & 0 deletions Zend/tests/assert/expect_010.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
test stack trace is correct from failed exception in extended class (parent implementing constructor)
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php
class One {
public function __construct() {
assert(false);
}
}
class Two extends One {}

new Two();
?>
--EXPECTF--
Fatal error: Uncaught exception 'AssertionException' with message 'assert(false)' in %sexpect_010.php:%d
Stack trace:
#0 %sexpect_010.php(%d): assert(false, 'assert(false)')
#1 %sexpect_010.php(%d): One->__construct()
#2 {main}
thrown in %sexpect_010.php on line %d
30 changes: 30 additions & 0 deletions Zend/tests/assert/expect_011.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
test overloaded __toString on custom exception
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php
class MyExpectations extends AssertionException {
public function __toString() {
return sprintf(
"[Message]: %s", __CLASS__);
}
}

class One {
public function __construct() {
assert(false, (string) new MyExpectations());
}
}
class Two extends One {}

new Two();
?>
--EXPECTF--
Fatal error: Uncaught exception 'AssertionException' with message '[Message]: MyExpectations' in %sexpect_011.php:%d
Stack trace:
#0 %sexpect_011.php(%d): assert(false, '[Message]: MyEx...')
#1 %sexpect_011.php(%d): One->__construct()
#2 {main}
thrown in %sexpect_011.php on line %d
21 changes: 21 additions & 0 deletions Zend/tests/assert/expect_012.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
test enable/disable assertions at runtime
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php
var_dump((integer)ini_get("zend.assertions"));
ini_set("zend.assertions", 0);
var_dump((integer)ini_get("zend.assertions"));
assert(false);
ini_set("zend.assertions", 1);
var_dump((integer)ini_get("zend.assertions"));
assert(true);
var_dump(true);
?>
--EXPECT--
int(1)
int(0)
int(1)
bool(true)
11 changes: 11 additions & 0 deletions Zend/tests/assert/expect_013.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
test failing assertion when disabled (with return value)
--INI--
zend.assertions=0
assert.exception=1
--FILE--
<?php
var_dump(assert(false));
?>
--EXPECT--
bool(true)
12 changes: 12 additions & 0 deletions Zend/tests/assert/expect_014.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
test failing assertion when disabled
--INI--
zend.assertions=0
assert.exception=1
--FILE--
<?php
assert(false);
var_dump(true);
?>
--EXPECT--
bool(true)