-
-
Notifications
You must be signed in to change notification settings - Fork 10
Tests addition #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Tests addition #46
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds initial test coverage for the ResponseGrabber and MockServiceProvider classes in the Yiisoft Yii Testing package. The tests help establish baseline functionality verification and error handling scenarios.
- Adds comprehensive test coverage for ResponseGrabber class functionality
- Adds basic test coverage for MockServiceProvider class methods
- Introduces tests for both successful operations and error conditions
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/ResponseGrabberTest.php | Tests ResponseGrabber's response setting/getting functionality and null response error handling |
| tests/MockServiceProviderTest.php | Tests MockServiceProvider's definition management and extensions retrieval |
Comments suppressed due to low confidence (1)
tests/ResponseGrabberTest.php:17
- [nitpick] The variable name 'responseStub' suggests a test double, but this is actually a real Response object. Consider renaming to 'response' or 'testResponse' for clarity.
$responseStub = new Response(200, body: '{"key": "value"}', reason: 'Ok with body', version: '1.1');
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #46 +/- ##
============================================
+ Coverage 26.21% 31.06% +4.85%
Complexity 60 60
============================================
Files 6 6
Lines 206 206
============================================
+ Hits 54 64 +10
+ Misses 152 142 -10 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
What exactly do these tests check? |
|
Hi @samdark, They check the actual behavior of the tested classes. I checked the package list of Yii3 and saw that this one had little code coverage. I'd like to start contributing to open-source projects, so that’s why I chose it. I also tried to write more tests for the remaining classes, but it would require refactoring the production code to make them simple. |
|
Production code? |
|
Production code? Yeah, I mean, the application code |
|
Which application code? I'm a bit lost. Could you elaborate? |
|
Sure, by application code/production code, I mean the actual source code that makes the software work (the classes, functions, and logic under the src fodler) as opposed to test code (the code under the test folder). I tried to write also other tests for the remaining classes but some of them are not unit-testable without refactoring them. Example: Then I started testing the methods. This could not be accomplished because FunctionalTester is a final class, and therefore it cannot be mocked. This makes the unit test impossible to write (or if possible, really complicated). Two possible refactors to make the test easy to write are:
|
| public function testGetDefinitionsWillReturnDefinitionsSetInArray(): void | ||
| { | ||
| $mockServiceProvider = new MockServiceProvider(); | ||
| $mockServiceProvider->setDefinition('example-id-001', ['class' => 'Example1\Class1\Position1']); | ||
| $mockServiceProvider->setDefinition('example-id-002', 'Example1\Class2\Position2'); | ||
| $mockServiceProvider->setDefinition('example-id-003', 'Example3\Class3\Position3'); | ||
|
|
||
| $this->assertEquals([ | ||
| 'example-id-001' => ['class' => 'Example1\Class1\Position1'], | ||
| 'example-id-002' => 'Example1\Class2\Position2', | ||
| 'example-id-003' => 'Example3\Class3\Position3', | ||
| ], $mockServiceProvider->getDefinitions()); | ||
| } | ||
|
|
||
| public function testGetExtensionsWillReturnEmptyArray(): void | ||
| { | ||
| $this->assertEmpty((new MockServiceProvider())->getExtensions()); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests do not correspond to real usage of the provider. Would you please make it closer to real usage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you provide a link where I can find real usages? I'll check it and modify both tests
| public function testGetResponseWillReturnResponseAccessorSetResponse(): void | ||
| { | ||
| $response = new Response(200, body: '{"key": "value"}', reason: 'Ok with body', version: '1.1'); | ||
|
|
||
| $responseGrabber = new ResponseGrabber(); | ||
| $responseGrabber->setResponse($response); | ||
|
|
||
| $this->assertEquals(new ResponseAccessor($response), $responseGrabber->getResponse()); | ||
| } | ||
|
|
||
| public function testGetResponseWillThrowExceptionIfSetResponseIsCalledWithNullParameter(): void | ||
| { | ||
| $this->expectException(RuntimeException::class); | ||
| $this->expectExceptionMessage('Response is null'); | ||
|
|
||
| $responseGrabber = new ResponseGrabber(); | ||
| $responseGrabber->setResponse(null); | ||
| $responseGrabber->getResponse(); | ||
| } | ||
|
|
||
| public function testGetResponseWillThrowExceptionIfResponseIsNotSet(): void | ||
| { | ||
| $this->expectException(RuntimeException::class); | ||
| $this->expectExceptionMessage('Response is null'); | ||
|
|
||
| (new ResponseGrabber())->getResponse(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests do not correspond to real usage. Would you please make it closer to real usage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you provide a link where I can find real usages? I'll check it and modify both tests
|
I think there are none. @vjik could you provide some examples? Is it still there in app template or demos? |
Hi, I added some tests just to get started. Hope it can help