Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
815d8c1
Enhance composer.json and composer.lock to include development depend…
iazaran Aug 31, 2025
97f9b55
Add real-world test results and performance comparison to documentation
iazaran Aug 31, 2025
a01e670
Remove example usage file for SmartCache, streamlining the repository…
iazaran Aug 31, 2025
48191ff
Refactor SmartCache to utilize CacheManager for improved cache handli…
iazaran Aug 31, 2025
c8b615d
Add initial test suite for SmartCache package, including bootstrap, T…
iazaran Aug 31, 2025
45ca411
Update .gitignore to include PHPUnit cache, coverage, and macOS syste…
iazaran Aug 31, 2025
f6baaa5
Add PHPUnit configuration and comprehensive testing guide for SmartCa…
iazaran Aug 31, 2025
1db4958
Add GitHub workflows for code analysis and testing, and include a pul…
iazaran Aug 31, 2025
9f83b94
Rename GitHub workflow from Code Analysis to Coverage and update job …
iazaran Aug 31, 2025
8dbc7c7
Update GitHub Actions workflow to target PHP 8.2, 8.3 and Laravel 11,…
iazaran Aug 31, 2025
46bf05b
Update composer.json and composer.lock to support PHP 8.2 and Laravel…
iazaran Aug 31, 2025
dcc80fe
Update GitHub Actions workflow to include support for Laravel 12 in a…
iazaran Aug 31, 2025
aeaedc2
Update composer.json and composer.lock to support Laravel 12, PHP 8.2
iazaran Aug 31, 2025
4ac5435
Remove SmartCache Test Suite from PHPUnit configuration to streamline…
iazaran Aug 31, 2025
8bd577c
Update PHPUnit test scripts in composer.json to use --testdox format …
iazaran Aug 31, 2025
2a7c8dc
Update GitHub Actions workflows to use --testdox format for PHPUnit e…
iazaran Aug 31, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Pull Request

## Description
Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Fixes # (issue)

## Type of change
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update

## How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce.

- [ ] Test A
- [ ] Test B

**Test Configuration**:
* PHP version:
* Laravel version:

## Checklist:
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream modules
30 changes: 30 additions & 0 deletions .github/workflows/code-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Coverage

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]

jobs:
coverage:
runs-on: ubuntu-latest
name: Coverage Report (PHP 8.3)

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: xdebug

- name: Install dependencies
run: |
composer update --prefer-stable --prefer-dist --no-interaction

- name: Execute tests with coverage
run: vendor/bin/phpunit --coverage-text --testdox
52 changes: 52 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Tests

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]

jobs:
test:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php: [8.2, 8.3]
laravel: [11.*, 12.*]
stability: [prefer-stable]
include:
- laravel: 11.*
testbench: ^9.0
- laravel: 12.*
testbench: ^10.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction

- name: List Installed Dependencies
run: composer show -D

- name: Execute tests
run: vendor/bin/phpunit --testdox
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
vendor/
.phpunit.cache/
coverage/
.DS_Store
269 changes: 269 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
# SmartCache Testing Guide

This guide explains how to run tests for the SmartCache Laravel package without needing a full Laravel installation.

## Overview

The test suite uses:
- **PHPUnit 10** - Testing framework
- **Orchestra Testbench** - Minimal Laravel environment for package testing
- **Mockery** - Mocking framework for isolated unit tests

## Installation

First, install the development dependencies:

```bash
composer install
```

## Running Tests

### Run All Tests
```bash
composer test
# or
vendor/bin/phpunit
```

### Run Specific Test Suites
```bash
# 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_value
```

### Generate Coverage Report
```bash
composer test-coverage
# Generates HTML coverage report in ./coverage/ directory
```

### Test with Different PHP Versions
If you have multiple PHP versions installed:
```bash
php8.1 vendor/bin/phpunit
php8.2 vendor/bin/phpunit
php8.3 vendor/bin/phpunit
```

## Test Structure

```
tests/
├── bootstrap.php # Test bootstrap (sets up environment)
├── TestCase.php # Base test class
├── Unit/ # Unit tests (isolated component testing)
│ ├── SmartCacheTest.php # Main SmartCache functionality
│ ├── Console/ # Console command tests
│ │ ├── ClearCommandTest.php
│ │ └── StatusCommandTest.php
│ ├── Providers/ # Service provider tests
│ │ └── SmartCacheServiceProviderTest.php
│ └── Strategies/ # Strategy tests
│ ├── CompressionStrategyTest.php
│ └── ChunkingStrategyTest.php
├── Feature/ # Integration tests
│ └── SmartCacheIntegrationTest.php
└── Fixtures/ # Test fixtures and data
```

## Test Categories

### Unit Tests
- Test individual components in isolation
- Use mocks to avoid external dependencies
- Fast execution
- Located in `tests/Unit/`

### Feature Tests
- Test complete workflows and integration
- Use real Laravel components
- Test the package as end users would use it
- Located in `tests/Feature/`

## Key Testing Features

### 1. Orchestra Testbench Integration
The `TestCase` class extends Orchestra Testbench, providing:
- Minimal Laravel application environment
- Service container with Laravel services
- Configuration management
- Cache system simulation

### 2. Smart Test Data Helpers
Built-in helper methods for creating test data:
```php
$this->createCompressibleData(); // Large repetitive string
$this->createChunkableData(); // Large array for chunking
$this->createLargeTestData(100); // Array with 100 complex items
```

### 3. Custom Assertions
Specialized assertions for testing optimizations:
```php
$this->assertValueIsOptimized($original, $cached);
$this->assertValueIsCompressed($value);
$this->assertValueIsChunked($value);
```

### 4. Configuration Testing
Tests cover various configuration scenarios:
- Strategies enabled/disabled
- Custom thresholds
- Different compression levels
- Custom chunk sizes

### 5. Console Command Testing
Command tests verify:
- Correct output formatting
- Proper exit codes
- Integration with SmartCache service
- Error handling

## Testing Without Laravel Installation

The package tests run completely independently using Orchestra Testbench:

1. **No Laravel Project Required** - Tests create a minimal Laravel environment
2. **Isolated Dependencies** - Only requires packages specified in `composer.json`
3. **Cross-Version Compatible** - Works with Laravel 8.0+ and PHP 8.1+
4. **Fast Execution** - No database migrations or complex setup

## Environment Configuration

Test environment is configured in `TestCase::defineEnvironment()`:
- Cache driver: `array` (in-memory)
- Debug mode: enabled
- Optimized thresholds for testing (lower than production)

## Continuous Integration

The test suite is designed to work well in CI environments:

```yaml
# Example GitHub Actions configuration
- name: Run Tests
run: |
composer install
vendor/bin/phpunit --coverage-clover=coverage.xml
```

## Debugging Tests

### Enable Debug Output
```bash
vendor/bin/phpunit --debug
```

### Stop on First Failure
```bash
vendor/bin/phpunit --stop-on-failure
```

### Verbose Output
```bash
vendor/bin/phpunit --verbose
```

### Test Specific Cache Driver
The package can be tested against different cache drivers by modifying the test configuration.

## Writing New Tests

### Unit Test Example
```php
<?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'));
}
}
```

### Feature Test Example
```php
<?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);
}
}
```

## Troubleshooting

### Common Issues

1. **Memory Limit Errors**
```bash
php -dmemory_limit=1G vendor/bin/phpunit
```

2. **Missing Extensions**
Ensure required PHP extensions are installed:
- `ext-zlib` (for compression)
- `ext-json`
- `ext-mbstring`

3. **Composer Autoload Issues**
```bash
composer dump-autoload
```

4. **Cache Permission Issues**
The test bootstrap creates temporary cache directories automatically.

### Getting Help

- Check the test output for detailed error messages
- Use `--debug` flag for additional debugging information
- Review the test configuration in `phpunit.xml`
- Examine the base `TestCase` class for available helpers

## Performance Considerations

- 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

This testing setup ensures your SmartCache package works correctly across different environments without requiring a full Laravel installation.
Loading