Skip to content

Commit 73c6fb0

Browse files
committed
Adding tests for native weak references
1 parent 4ab5ae1 commit 73c6fb0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace TheCodingMachine\TDBM;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class WeakreferenceObjectStorageTest extends TestCase
8+
{
9+
public function testObjectStorage(): void
10+
{
11+
if (!\class_exists(\WeakReference::class)) {
12+
$this->markTestSkipped('PHP 7.4+ only');
13+
return;
14+
}
15+
$objectStorage = new NativeWeakrefObjectStorage();
16+
$this->assertNull($objectStorage->get('foo', 42));
17+
$dbRow = $this->createMock(DbRow::class);
18+
$objectStorage->set('foo', 42, $dbRow);
19+
$this->assertSame($dbRow, $objectStorage->get('foo', 42));
20+
$objectStorage->remove('foo', 42);
21+
$this->assertNull($objectStorage->get('foo', 42));
22+
}
23+
24+
public function testDanglingPointers(): void
25+
{
26+
if (!\class_exists(\WeakReference::class)) {
27+
$this->markTestSkipped('PHP 7.4+ only');
28+
return;
29+
}
30+
$objectStorage = new NativeWeakrefObjectStorage();
31+
$dbRow = $this->createMock(DbRow::class);
32+
33+
for ($i=0; $i<10001; $i++) {
34+
$objectStorage->set('foo', $i, clone $dbRow);
35+
}
36+
$this->assertNull($objectStorage->get('foo', 42));
37+
}
38+
}

0 commit comments

Comments
 (0)