php-ds/tests
Test suite package for php-ds, providing automated tests and fixtures to verify correct behavior, edge cases, and performance expectations of PHP Data Structures. Useful for contributors and CI to ensure changes don’t break core collections.
Install Dependencies:
composer require --dev php-ds/php-ds phpunit/phpunit ^11
Ensure your phpunit.xml targets PHP 8.2+.
First Use Case:
Run the SeqTest (Sequence tests) to verify basic php-ds functionality:
./vendor/bin/phpunit vendor/php-ds/tests/SeqTest.php
Focus on SeqTest or MapTest if evaluating php-ds\Seq or php-ds\Map for Laravel.
Where to Look First:
tests/Seq/ or tests/Map/ for trait-per-method organization (e.g., testPushPop.php).v2.0.0.md for API shifts (e.g., KeyObject replaces HashableObject).MapTest for 0.0/-0.0 key handling (relevant for financial/caching use cases).Trait-Based Testing:
Use traits like SeqTest::testPushPop() to validate php-ds\Seq operations in custom Laravel services:
use php\ds\Seq;
use php\ds\tests\SeqTest;
class LaravelSeqServiceTest extends TestCase
{
use SeqTest;
public function testLaravelSeqOperations()
{
$seq = new Seq();
$this->testPushPop($seq); // Reuse trait logic
// Add Laravel-specific assertions
}
}
Benchmark-Driven Adoption:
Collection with php-ds\Map for O(1) lookups:
use php\ds\Map;
$cache = new Map();
$cache->put('user:1', $userModel);
$this->assertEquals($userModel, $cache->get('user:1')); // Faster than Collection::where()
php-ds in a Laravel-friendly facade (e.g., PhpDsCollection) to maintain API consistency.Event-Driven Workflows:
Use php-ds\Deque (via SeqTest traits) for job queues:
use php\ds\Deque;
class JobQueueService
{
public function __construct(private Deque $queue) {}
public function pushJob($job)
{
$this->queue->push($job);
}
}
Refactor Legacy Code:
SplQueue implementations.php-ds\Deque and reuse SeqTest traits for validation.composer remove ext-spl
composer require php-ds/php-ds
Testing Strategy:
php-ds tests in isolation:
./vendor/bin/phpunit --testsuite PhpDs
SeqTest).Integration with Laravel:
array/Collection caches with php-ds\Map:
$cache = new Map();
Cache::extend('phpds', function () use ($cache) {
return new class($cache) implements CacheStore {
// ...
};
});
PHPUnit Configuration:
Add a separate test suite in phpunit.xml:
<testsuites>
<testsuite name="PhpDs">
<directory>./vendor/php-ds/tests</directory>
<exclude>*/VectorTest.php</exclude> <!-- Skip irrelevant tests -->
</testsuite>
</testsuites>
Type Safety:
Leverage php-ds’s generics (e.g., Seq<int>) in Laravel services:
use php\ds\Seq;
class OrderProcessor
{
public function __construct(private Seq<int> $orderIds) {}
}
Debugging:
Use php-ds’s dump() method for inspection:
$map = new Map();
$map->put('key', 'value');
\php\ds\dump($map); // Visualize structure
PHPUnit Version Lock:
docker run --rm -v $(pwd):/app composer:latest require phpunit/phpunit:^11 --dev
API Breaking Changes:
v2.0.0 removed VectorTest/DequeTest; traits are now inlined.php-ds v2:
// Old (v1.x)
use php\ds\tests\VectorTest;
// New (v2.x)
use php\ds\tests\SeqTest;
Laravel Test Helpers:
DatabaseTestCase won’t work with php-ds tests.TestCase directly or mock dependencies:
use Tests\TestCase as BaseTestCase;
class PhpDsTest extends BaseTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->withoutExceptionHandling(); // For strict php-ds errors
}
}
Key Interface:
KeyObject (implements Ds\Key) is stricter than HashableObject.Ds\Key:
class UserKey implements Ds\Key
{
public function __hash(): int { return hash('sha256', $this->id); }
}
Test Failures:
Key interface violations.-d error_reporting=E_ALL:
./vendor/bin/phpunit -d error_reporting=E_ALL vendor/php-ds/tests/MapTest.php
Performance Mismatches:
php-ds operations slower than expected in Laravel.Collection benchmarks:
Benchmark::group('Map vs Collection', function () {
Benchmark::run('Map::get', fn () => $map->get('key'));
Benchmark::run('Collection::where', fn () => $collection->where('key', 'value')->first());
});
Memory Leaks:
php-ds structures retain references unexpectedly.clear() or drop() explicitly:
$seq->clear(); // Free memory
Autoloading:
php-ds classes may not autoload in Laravel.composer.json includes:
"autoload": {
"psr-4": {
"php\\ds\\": "vendor/php-ds/src"
}
}
IDE Support:
php-ds types.phpstan.neon:
includes:
- vendor/php-ds/extension.neon
Custom Traits:
trait LaravelSeqTest
{
use \php\ds\tests\SeqTest;
public function testLaravelSpecific()
{
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $this->convertToCollection($this->seq));
}
protected function convertToCollection(Seq $seq): Collection
{
return collect(iterator_to_array($seq));
}
}
Benchmarking:
php-ds tests as a baseline for Laravel optimizations:
use php\ds\tests\MapTest;
class LaravelMapBenchmark extends MapTest
{
public function testVsLaravelCollection()
{
$this->assertLessThan(100, $this->benchmarkMapVsCollection());
}
}
Polyfill Integration:
php-ds/polyfill for backward compatibility:
use php\ds\polyfill\Map;
$map = new
How can I help you explore Laravel packages today?