Add some class related tests, fix hard-coded object ID in serialize_001.phpt.

This commit is contained in:
Robin Fernandes 2008-12-05 22:12:04 +00:00
parent 97a63ab6b4
commit 5e33549e0d
18 changed files with 692 additions and 8 deletions

View File

@ -0,0 +1,77 @@
--TEST--
Ensure exceptions are handled properly when thrown in __call.
--FILE--
<?php
class A {
function __call($strMethod, $arrArgs) {
var_dump($this);
throw new Exception;
echo "You should not see this";
}
function test() {
A::unknownCalledWithSRO(1,2,3);
}
}
class B extends A {
function test() {
B::unknownCalledWithSROFromChild(1,2,3);
}
}
$a = new A();
echo "---> Invoke __call via simple method call.\n";
try {
$a->unknown();
} catch (Exception $e) {
echo "Exception caught OK; continuing.\n";
}
echo "\n\n---> Invoke __call via scope resolution operator within instance.\n";
try {
$a->test();
} catch (Exception $e) {
echo "Exception caught OK; continuing.\n";
}
echo "\n\n---> Invoke __call via scope resolution operator within child instance.\n";
$b = new B();
try {
$b->test();
} catch (Exception $e) {
echo "Exception caught OK; continuing.\n";
}
echo "\n\n---> Invoke __call via callback.\n";
try {
call_user_func(array($b, 'unknownCallback'), 1,2,3);
} catch (Exception $e) {
echo "Exception caught OK; continuing.\n";
}
?>
==DONE==
--EXPECTF--
---> Invoke __call via simple method call.
object(A)#%d (0) {
}
Exception caught OK; continuing.
---> Invoke __call via scope resolution operator within instance.
object(A)#%d (0) {
}
Exception caught OK; continuing.
---> Invoke __call via scope resolution operator within child instance.
object(B)#%d (0) {
}
Exception caught OK; continuing.
---> Invoke __call via callback.
object(B)#%d (0) {
}
Exception caught OK; continuing.
==DONE==

View File

@ -0,0 +1,55 @@
--TEST--
Ensure exceptions are handled properly when thrown in a statically declared __call.
--FILE--
<?php
class A {
static function __call($strMethod, $arrArgs) {
@var_dump($this);
throw new Exception;
echo "You should not see this";
}
function test() {
A::unknownCalledWithSRO(1,2,3);
}
}
class B extends A {
function test() {
B::unknownCalledWithSROFromChild(1,2,3);
}
}
$a = new A();
echo "---> Invoke __call via simple method call.\n";
try {
$a->unknown();
} catch (Exception $e) {
echo "Exception caught OK; continuing.\n";
}
echo "\n\n---> Invoke __call via scope resolution operator within instance.\n";
try {
$a->test();
} catch (Exception $e) {
echo "Exception caught OK; continuing.\n";
}
echo "\n\n---> Invoke __call via scope resolution operator within child instance.\n";
$b = new B();
try {
$b->test();
} catch (Exception $e) {
echo "Exception caught OK; continuing.\n";
}
echo "\n\n---> Invoke __call via callback.\n";
try {
call_user_func(array($b, 'unknownCallback'), 1,2,3);
} catch (Exception $e) {
echo "Exception caught OK; continuing.\n";
}
?>
==DONE==
--EXPECTF--
Fatal error: The magic method __call() must have public visibility and can not be static in %s on line 3

View File

@ -0,0 +1,146 @@
--TEST--
Implicit object instantiation when accessing properties of non-object.
--FILE--
<?php
class C {
// These values get implicitly converted to objects
public $boolFalse = false;
public $emptyString = '';
public $null = null;
// These values do not get implicitly converted to objects
public $boolTrue = true;
public $nonEmptyString = 'hello';
public $intZero = 0;
}
$c = new C;
foreach($c as $name => $value) {
echo "\n\n---( \$c->$name )---";
echo "\n --> Attempting implicit conversion to object using increment...\n";
$c->$name->prop++;
$c->$name = $value; // reset value in case implicit conversion was successful
echo "\n --> Attempting implicit conversion to object using assignment...\n";
$c->$name->prop = "Implicit instantiation!";
$c->$name = $value; // reset value in case implicit conversion was successful
echo "\n --> Attempting implicit conversion to object using combined assignment...\n";
$c->$name->prop .= " Implicit instantiation!";
}
echo "\n\n\n --> Resulting object:";
var_dump($c);
?>
--EXPECTF--
---( $c->boolFalse )---
--> Attempting implicit conversion to object using increment...
Strict Standards: Creating default object from empty value in %s on line 18
--> Attempting implicit conversion to object using assignment...
Strict Standards: Creating default object from empty value in %s on line 22
--> Attempting implicit conversion to object using combined assignment...
Strict Standards: Creating default object from empty value in %s on line 26
---( $c->emptyString )---
--> Attempting implicit conversion to object using increment...
Strict Standards: Creating default object from empty value in %s on line 18
--> Attempting implicit conversion to object using assignment...
Strict Standards: Creating default object from empty value in %s on line 22
--> Attempting implicit conversion to object using combined assignment...
Strict Standards: Creating default object from empty value in %s on line 26
---( $c->null )---
--> Attempting implicit conversion to object using increment...
Strict Standards: Creating default object from empty value in %s on line 18
--> Attempting implicit conversion to object using assignment...
Strict Standards: Creating default object from empty value in %s on line 22
--> Attempting implicit conversion to object using combined assignment...
Strict Standards: Creating default object from empty value in %s on line 26
---( $c->boolTrue )---
--> Attempting implicit conversion to object using increment...
Warning: Attempt to %s property of non-object in %s on line 18
--> Attempting implicit conversion to object using assignment...
Warning: Attempt to assign property of non-object in %s on line 22
--> Attempting implicit conversion to object using combined assignment...
Warning: Attempt to assign property of non-object in %s on line 26
---( $c->nonEmptyString )---
--> Attempting implicit conversion to object using increment...
Warning: Attempt to %s property of non-object in %s on line 18
--> Attempting implicit conversion to object using assignment...
Warning: Attempt to assign property of non-object in %s on line 22
--> Attempting implicit conversion to object using combined assignment...
Warning: Attempt to assign property of non-object in %s on line 26
---( $c->intZero )---
--> Attempting implicit conversion to object using increment...
Warning: Attempt to %s property of non-object in %s on line 18
--> Attempting implicit conversion to object using assignment...
Warning: Attempt to assign property of non-object in %s on line 22
--> Attempting implicit conversion to object using combined assignment...
Warning: Attempt to assign property of non-object in %s on line 26
--> Resulting object:object(C)#%d (6) {
[%u|b%"boolFalse"]=>
object(stdClass)#%d (1) {
[%u|b%"prop"]=>
%unicode|string%(24) " Implicit instantiation!"
}
[%u|b%"emptyString"]=>
object(stdClass)#%d (1) {
[%u|b%"prop"]=>
%unicode|string%(24) " Implicit instantiation!"
}
[%u|b%"null"]=>
object(stdClass)#%d (1) {
[%u|b%"prop"]=>
%unicode|string%(24) " Implicit instantiation!"
}
[%u|b%"boolTrue"]=>
bool(true)
[%u|b%"nonEmptyString"]=>
%unicode|string%(5) "hello"
[%u|b%"intZero"]=>
int(0)
}

View File

@ -0,0 +1,24 @@
--TEST--
Private property inheritance check
--FILE--
<?php
Class A {
private $c;
}
Class B extends A {
private $c;
}
Class C extends B {
}
var_dump(new C);
?>
--EXPECTF--
object(C)#%d (2) {
[%u|b%"c":%u|b%"B":private]=>
NULL
[%u|b%"c":%u|b%"A":private]=>
NULL
}

View File

@ -0,0 +1,39 @@
--TEST--
Ensure inherited old-style constructor doesn't block other methods.
--FILE--
<?php
class A {
public function B () { echo "In " . __METHOD__ . "\n"; }
public function A () { echo "In " . __METHOD__ . "\n"; }
}
class B extends A { }
$rc = new ReflectionClass('B');
var_dump($rc->getMethods());
$b = new B();
$b->a();
$b->b();
?>
--EXPECTF--
array(2) {
[0]=>
&object(ReflectionMethod)#%d (2) {
[%u|b%"name"]=>
%string|unicode%(1) "B"
[%u|b%"class"]=>
%string|unicode%(1) "B"
}
[1]=>
&object(ReflectionMethod)#%d (2) {
[%u|b%"name"]=>
%string|unicode%(1) "A"
[%u|b%"class"]=>
%string|unicode%(1) "B"
}
}
In A::A
In A::A
In A::B

View File

@ -0,0 +1,81 @@
--TEST--
Unsetting and recreating private properties.
--FILE--
<?php
class C {
private $p = 'test';
function unsetPrivate() {
unset($this->p);
}
function setPrivate() {
$this->p = 'changed';
}
}
class D extends C {
function setP() {
$this->p = 'changed in D';
}
}
echo "Unset and recreate a superclass's private property:\n";
$d = new D;
$d->unsetPrivate();
$d->setPrivate();
var_dump($d);
echo "\nUnset superclass's private property, and recreate it as public in subclass:\n";
$d = new D;
$d->unsetPrivate();
$d->setP();
var_dump($d);
echo "\nUnset superclass's private property, and recreate it as public at global scope:\n";
$d = new D;
$d->unsetPrivate();
$d->p = 'this will create a public property';
var_dump($d);
echo "\n\nUnset and recreate a private property:\n";
$c = new C;
$c->unsetPrivate();
$c->setPrivate();
var_dump($c);
echo "\nUnset a private property, and attempt to recreate at global scope (expecting failure):\n";
$c = new C;
$c->unsetPrivate();
$c->p = 'this will fail';
var_dump($c);
?>
==Done==
--EXPECTF--
Unset and recreate a superclass's private property:
object(D)#%d (1) {
[%u|b%"p":%u|b%"C":private]=>
%unicode|string%(7) "changed"
}
Unset superclass's private property, and recreate it as public in subclass:
object(D)#%d (1) {
[%u|b%"p"]=>
%unicode|string%(12) "changed in D"
}
Unset superclass's private property, and recreate it as public at global scope:
object(D)#%d (1) {
[%u|b%"p"]=>
%unicode|string%(34) "this will create a public property"
}
Unset and recreate a private property:
object(C)#%d (1) {
[%u|b%"p":%u|b%"C":private]=>
%unicode|string%(7) "changed"
}
Unset a private property, and attempt to recreate at global scope (expecting failure):
Fatal error: Cannot access private property C::$p in %s on line 46

View File

@ -0,0 +1,53 @@
--TEST--
Unsetting and recreating protected properties.
--FILE--
<?php
class C {
protected $p = 'test';
function unsetProtected() {
unset($this->p);
}
function setProtected() {
$this->p = 'changed';
}
}
class D extends C {
function setP() {
$this->p = 'changed in D';
}
}
$d = new D;
echo "Unset and recreate a protected property from property's declaring class scope:\n";
$d->unsetProtected();
$d->setProtected();
var_dump($d);
echo "\nUnset and recreate a protected property from subclass:\n";
$d = new D;
$d->unsetProtected();
$d->setP();
var_dump($d);
echo "\nUnset a protected property, and attempt to recreate it outside of scope (expected failure):\n";
$d->unsetProtected();
$d->p = 'this will fail';
var_dump($d);
?>
--EXPECTF--
Unset and recreate a protected property from property's declaring class scope:
object(D)#%d (1) {
[%u|b%"p":protected]=>
%unicode|string%(7) "changed"
}
Unset and recreate a protected property from subclass:
object(D)#%d (1) {
[%u|b%"p":protected]=>
%unicode|string%(12) "changed in D"
}
Unset a protected property, and attempt to recreate it outside of scope (expected failure):
Fatal error: Cannot access protected property %s::$p in %s on line 32

View File

@ -47,19 +47,19 @@ foreach($tests as $data)
?>
===DONE===
<?php exit(0); ?>
--EXPECT--
--EXPECTF--
==========
unicode(6) "String"
%unicode|string%(6) "String"
Test::__construct(String)
Test::serialize(String)
Test::unserialize(String)
object(Test)#1 (1) {
[u"data"]=>
unicode(6) "String"
object(Test)#%d (1) {
[%u|b%"data"]=>
%unicode|string%(6) "String"
}
object(Test)#1 (1) {
[u"data"]=>
unicode(6) "String"
object(Test)#%d (1) {
[%u|b%"data"]=>
%unicode|string%(6) "String"
}
==========
NULL

View File

@ -0,0 +1,49 @@
--TEST--
Attempting to access static properties using instance property syntax
--FILE--
<?php
class C {
public static $x = 'C::$x';
protected static $y = 'C::$y';
}
$c = new C;
echo "\n--> Access visible static prop like instance prop:\n";
var_dump(isset($c->x));
unset($c->x);
echo $c->x;
$c->x = 1;
$ref = 'ref';
$c->x =& $ref;
var_dump($c->x, C::$x);
echo "\n--> Access non-visible static prop like instance prop:\n";
var_dump(isset($c->y));
//unset($c->y); // Fatal error, tested in static_properties_003_error1.phpt
//echo $c->y; // Fatal error, tested in static_properties_003_error2.phpt
//$c->y = 1; // Fatal error, tested in static_properties_003_error3.phpt
//$c->y =& $ref; // Fatal error, tested in static_properties_003_error4.phpt
?>
==Done==
--EXPECTF--
--> Access visible static prop like instance prop:
bool(false)
Strict Standards: Accessing static property C::$x as non static in %s on line 11
Strict Standards: Accessing static property C::$x as non static in %s on line 12
Notice: Undefined property: C::$x in %s on line 12
Strict Standards: Accessing static property C::$x as non static in %s on line 13
Strict Standards: Accessing static property C::$x as non static in %s on line 15
Strict Standards: Accessing static property C::$x as non static in %s on line 16
%unicode|string%(3) "ref"
%unicode|string%(5) "C::$x"
--> Access non-visible static prop like instance prop:
bool(false)
==Done==

View File

@ -0,0 +1,18 @@
--TEST--
Attempting to access static properties using instance property syntax
--FILE--
<?php
class C {
protected static $y = 'C::$y';
}
$c = new C;
echo "\n--> Access non-visible static prop like instance prop:\n";
unset($c->y);
?>
==Done==
--EXPECTF--
--> Access non-visible static prop like instance prop:
Fatal error: Cannot access protected property C::$y in %s on line 8

View File

@ -0,0 +1,18 @@
--TEST--
Attempting to access static properties using instance property syntax
--FILE--
<?php
class C {
protected static $y = 'C::$y';
}
$c = new C;
echo "\n--> Access non-visible static prop like instance prop:\n";
echo $c->y;
?>
==Done==
--EXPECTF--
--> Access non-visible static prop like instance prop:
Fatal error: Cannot access protected property C::$y in %s on line 8

View File

@ -0,0 +1,18 @@
--TEST--
Attempting to access static properties using instance property syntax
--FILE--
<?php
class C {
protected static $y = 'C::$y';
}
$c = new C;
echo "\n--> Access non-visible static prop like instance prop:\n";
$c->y = 1;
?>
==Done==
--EXPECTF--
--> Access non-visible static prop like instance prop:
Fatal error: Cannot access protected property C::$y in %s on line 8

View File

@ -0,0 +1,18 @@
--TEST--
Attempting to access static properties using instance property syntax
--FILE--
<?php
class C {
protected static $y = 'C::$y';
}
$c = new C;
echo "\n--> Access non-visible static prop like instance prop:\n";
$c->y =& $ref;
?>
==Done==
--EXPECTF--
--> Access non-visible static prop like instance prop:
Fatal error: Cannot access protected property C::$y in %s on line 8

View File

@ -0,0 +1,37 @@
--TEST--
Inherited static properties can be separated from their reference set.
--FILE--
<?php
class C { public static $p = 'original'; }
class D extends C { }
class E extends D { }
echo "\nInherited static properties refer to the same value accross classes:\n";
var_dump(C::$p, D::$p, E::$p);
echo "\nChanging one changes all the others:\n";
D::$p = 'changed.all';
var_dump(C::$p, D::$p, E::$p);
echo "\nBut because this is implemented using PHP references, the reference set can easily be split:\n";
$ref = 'changed.one';
D::$p =& $ref;
var_dump(C::$p, D::$p, E::$p);
?>
==Done==
--EXPECTF--
Inherited static properties refer to the same value accross classes:
%unicode|string%(8) "original"
%unicode|string%(8) "original"
%unicode|string%(8) "original"
Changing one changes all the others:
%unicode|string%(11) "changed.all"
%unicode|string%(11) "changed.all"
%unicode|string%(11) "changed.all"
But because this is implemented using PHP references, the reference set can easily be split:
%unicode|string%(11) "changed.all"
%unicode|string%(11) "changed.one"
%unicode|string%(11) "changed.all"
==Done==

View File

@ -0,0 +1,15 @@
--TEST--
Check type hint compatibility in overrides with array hints.
--FILE--
<?php
Class C { function f(array $a) {} }
echo "Compatible hint.\n";
Class D1 extends C { function f(array $a) {} }
echo "Class hint, should be array.\n";
Class D2 extends C { function f(SomeClass $a) {} }
?>
==DONE==
--EXPECTF--
Fatal error: Declaration of D2::f() must be compatible with that of C::f() in %s on line 8

View File

@ -0,0 +1,12 @@
--TEST--
Check type hint compatibility in overrides with array hints.
--FILE--
<?php
Class C { function f(array $a) {} }
echo "No hint, should be array.\n";
Class D extends C { function f($a) {} }
?>
==DONE==
--EXPECTF--
Fatal error: Declaration of D::f() must be compatible with that of C::f() in %s on line 5

View File

@ -0,0 +1,12 @@
--TEST--
Check type hint compatibility in overrides with array hints.
--FILE--
<?php
Class C { function f(SomeClass $a) {} }
echo "Array hint, should be class.\n";
Class D extends C { function f(array $a) {} }
?>
==DONE==
--EXPECTF--
Fatal error: Declaration of D::f() must be compatible with that of C::f() in %s on line 5

View File

@ -0,0 +1,12 @@
--TEST--
Check type hint compatibility in overrides with array hints.
--FILE--
<?php
Class C { function f($a) {} }
echo "Array hint, should be nothing.\n";
Class D extends C { function f(array $a) {} }
?>
==DONE==
--EXPECTF--
Fatal error: Declaration of D::f() must be compatible with that of C::f() in %s on line 5