Skip to content

Commit 76f6592

Browse files
committed
ext/standard: Add tests for Directory class
1 parent 6bf5bde commit 76f6592

8 files changed

+146
-45
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Cannot serialize instance of Directory class constructed via Reflection.
3+
--FILE--
4+
<?php
5+
6+
$d = dir(__DIR__);
7+
try {
8+
$cloned = clone $d;
9+
$cloned_files = [];
10+
while ($row = $cloned->read()){
11+
$cloned_files[] = $row;
12+
}
13+
var_dump(count($cloned_files));
14+
echo "Using original object:\n";
15+
$original_files = [];
16+
while ($row = $d->read()){
17+
$original_files[] = $row;
18+
}
19+
var_dump(count($original_files));
20+
} catch (\Throwable $e) {
21+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
22+
}
23+
24+
?>
25+
--EXPECT--
26+
int(17)
27+
Using original object:
28+
int(0)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Cannot directly instantiate Directory class.
3+
--FILE--
4+
<?php
5+
6+
try {
7+
$d = new Directory();
8+
var_dump($d);
9+
} catch (\Throwable $e) {
10+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
11+
}
12+
13+
?>
14+
--EXPECT--
15+
object(Directory)#1 (0) {
16+
["path"]=>
17+
uninitialized(string)
18+
["handle"]=>
19+
uninitialized(mixed)
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Cannot serialize instance of Directory class constructed via Reflection.
3+
--FILE--
4+
<?php
5+
6+
$d = dir(__DIR__);
7+
try {
8+
$s = serialize($d);
9+
var_dump($s);
10+
} catch (\Throwable $e) {
11+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
12+
}
13+
14+
?>
15+
--EXPECTF--
16+
string(%d) "O:9:"Directory":2:{s:4:"path";s:%d:"%s";s:6:"handle";i:%d;}"

ext/standard/tests/directory/DirectoryClass_error_001.phpt

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Changing Directory::$handle property
3+
--FILE--
4+
<?php
5+
6+
$d = dir(__DIR__);
7+
try {
8+
$d->handle = "Havoc!";
9+
} catch (\Throwable $e) {
10+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
11+
}
12+
var_dump($d->handle);
13+
14+
try {
15+
unset($d->handle);
16+
} catch (\Throwable $e) {
17+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
18+
}
19+
var_dump($d->handle);
20+
21+
?>
22+
--EXPECTF--
23+
Error: Cannot modify readonly property Directory::$handle
24+
resource(%d) of type (stream)
25+
Error: Cannot unset readonly property Directory::$handle
26+
resource(%d) of type (stream)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Changing Directory::$handle property
3+
--FILE--
4+
<?php
5+
6+
$d = dir(__DIR__);
7+
try {
8+
$d->path = "Havoc!";
9+
} catch (\Throwable $e) {
10+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
11+
}
12+
var_dump($d->path == __DIR__);
13+
14+
try {
15+
unset($d->path);
16+
} catch (\Throwable $e) {
17+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
18+
}
19+
var_dump($d->path == __DIR__);
20+
21+
?>
22+
--EXPECTF--
23+
Error: Cannot modify readonly property Directory::$path
24+
bool(true)
25+
Error: Cannot unset readonly property Directory::$path
26+
bool(true)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Cannot use instance of Directory class constructed via Reflection.
3+
--FILE--
4+
<?php
5+
6+
$rc = new ReflectionClass("Directory");
7+
var_dump($rc->isInstantiable());
8+
try {
9+
$d = $rc->newInstanceWithoutConstructor();
10+
} catch (\Throwable $e) {
11+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
12+
}
13+
14+
var_dump($d);
15+
try {
16+
var_dump($d->read());
17+
} catch (\Throwable $e) {
18+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
19+
}
20+
21+
?>
22+
--EXPECT--
23+
bool(true)
24+
object(Directory)#2 (0) {
25+
["path"]=>
26+
uninitialized(string)
27+
["handle"]=>
28+
uninitialized(mixed)
29+
}
30+
Error: Unable to find my handle property

ext/standard/tests/directory/DirectoryClass_basic_001.phpt renamed to ext/standard/tests/directory/DirectoryClass_reflection_structure.phpt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ echo "Structure of Directory class:\n";
1111
$rc = new ReflectionClass("Directory");
1212
echo $rc;
1313

14-
echo "Cannot instantiate a valid Directory directly:\n";
15-
$d = new Directory(getcwd());
16-
var_dump($d);
17-
18-
try {
19-
var_dump($d->read());
20-
} catch (\Error $e) {
21-
echo $e->getMessage() . "\n";
22-
}
23-
2414
?>
2515
--EXPECTF--
2616
Structure of Directory class:
@@ -63,11 +53,3 @@ Class [ <internal%s> class Directory ] {
6353
}
6454
}
6555
}
66-
Cannot instantiate a valid Directory directly:
67-
object(Directory)#%d (0) {
68-
["path"]=>
69-
uninitialized(string)
70-
["handle"]=>
71-
uninitialized(mixed)
72-
}
73-
Unable to find my handle property

0 commit comments

Comments
 (0)