This guide explains how to run tests for the SmartCache Laravel package without needing a full Laravel installation.
The test suite uses:
- PHPUnit 10 - Testing framework
- Orchestra Testbench - Minimal Laravel environment for package testing
- Mockery - Mocking framework for isolated unit tests
First, install the development dependencies:
composer installcomposer test
# or
vendor/bin/phpunitCurrent suite size:
# 435 tests, 1,821 assertions# Unit tests only
vendor/bin/phpunit --testsuite=Unit
# Feature tests only
vendor/bin/phpunit --testsuite=Feature
# Specific test file
vendor/bin/phpunit tests/Unit/SmartCacheTest.php
# Specific test method
vendor/bin/phpunit --filter test_can_store_and_retrieve_simple_valuecomposer test-coverage
# Generates HTML coverage report in ./coverage/ directoryWhen contributing to SmartCache, follow these testing guidelines:
- Isolated functionality - Test individual components
- Mock external dependencies - Use Laravel's testing mocks
- Edge case coverage - Test boundary conditions
- Error scenarios - Ensure graceful failure handling
- Real Laravel environment - Use Orchestra Testbench
- Integration scenarios - Test component interactions
- End-to-end workflows - Validate complete user journeys
- Performance validation - Ensure optimization benefits
# Generate test data of different sizes
$smallData = range(1, 10); # No optimization
$mediumData = range(1, 1000); # May trigger compression
$largeData = range(1, 5000); # Likely chunking + compressionIf you have multiple PHP versions installed:
php8.1 vendor/bin/phpunit
php8.2 vendor/bin/phpunit
php8.3 vendor/bin/phpunit
php8.4 vendor/bin/phpunitThe TestCase class extends Orchestra Testbench, providing:
- Minimal Laravel application environment
- Service container with Laravel services
- Configuration management
- Cache system simulation
Built-in helper methods for creating test data:
$this->createCompressibleData(); // Large repetitive string
$this->createChunkableData(); // Large array for chunking
$this->createLargeTestData(100); // Array with 100 complex itemsSpecialized assertions for testing optimizations:
$this->assertValueIsOptimized($original, $cached);
$this->assertValueIsCompressed($value);
$this->assertValueIsChunked($value);Tests cover various configuration scenarios:
- Strategies enabled/disabled
- Custom thresholds
- Different compression levels
- Custom chunk sizes
Command tests verify:
- Correct output formatting
- Proper exit codes
- Integration with SmartCache service
- Error handling
Audit and benchmark commands also have focused coverage:
vendor/bin/phpunit tests/Unit/Console/AuditCommandTest.php
vendor/bin/phpunit tests/Unit/Console/BenchCommandTest.phpsmart-cache:audit tests cover healthy reports, JSON output, and broken chunk detection. smart-cache:bench tests cover profile execution, JSON output, report file writing, data integrity, and key-shape preservation.
The package can generate a reproducible benchmark report without a full Laravel app by using Orchestra Testbench. Prefer Redis for public-facing examples when it is available locally:
vendor/bin/testbench smart-cache:bench --driver=redis --iterations=10 --format=json --output=docs/benchmark-report-redis.jsonIf Redis is not configured, use --driver=array as a command-behavior smoke test, but avoid presenting it as cache-backend performance. Benchmark numbers depend on hardware, PHP version, Laravel version, cache driver, serialization settings, and payload shape; run the command against Redis, Memcached, file, or database stores in your own environment before making production sizing decisions.
The package tests run completely independently using Orchestra Testbench:
- No Laravel Project Required - Tests create a minimal Laravel environment
- Isolated Dependencies - Only requires packages specified in
composer.json - Cross-Version Compatible - Works with Laravel 8.0+ and PHP 8.1+
- Fast Execution - No database migrations or complex setup
Test environment is configured in TestCase::defineEnvironment():
- Cache driver:
array(in-memory) - Debug mode: enabled
- Optimized thresholds for testing (lower than production)
The test suite is designed to work well in CI environments:
# Example GitHub Actions configuration
- name: Run Tests
run: |
composer install
vendor/bin/phpunit --coverage-clover=coverage.xmlvendor/bin/phpunit --debugvendor/bin/phpunit --stop-on-failurevendor/bin/phpunit --verboseThe package can be tested against different cache drivers by modifying the test configuration.
<?php
namespace SmartCache\Tests\Unit;
use SmartCache\Tests\TestCase;
class MyNewTest extends TestCase
{
public function test_my_functionality()
{
$smartCache = $this->app->make(\SmartCache\Contracts\SmartCache::class);
// Test your functionality
$smartCache->put('test-key', 'test-value');
$this->assertEquals('test-value', $smartCache->get('test-key'));
}
}<?php
namespace SmartCache\Tests\Feature;
use SmartCache\Tests\TestCase;
use SmartCache\Contracts\SmartCache;
class MyIntegrationTest extends TestCase
{
public function test_complete_workflow()
{
$smartCache = $this->app->make(SmartCache::class);
// Test complete user workflow
$largeData = $this->createCompressibleData();
$smartCache->put('integration-test', $largeData, 3600);
$retrieved = $smartCache->get('integration-test');
$this->assertEquals($largeData, $retrieved);
// Verify optimization occurred
$managedKeys = $smartCache->getManagedKeys();
$this->assertContains('integration-test', $managedKeys);
}
}-
Memory Limit Errors
php -dmemory_limit=1G vendor/bin/phpunit
-
Missing Extensions Ensure required PHP extensions are installed:
ext-zlib(for compression)ext-jsonext-mbstring
-
Composer Autoload Issues
composer dump-autoload
-
Cache Permission Issues The test bootstrap creates temporary cache directories automatically.
- Check the test output for detailed error messages
- Use
--debugflag for additional debugging information - Review the test configuration in
phpunit.xml - Examine the base
TestCaseclass for available helpers
- Unit tests should complete in under 1 second each
- Feature tests may take longer due to data processing
- Use smaller test datasets when possible
- Clean up test data in tearDown methods