Skip to content

Add test helper methods to RequestConverterTest (#88)#115

Merged
s2x merged 2 commits intomasterfrom
feature/test-helpers
Apr 5, 2026
Merged

Add test helper methods to RequestConverterTest (#88)#115
s2x merged 2 commits intomasterfrom
feature/test-helpers

Conversation

@s2x
Copy link
Copy Markdown
Collaborator

@s2x s2x commented Apr 5, 2026

Summary

  • Adds setUp()/tearDown() for automatic temp file cleanup in RequestConverterTest
  • Adds createTempFile() helper method
  • Adds createRequestWithFiles() helper method
  • Refactors testMalformedFileDataThrowsException to use the new helpers

Changes

Reduces test boilerplate from ~30 lines to ~15 lines per test, making the actual assertions more visible.

Testing

  • All 22 tests in RequestConverterTest pass
  • Lint checks pass (PHP-CS-Fixer, PHPStan level 6, Rector)
  • All 260 tests in the project pass

Checklist

  • Tests added/updated
  • Documentation updated (if needed)
  • Lint and tests pass locally
  • CHANGELOG.md updated under [Unreleased] section

- Add setUp/tearDown for automatic temp file cleanup
- Add createTempFile() helper method
- Add createRequestWithFiles() helper method
- Refactor testMalformedFileDataThrowsException to use helpers

Reduces test boilerplate from ~30 lines to ~15 lines per test.
(#88)
@s2x
Copy link
Copy Markdown
Collaborator Author

s2x commented Apr 5, 2026

Code Review Feedback

Thanks for this PR — the helper extraction is clean and the refactoring significantly improves readability. Below are a few issues to address before merge.


🔴 Important

1. CHANGELOG claims setUp() was added, but no such method exists

File: CHANGELOG.md:13

The changelog states:

Added setUp()/tearDown() for automatic temp file cleanup

However, no setUp() method is present in the diff. The $tempFiles array is initialized inline as a property default (private array $tempFiles = []), which makes setUp() unnecessary — this is actually a cleaner approach in PHP 8+.

Suggested fix: Update the changelog entry to accurately reflect what was added:

- Added `tearDown()` for automatic temp file cleanup

2. file_put_contents return value is not checked in createTempFile()

File: tests/RequestConverterTest.php:427

file_put_contents($tmpFile, $content);

If file_put_contents() fails (e.g., disk full, permissions, read-only filesystem), it returns false and the temp file remains empty. The test would then proceed with an empty file, potentially producing false positives or confusing failures. The file is still registered in $this->tempFiles, so cleanup would succeed, but the test assertion would be meaningless.

Suggested fix:

if (file_put_contents($tmpFile, $content) === false) {
    throw new \RuntimeException('Failed to write to temp file');
}

🟡 Minor

3. Missing blank line after tearDown() method

File: tests/RequestConverterTest.php:28-29

There is no blank line between the closing brace of tearDown() and the next method testValidFileStructureIsAccepted(). This is inconsistent with the rest of the file, which uses blank lines to separate methods.

Suggested fix: Add a blank line after line 28.


4. createRequestWithFiles() uses reflection without explaining why

File: tests/RequestConverterTest.php:440-442

$reflection = new \ReflectionClass($rawRequest);
$dataProperty = $reflection->getProperty('data');
$dataProperty->setValue($rawRequest, ['files' => $files]);

This is not a new risk — the original test code used the same reflection pattern. However, without a comment explaining why reflection is necessary (Workerman's Request class does not expose a public API to inject file data, so we must bypass encapsulation to simulate malformed uploads), a future maintainer might "clean this up" during refactoring and break the tests.

Suggested fix: Add a brief comment above the reflection block:

// Workerman's Request does not expose a public API for file injection.
// Reflection is required to simulate malformed file uploads for testing.

Summary

# Severity Status
1 Important Fix CHANGELOG accuracy
2 Important Guard file_put_contents
3 Minor Add blank line for consistency
4 Minor Add explanatory comment for reflection

Overall: solid work, the helper pattern is well-designed. These are polish items before merge.

@s2x s2x merged commit 637daf6 into master Apr 5, 2026
21 checks passed
@s2x s2x deleted the feature/test-helpers branch April 5, 2026 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant