Unit Testing

Posted . Visible to the public.

Running Unit Test in DDEV

# Run all tests
ddev exec composer run phpunit:test

# Run a specific test, example: OAuth tests
ddev exec vendor/bin/phpunit --configuration .phpunit.dist.xml tests/unit/Mage/Oauth/Controllers/OauthFlowTest.php --testdox

Running OAuth Tests on Actual Server

1. Basic Test Execution

# Navigate to your OpenMage root directory
cd /var/www/html/openmage  # or wherever your OpenMage is installed

# Run OAuth tests specifically
vendor/bin/phpunit --configuration .phpunit.dist.xml tests/unit/Mage/Oauth/Controllers/OauthFlowTest.php --testdox

The easiest way is to use the predefined composer scripts:

# Run all tests (including OAuth)
composer run phpunit:test

# Run with code coverage (if XDEBUG is available)
composer run phpunit:coverage

# Run with local HTML coverage report
composer run phpunit:coverage-local

3. Test Output Examples

When you run the OAuth tests, you'll see output like this:

$ vendor/bin/phpunit --configuration .phpunit.dist.xml tests/unit/Mage/Oauth/Controllers/OauthFlowTest.php --testdox

PHPUnit 9.6.23 by Sebastian Bergmann and contributors.

Oauth Flow (OpenMage\Tests\Unit\Mage\Oauth\Controllers\OauthFlow)
 ✔ Complete oauth flow with valid_oauth_flow
 ✔ Complete oauth flow with initiate_only_flow
 ✔ Complete oauth flow with authorize_only_flow
 ✔ Initiate controller index action with valid_initiate_request
 ✔ Initiate controller index action with initiate_with_callback
 ✔ Initiate controller index action with initiate_oob_callback
 ✔ Authorize controller actions with valid_authorize_request
 ✔ Authorize controller actions with authorize_confirm_action
 ✔ Authorize controller actions with authorize_reject_action
 ✔ Authorize controller actions with authorize_simple_page
 ✔ Token controller index action with valid_token_request
 ✔ Token controller index action with token_with_verifier
 ✔ Oauth error scenarios with invalid_consumer_key
 ✔ Oauth error scenarios with invalid_signature
 ✔ Oauth error scenarios with token_expired
 ✔ Oauth error scenarios with token_revoked
 ✔ Oauth error scenarios with invalid_verifier
 ✔ Oauth error scenarios with nonce_used
 ✔ Oauth error scenarios with timestamp_refused
 ✔ Oauth error scenarios with parameter_absent
 ✔ Oauth error scenarios with signature_method_rejected
 ✔ Oauth error scenarios with version_rejected
 ✔ Oauth signature validation
 ✔ Oauth token lifecycle

Time: 00:00.275, Memory: 8.00 MB

OK (24 tests, 57 assertions)

4. Running Specific Test Groups

You can run tests by groups using the --group option:

# Run only OAuth tests
vendor/bin/phpunit --configuration .phpunit.dist.xml --group Oauth

# Run only controller tests
vendor/bin/phpunit --configuration .phpunit.dist.xml --group Controllers

# Run only error scenario tests
vendor/bin/phpunit --configuration .phpunit.dist.xml --group Error

5. Performance Optimization

For production servers, you might want to optimize performance:

# Disable XDEBUG for faster execution
XDEBUG_MODE=off vendor/bin/phpunit --configuration .phpunit.dist.xml tests/unit/Mage/Oauth/Controllers/OauthFlowTest.php

# Set memory limit
php -d memory_limit=512M vendor/bin/phpunit --configuration .phpunit.dist.xml tests/unit/Mage/Oauth/Controllers/OauthFlowTest.php

# Run without coverage for speed
vendor/bin/phpunit --configuration .phpunit.dist.xml --no-coverage tests/unit/Mage/Oauth/Controllers/OauthFlowTest.php

6. Integration with CI/CD

For automated testing, you can integrate with your CI/CD pipeline:

# Exit with error code if tests fail
vendor/bin/phpunit --configuration .phpunit.dist.xml tests/unit/Mage/Oauth/Controllers/OauthFlowTest.php --stop-on-failure

# Generate JUnit XML for CI systems
vendor/bin/phpunit --configuration .phpunit.dist.xml tests/unit/Mage/Oauth/Controllers/OauthFlowTest.php --log-junit oauth-test-results.xml

7. Cron Job Example

To run tests automatically, you could set up a cron job:

# Add to crontab (runs daily at 2 AM)
0 2 * * * cd /var/www/html/openmage && vendor/bin/phpunit --configuration .phpunit.dist.xml tests/unit/Mage/Oauth/Controllers/OauthFlowTest.php --log-junit /var/log/oauth-tests.xml

8. Quick Verification

To quickly verify the OAuth tests are working:

# Simple test run
vendor/bin/phpunit tests/unit/Mage/Oauth/Controllers/OauthFlowTest.php

# Check if all OAuth tests pass
vendor/bin/phpunit --configuration .phpunit.dist.xml tests/unit/Mage/Oauth/Controllers/OauthFlowTest.php | grep -E "(OK|FAILURES|ERRORS)"

9. What the Tests Validate

The OAuth tests validate:

  • Controller Instantiation: All OAuth controllers can be created
  • Complete OAuth Flow: Full initiate → authorize → token → resource flow
  • Error Handling: Proper error responses for invalid requests
  • Security: OAuth signature validation
  • Token Lifecycle: Token creation and conversion
  • Callback Handling: OOB and URL callbacks

10. Expected Results

On a properly configured production server, you should see:

  • 24 tests passing
  • 57 assertions successful
  • 0 errors, 0 failures
  • Execution time: Typically under 1 second

That's it! Since your production server already has OpenMage set up, the tests should run immediately without any additional configuration.

kiatng
Last edit
kiatng
Posted by kiatng to OpenMage (2025-09-11 04:34)