ext/standard: Add tests for Directory class

This commit is contained in:
Gina Peter Banyard 2024-09-24 17:58:22 +01:00
parent 6bf5bde107
commit 76f6592d12
8 changed files with 146 additions and 45 deletions

View file

@ -0,0 +1,28 @@
--TEST--
Cannot serialize instance of Directory class constructed via Reflection.
--FILE--
<?php
$d = dir(__DIR__);
try {
$cloned = clone $d;
$cloned_files = [];
while ($row = $cloned->read()){
$cloned_files[] = $row;
}
var_dump(count($cloned_files));
echo "Using original object:\n";
$original_files = [];
while ($row = $d->read()){
$original_files[] = $row;
}
var_dump(count($original_files));
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
int(17)
Using original object:
int(0)

View file

@ -0,0 +1,20 @@
--TEST--
Cannot directly instantiate Directory class.
--FILE--
<?php
try {
$d = new Directory();
var_dump($d);
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
object(Directory)#1 (0) {
["path"]=>
uninitialized(string)
["handle"]=>
uninitialized(mixed)
}

View file

@ -0,0 +1,16 @@
--TEST--
Cannot serialize instance of Directory class constructed via Reflection.
--FILE--
<?php
$d = dir(__DIR__);
try {
$s = serialize($d);
var_dump($s);
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
?>
--EXPECTF--
string(%d) "O:9:"Directory":2:{s:4:"path";s:%d:"%s";s:6:"handle";i:%d;}"

View file

@ -1,27 +0,0 @@
--TEST--
Changing Directory::$handle property
--FILE--
<?php
$d = dir(getcwd());
try {
$d->handle = "Havoc!";
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump($d->handle);
$d = dir(getcwd());
try {
unset($d->handle);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump($d->handle);
?>
--EXPECTF--
Cannot modify readonly property Directory::$handle
resource(%d) of type (stream)
Cannot unset readonly property Directory::$handle
resource(%d) of type (stream)

View file

@ -0,0 +1,26 @@
--TEST--
Changing Directory::$handle property
--FILE--
<?php
$d = dir(__DIR__);
try {
$d->handle = "Havoc!";
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
var_dump($d->handle);
try {
unset($d->handle);
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
var_dump($d->handle);
?>
--EXPECTF--
Error: Cannot modify readonly property Directory::$handle
resource(%d) of type (stream)
Error: Cannot unset readonly property Directory::$handle
resource(%d) of type (stream)

View file

@ -0,0 +1,26 @@
--TEST--
Changing Directory::$handle property
--FILE--
<?php
$d = dir(__DIR__);
try {
$d->path = "Havoc!";
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
var_dump($d->path == __DIR__);
try {
unset($d->path);
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
var_dump($d->path == __DIR__);
?>
--EXPECTF--
Error: Cannot modify readonly property Directory::$path
bool(true)
Error: Cannot unset readonly property Directory::$path
bool(true)

View file

@ -0,0 +1,30 @@
--TEST--
Cannot use instance of Directory class constructed via Reflection.
--FILE--
<?php
$rc = new ReflectionClass("Directory");
var_dump($rc->isInstantiable());
try {
$d = $rc->newInstanceWithoutConstructor();
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
var_dump($d);
try {
var_dump($d->read());
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
bool(true)
object(Directory)#2 (0) {
["path"]=>
uninitialized(string)
["handle"]=>
uninitialized(mixed)
}
Error: Unable to find my handle property

View file

@ -11,16 +11,6 @@ echo "Structure of Directory class:\n";
$rc = new ReflectionClass("Directory");
echo $rc;
echo "Cannot instantiate a valid Directory directly:\n";
$d = new Directory(getcwd());
var_dump($d);
try {
var_dump($d->read());
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}
?>
--EXPECTF--
Structure of Directory class:
@ -63,11 +53,3 @@ Class [ <internal%s> class Directory ] {
}
}
}
Cannot instantiate a valid Directory directly:
object(Directory)#%d (0) {
["path"]=>
uninitialized(string)
["handle"]=>
uninitialized(mixed)
}
Unable to find my handle property