Skip to content

Commit 8800532

Browse files
committed
Add tests for function/class declared in a file included repeatedly
1 parent 5ed654e commit 8800532

8 files changed

+166
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
$className = AnonymousClassNameCache::get_class(fn () => new class() {
4+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
Repeated include must not increase memory - anonymous class
3+
--EXTENSIONS--
4+
opcache
5+
--FILE--
6+
<?php
7+
8+
// TODO test should pass even without opcache, but fixes are needed
9+
// related bug tracker https://bugs.php.net/bug.php?id=76982
10+
11+
// also, new anonymous class included multiple times always get a new class name,
12+
// this test accounts for that and tests if anonymous class declared in a closure,
13+
// invoked only once, does not increase memory consumption
14+
15+
final class AnonymousClassNameCache
16+
{
17+
/** @var array<string, string> */
18+
private static $classNameByFxHash = [];
19+
20+
private function __construct()
21+
{
22+
}
23+
24+
public static function get_class(\Closure $createAnonymousClassFx): string
25+
{
26+
$fxRefl = new \ReflectionFunction($createAnonymousClassFx);
27+
$fxHash = $fxRefl->getFileName() . ':' . $fxRefl->getStartLine() . '-' . $fxRefl->getEndLine();
28+
29+
if (!isset(self::$classNameByFxHash[$fxHash])) {
30+
self::$classNameByFxHash[$fxHash] = get_class($createAnonymousClassFx());
31+
}
32+
33+
return self::$classNameByFxHash[$fxHash];
34+
}
35+
}
36+
37+
$classNamePrev = null;
38+
$m = -1;
39+
$mDiff = -1;
40+
$mPrev = 0;
41+
for ($i = 0; $i < 10 * 1000; $i++) {
42+
require __DIR__ . '/anonymous_class_in_closure.inc';
43+
if ($classNamePrev !== null && $className !== $classNamePrev) {
44+
echo 'Class name is different: ' . $className . ' vs ' . $classNamePrev . ' (' . $i . ' iteration)' . "\n";
45+
exit(1);
46+
}
47+
$classNamePrev = $className;
48+
$m = memory_get_usage();
49+
$mDiff = $m - $mPrev;
50+
if ($mPrev !== 0 && $mDiff !== 0) {
51+
echo 'Increased memory detected: ' . $mDiff . ' B (' . $i . ' iteration)' . "\n";
52+
exit(1);
53+
}
54+
$mPrev = $m;
55+
}
56+
echo 'done';
57+
58+
?>
59+
--EXPECT--
60+
done
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
function () {
4+
};
5+
6+
fn () => 'test';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Repeated include must not increase memory - anonymous function
3+
--EXTENSIONS--
4+
opcache
5+
--FILE--
6+
<?php
7+
8+
// TODO test should pass even without opcache, but fixes are needed
9+
// related bug tracker https://bugs.php.net/bug.php?id=76982
10+
11+
$m = -1;
12+
$mDiff = -1;
13+
$mPrev = 0;
14+
for ($i = 0; $i < 10 * 1000; $i++) {
15+
require __DIR__ . '/anonymous_function.inc';
16+
$m = memory_get_usage();
17+
$mDiff = $m - $mPrev;
18+
if ($mPrev !== 0 && $mDiff !== 0) {
19+
echo 'Increased memory detected: ' . $mDiff . ' B (' . $i . ' iteration)' . "\n";
20+
exit(1);
21+
}
22+
$mPrev = $m;
23+
}
24+
echo 'done';
25+
26+
?>
27+
--EXPECT--
28+
done
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
if (!class_exists(Test::class)) {
4+
class Test {
5+
}
6+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Repeated include must not increase memory - class in if condition
3+
--EXTENSIONS--
4+
opcache
5+
--FILE--
6+
<?php
7+
8+
// TODO test should pass even without opcache, but fixes are needed
9+
// related bug tracker https://bugs.php.net/bug.php?id=76982
10+
11+
$m = -1;
12+
$mDiff = -1;
13+
$mPrev = 0;
14+
for ($i = 0; $i < 10 * 1000; $i++) {
15+
require __DIR__ . '/class_declaration_in_if.inc';
16+
$m = memory_get_usage();
17+
$mDiff = $m - $mPrev;
18+
if ($mPrev !== 0 && $mDiff !== 0) {
19+
echo 'Increased memory detected: ' . $mDiff . ' B (' . $i . ' iteration)' . "\n";
20+
exit(1);
21+
}
22+
$mPrev = $m;
23+
}
24+
echo 'done';
25+
26+
?>
27+
--EXPECT--
28+
done
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
if (!function_exists('test')) {
4+
function test() {
5+
}
6+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Repeated include must not increase memory - function in if condition
3+
--EXTENSIONS--
4+
opcache
5+
--FILE--
6+
<?php
7+
8+
// TODO test should pass even without opcache, but fixes are needed
9+
// related bug tracker https://bugs.php.net/bug.php?id=76982
10+
11+
$m = -1;
12+
$mDiff = -1;
13+
$mPrev = 0;
14+
for ($i = 0; $i < 10 * 1000; $i++) {
15+
require __DIR__ . '/function_declaration_in_if.inc';
16+
$m = memory_get_usage();
17+
$mDiff = $m - $mPrev;
18+
if ($mPrev !== 0 && $mDiff !== 0) {
19+
echo 'Increased memory detected: ' . $mDiff . ' B (' . $i . ' iteration)' . "\n";
20+
exit(1);
21+
}
22+
$mPrev = $m;
23+
}
24+
echo 'done';
25+
26+
?>
27+
--EXPECT--
28+
done

0 commit comments

Comments
 (0)