This document provides comprehensive information about testing the Kinde PHP SDK, including how to run tests, what tests are available, and how to contribute to testing.
- Quick Start
- Test Structure
- Running Tests
- Test Suites
- Framework-Specific Tests
- Examples
- Coverage Reports
- Contributing to Tests
- Troubleshooting
- PHP 8.0 or higher
- Composer
- PHPUnit 10.x
# Install dependencies
composer install
# Run all tests
./run-tests.sh all
# Or run specific test suites
./run-tests.sh core
./run-tests.sh laraveltests/
├── Unit/ # Core SDK unit tests
│ ├── KindeClientSDKTest.php
│ └── KindeManagementClientTest.php
├── Framework/ # Framework-specific tests
│ ├── Laravel/
│ │ ├── KindeServiceProviderTest.php
│ │ ├── KindeAuthMiddlewareTest.php
│ │ └── KindeAuthControllerTest.php
│ ├── Slim/
│ ├── Symfony/
│ └── CodeIgniter/
├── Integration/ # Integration tests
└── examples/ # Example applications
├── laravel/
├── slim/
├── symfony/
└── codeigniter/
The run-tests.sh script provides an easy way to run different test suites:
# Run all tests
./run-tests.sh all
# Run specific test suites
./run-tests.sh core
./run-tests.sh laravel
./run-tests.sh slim
./run-tests.sh symfony
./run-tests.sh codeigniter
./run-tests.sh integration
# Show coverage information
./run-tests.sh coverage
# Clean up test artifacts
./run-tests.sh clean
# Show help
./run-tests.sh help# Run all tests
php vendor/bin/phpunit
# Run specific test suite
php vendor/bin/phpunit --testsuite="Core SDK"
php vendor/bin/phpunit --testsuite="Laravel Framework"
# Run specific test file
php vendor/bin/phpunit tests/Unit/KindeClientSDKTest.php
# Run with coverage
php vendor/bin/phpunit --coverage-html=coverage/htmlTests for the core SDK functionality:
-
KindeClientSDKTest.php: Tests for the main OAuth client
- Environment variable initialization
- Constructor parameter handling
- Endpoint generation
- Authentication flow
- Error handling
-
KindeManagementClientTest.php: Tests for the management API client
- Environment variable initialization
- API client initialization
- Configuration handling
- Access token management
Tests for framework-specific implementations:
-
KindeServiceProviderTest.php: Tests the Laravel service provider
- Service registration
- Configuration handling
- Environment variable fallbacks
- Singleton registration
-
KindeAuthMiddlewareTest.php: Tests the authentication middleware
- Authentication checks
- Redirect handling
- JSON response handling
- Inertia.js integration
-
KindeAuthControllerTest.php: Tests the authentication controller
- Login/logout flows
- Callback handling
- Portal URL generation
- Error handling
Similar test structures exist for:
- Slim Framework
- Symfony Framework
- CodeIgniter Framework
End-to-end tests that verify the SDK works correctly in real-world scenarios.
Laravel tests require a Laravel application context. The tests mock the Laravel framework components:
// Example: Testing service provider registration
public function testServiceProviderRegistersKindeClientSDK()
{
Config::set('kinde', [
'domain' => 'https://test-domain.kinde.com',
'client_id' => 'test_client_id',
'client_secret' => 'test_client_secret'
]);
$app = $this->createApplication();
$provider = new KindeServiceProvider($app);
$provider->register();
$kindeClient = $app->make(KindeClientSDK::class);
$this->assertInstanceOf(KindeClientSDK::class, $kindeClient);
}Middleware tests verify authentication and authorization behavior:
// Example: Testing middleware with authenticated user
public function testMiddlewareAllowsAuthenticatedUser()
{
$this->kindeClient->method('isAuthenticated')->willReturn(true);
$request = Request::create('/protected-route', 'GET');
$response = $this->middleware->handle($request, $next);
$this->assertEquals('Protected content', $response->getContent());
}The examples/ directory contains complete example applications for each framework:
// examples/laravel/ExampleController.php
class ExampleController extends Controller
{
public function __construct(
private KindeClientSDK $kindeClient,
private KindeManagementClient $management
) {}
public function login(Request $request): RedirectResponse
{
$additionalParams = $request->only(['org_code', 'org_name', 'is_create_org']);
try {
$result = $this->kindeClient->login($additionalParams);
return redirect()->away($result->getAuthUrl());
} catch (Exception $e) {
return redirect()->route('home')->withErrors(['auth' => $e->getMessage()]);
}
}
}After running tests, coverage reports are generated in the coverage/ directory:
# Generate coverage reports
./run-tests.sh all
# View coverage
open coverage/all/index.htmlCoverage reports include:
- HTML reports for browser viewing
- Clover XML reports for CI/CD integration
- Per-test-suite coverage breakdown
- Unit Tests: Add to
tests/Unit/for core SDK functionality - Framework Tests: Add to
tests/Framework/{Framework}/for framework-specific code - Integration Tests: Add to
tests/Integration/for end-to-end scenarios
- Test classes:
{ClassName}Test.php - Test methods:
test{Description}() - Use descriptive names that explain what is being tested
class MyTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
// Setup code
}
public function testSomething()
{
// Arrange
$input = 'test';
// Act
$result = $this->subject->method($input);
// Assert
$this->assertEquals('expected', $result);
}
}- Mock external dependencies (HTTP clients, databases)
- Use realistic test data
- Test both success and failure scenarios
- Test edge cases and error conditions
Tests use predefined environment variables in phpunit.xml:
<env name="KINDE_DOMAIN" value="https://test-domain.kinde.com"/>
<env name="KINDE_CLIENT_ID" value="test_client_id"/>
<env name="KINDE_CLIENT_SECRET" value="test_client_secret"/>-
Tests failing due to missing dependencies
composer install --dev
-
Coverage reports not generating
# Ensure Xdebug is installed and enabled php -m | grep xdebug
-
Framework tests failing
# Check if framework dependencies are installed composer require --dev laravel/framework -
Permission issues with test runner
chmod +x run-tests.sh
# Run tests with verbose output
php vendor/bin/phpunit --verbose
# Run specific test with debug output
php vendor/bin/phpunit --debug tests/Unit/KindeClientSDKTest.php
# Run tests with coverage and stop on failure
php vendor/bin/phpunit --coverage-html=coverage/html --stop-on-failureFor CI/CD pipelines, use the Clover XML reports:
# Example GitHub Actions step
- name: Run tests
run: |
composer install --dev
php vendor/bin/phpunit --coverage-clover=coverage.xml
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
file: coverage.xml- Test Isolation: Each test should be independent and not rely on other tests
- Descriptive Names: Use clear, descriptive test and method names
- Arrange-Act-Assert: Structure tests with clear sections
- Mock External Dependencies: Don't rely on external services in unit tests
- Test Both Success and Failure: Cover error conditions and edge cases
- Keep Tests Fast: Unit tests should run quickly
- Maintain Test Data: Use realistic but minimal test data