|
| 1 | +# Co-PHPUnit |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +`friendsofhyperf/co-phpunit` is a PHPUnit extension specifically designed for testing Hyperf applications and other Swoole-based frameworks, enabling tests to run inside Swoole coroutines. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +composer require friendsofhyperf/co-phpunit --dev |
| 11 | +``` |
| 12 | + |
| 13 | +## Why Co-PHPUnit? |
| 14 | + |
| 15 | +When testing Hyperf applications, many components rely on Swoole's coroutine context to function properly. Running tests in a traditional synchronous environment can lead to issues such as: |
| 16 | + |
| 17 | +- Coroutine context not being available |
| 18 | +- Timers and event loops not working correctly |
| 19 | +- Coordinator pattern failures |
| 20 | +- Database connection pool issues |
| 21 | + |
| 22 | +Co-PHPUnit solves these problems by automatically wrapping test execution in a Swoole coroutine context when needed. |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +### Basic Usage |
| 27 | + |
| 28 | +Simply use the `RunTestsInCoroutine` trait in your test class: |
| 29 | + |
| 30 | +```php |
| 31 | +<?php |
| 32 | + |
| 33 | +namespace Your\Namespace\Tests; |
| 34 | + |
| 35 | +use FriendsOfHyperf\CoPHPUnit\Concerns\RunTestsInCoroutine; |
| 36 | +use PHPUnit\Framework\TestCase; |
| 37 | + |
| 38 | +class YourTest extends TestCase |
| 39 | +{ |
| 40 | + use RunTestsInCoroutine; |
| 41 | + |
| 42 | + public function testSomething() |
| 43 | + { |
| 44 | + // Your test code here |
| 45 | + // This will automatically run inside a Swoole coroutine |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +### Disabling Coroutine for Specific Tests |
| 51 | + |
| 52 | +If you need to disable coroutine execution for a specific test class, set the `$enableCoroutine` property to `false`: |
| 53 | + |
| 54 | +```php |
| 55 | +<?php |
| 56 | + |
| 57 | +namespace Your\Namespace\Tests; |
| 58 | + |
| 59 | +use FriendsOfHyperf\CoPHPUnit\Concerns\RunTestsInCoroutine; |
| 60 | +use PHPUnit\Framework\TestCase; |
| 61 | + |
| 62 | +class YourTest extends TestCase |
| 63 | +{ |
| 64 | + use RunTestsInCoroutine; |
| 65 | + |
| 66 | + protected bool $enableCoroutine = false; |
| 67 | + |
| 68 | + public function testSomething() |
| 69 | + { |
| 70 | + // This test will run in normal synchronous mode |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## How It Works |
| 76 | + |
| 77 | +The `RunTestsInCoroutine` trait overrides PHPUnit's `runBare()` method to: |
| 78 | + |
| 79 | +1. **Check Prerequisites**: Verifies that the Swoole extension is loaded and not already in a coroutine context |
| 80 | +2. **Create Coroutine Context**: Wraps test execution in `Swoole\Coroutine\run()` |
| 81 | +3. **Exception Handling**: Properly captures and re-throws exceptions from within the coroutine |
| 82 | +4. **Cleanup**: Clears all timers and resumes coordinator on test completion |
| 83 | +5. **Fallback**: If conditions aren't met, falls back to normal test execution |
| 84 | + |
| 85 | +### PHPUnit Patch |
| 86 | + |
| 87 | +The package includes a `phpunit-patch.php` file that automatically removes the `final` keyword from PHPUnit's `TestCase::runBare()` method, allowing the trait to override it. This patch is applied automatically when the package is autoloaded. |
| 88 | + |
| 89 | +## Requirements |
| 90 | + |
| 91 | +- PHP >= 8.0 |
| 92 | +- PHPUnit >= 10.0 |
| 93 | +- Swoole extension (when running tests in coroutine mode) |
| 94 | +- Hyperf >= 3.1 (for coordinator functionality) |
| 95 | + |
| 96 | +## Configuration |
| 97 | + |
| 98 | +### Composer Autoload |
| 99 | + |
| 100 | +The package automatically registers its autoload files in composer.json: |
| 101 | + |
| 102 | +```json |
| 103 | +{ |
| 104 | + "autoload-dev": { |
| 105 | + "psr-4": { |
| 106 | + "Your\\Tests\\": "tests/" |
| 107 | + }, |
| 108 | + "files": [ |
| 109 | + "vendor/friendsofhyperf/co-phpunit/phpunit-patch.php" |
| 110 | + ] |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### PHPUnit Configuration |
| 116 | + |
| 117 | +No special PHPUnit configuration is required. The package works seamlessly with your existing `phpunit.xml` configuration. |
| 118 | + |
| 119 | +## Best Practices |
| 120 | + |
| 121 | +1. **Use for Integration Tests**: This is particularly useful for integration tests that interact with Hyperf's coroutine-aware components |
| 122 | +2. **Selective Enablement**: Not all tests need to run in coroutines. Use `$enableCoroutine = false` for unit tests that don't require coroutine context |
| 123 | +3. **Test Isolation**: The package automatically cleans up timers and coordinator state between tests |
| 124 | +4. **Performance**: Tests running in coroutines may have slightly different performance characteristics |
| 125 | + |
| 126 | +## Example: Testing Hyperf Services |
| 127 | + |
| 128 | +```php |
| 129 | +<?php |
| 130 | + |
| 131 | +namespace App\Tests; |
| 132 | + |
| 133 | +use FriendsOfHyperf\CoPHPUnit\Concerns\RunTestsInCoroutine; |
| 134 | +use Hyperf\Context\ApplicationContext; |
| 135 | +use PHPUnit\Framework\TestCase; |
| 136 | + |
| 137 | +class ServiceTest extends TestCase |
| 138 | +{ |
| 139 | + use RunTestsInCoroutine; |
| 140 | + |
| 141 | + public function testServiceWithCoroutineContext() |
| 142 | + { |
| 143 | + // Get service from container |
| 144 | + $service = ApplicationContext::getContainer()->get(YourService::class); |
| 145 | + |
| 146 | + // Test methods that use coroutine context |
| 147 | + $result = $service->asyncOperation(); |
| 148 | + |
| 149 | + $this->assertNotNull($result); |
| 150 | + } |
| 151 | + |
| 152 | + public function testDatabaseConnection() |
| 153 | + { |
| 154 | + // Test database operations that require connection pool |
| 155 | + $result = Db::table('users')->first(); |
| 156 | + |
| 157 | + $this->assertIsArray($result); |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +## Troubleshooting |
| 163 | + |
| 164 | +### Tests Hang or Timeout |
| 165 | + |
| 166 | +If tests hang, ensure that: |
| 167 | +- All async operations are properly awaited |
| 168 | +- No infinite loops exist in coroutine callbacks |
| 169 | +- Timers are cleared in test teardown |
| 170 | + |
| 171 | +### "Call to a member function on null" |
| 172 | + |
| 173 | +This usually indicates that coroutine context is not available. Ensure: |
| 174 | +- Swoole extension is installed and enabled |
| 175 | +- The `RunTestsInCoroutine` trait is included |
| 176 | +- `$enableCoroutine` is set to `true` |
| 177 | + |
| 178 | +### PHPUnit Version Compatibility |
| 179 | + |
| 180 | +The package supports PHPUnit 10.x, 11.x, and 12.x. Ensure your PHPUnit version is compatible. |
0 commit comments