-
Notifications
You must be signed in to change notification settings - Fork 20
Improving unit tests coverage #49
Improving unit tests coverage #49
Conversation
weierophinney
left a comment
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.
First off, great work - you jumped from 27% to 85% coverage!
There's still some room for improvement, though the improvements I suggest may not necessarily add coverage; they'll mainly make intent of tests more clear. In broad strokes:
- Testing constructors to ensure they create an instance of the class is mostly useless, unless you also test for default property values.
- Rename the various test methods to describe the behavior being tested. This will make the intent more clear, and may help you identify when you need to split a test into multiple tests and/or use a provider.
src/ConfigTrait.php
Outdated
| if (empty($config['grants']) || ! is_array($config['grants'])) { | ||
| if (empty($config['grants'])) { | ||
| throw new InvalidConfigException( | ||
| 'The grant value is missing in config authentication' |
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.
This should be "grants"; otherwise, users might add a grant configuration value, and still get the exception. I would also likely add a note that the value must be an array.
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.
Ok, I changed in "grants" and added "must be an array" in the exception message.
src/ConfigTrait.php
Outdated
| } | ||
| if (! is_array($config['grants'])) { | ||
| throw new InvalidConfigException( | ||
| 'The grant must be an array value' |
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.
Same comment here.
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.
Changed.
test/ConfigTraitTest.php
Outdated
| /** | ||
| * @expectedException Zend\Expressive\Authentication\OAuth2\Exception\InvalidConfigException | ||
| */ | ||
| public function testGetPrivateKeyNoConfig() |
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.
Update your test names to indicate what behavior is being tested. In this case, the method would become something like testGetPrivateKeyWhenNoConfigPresentWillResultInAnException.
This practice makes understanding the intention behind the test clearer when maintainers are trying to understand a failure later.
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.
Ok, I supposed was clear enough :)
test/ConfigTraitTest.php
Outdated
| <?php | ||
| /** | ||
| * @see https://github.com/zendframework/zend-expressive-authentication-oauth2 for the canonical source repository | ||
| * @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com) |
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.
Here and elsewhere you introduce new test classes: since the file was introduced this year, use 2018 as the copyright year.
| use PHPUnit\Framework\TestCase; | ||
| use Zend\Expressive\Authentication\OAuth2\Entity\AccessTokenEntity; | ||
|
|
||
| class AccessTokenEntityTest extends TestCase |
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.
I don't think we need this test. If it's just testing that the constructor returns an instance of the class, that's something we know already.
If there's other behavior to test — e.g., default property values, property values based on constructor arguments, etc. — then tests make sense.
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.
If I remove the AccessTokenEntityTest we need to use @codeCoverageIgnore to omit this class from the code coverage. IMHO I think a test class like this, even if just checking for constructor, can be useful as a constrain for future changes. For instance, if someone changes the AccessTokenEntityInterface this test is able to intercept it.
|
|
||
| class RefreshTokenEntityTest extends TestCase | ||
| { | ||
| public function testConstructor() |
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.
Same comment here as for the AccessTokenTest.
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.
I renamed to testImplementsRefreshTokenEntityInterface to test the interface implementation, not just the constructor.
test/Entity/RevokableTraitTest.php
Outdated
|
|
||
| class RevokableTraitTest extends TestCase | ||
| { | ||
| public function testRevoked() |
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.
Break this into two tests, or use a test provider, to detail what happens with each of the boolean arguments.
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.
Done.
test/Entity/ScopeEntityTest.php
Outdated
| $this->entity = new ScopeEntity(); | ||
| } | ||
|
|
||
| public function testConstructor() |
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.
This method is unnecessary.
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.
I renamed into testImplementsScopeEntityInterface.
|
|
||
| namespace ZendTest\Expressive\Authentication\OAuth2\Entity; | ||
|
|
||
| use DateTime; |
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.
Quick question: should we be using DateTime, or DateTimeImmutable? Which would make more sense for the domain?
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.
I would say accept DateTimeInterface, convert to DateTimeImmutable if mutable.
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.
I used DateTimeInterface as parameters of TimestampableTrait and DateTimeImmutable in TimestampableTrait::timestampOnCreate() function.
test/Entity/UserEntityTest.php
Outdated
| /** | ||
| * @expectedException ArgumentCountError | ||
| */ | ||
| public function testConstructorWithoutParam() |
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.
This test doesn't have any assertions. Assert against the default identifier.
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.
The assertion is in the phpdoc, I rename the function to testConstructorWithoutParamWillResultInAnException.
|
I would suggest explicit class level |
|
@weierophinney I updated the PR including your feedback. |
- Commas after all array entries - When splitting to multiple lines, move all arrow operations to their own lines when operating on an object
Writes three test methods that ensure the arguments passed to the constructor affect the instance state.
s/testJsonSerialize/testEntityIsJsonSerializable/
- New files: 2018 - Existing files: range adds "-2018"
So we can test against PHP 7.3.
This PR improves the unit tests coverage.