-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Fix exception handling and cover with unit tests the GetById media asset command #25547
Fix exception handling and cover with unit tests the GetById media asset command #25547
Conversation
Hi @coderimus. Thank you for your contribution
For more details, please, review the Magento Contributor Guide documentation. |
$mediaAssetStub = $this->getMockBuilder(AssetInterface::class)->getMock(); | ||
$this->assetFactory->expects($this->once())->method('create')->willReturn($mediaAssetStub); | ||
|
||
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID); |
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.
Please, add an assertion here. The execute
method returns a value, so it worth asserting that we have an expected 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.
Hi @rogyar I don't think the assertion is necessary here as execute method is not supposed to return anything in this specific test (it is supposed to throw an exception instead)
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.
Hi @sivaschenko. In this particular case, we test the positive scenario with no exception. As a result, the execute
method should return an ID. An assertion of the mentioned ID might be useful here.
Probably, I've missed some points.
Thank you.
->method('critical') | ||
->willReturnSelf(); | ||
|
||
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID); |
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.
Please, add an assertion of the returned result here as well.
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.
Hello @rogyar , thank you for your review. I have a question about this: do we need to have an assertion here when we already expect an exception: $this->expectException(IntegrationException::class);
In that case, I think that assertion is satisfied by the fact of throwing an integration exception. What assertion you would like to see 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.
I don't think the assertion is necessary 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.
Agree, missed the point that the test already asserts the exception. Thank you
Also, the |
ea2f050
to
d08eb44
Compare
@sivaschenko @rogyar But this semantic version test confused me. Do you have any idea why it failed? I didn't change anything according to its report. :( |
Nevermind about the semantic version check. I re-run it, and everything is ok now. @rogyar @sivaschenko |
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.
Thanks for the pull request @coderimus ! Please see my review comments
$mediaAssetStub = $this->getMockBuilder(AssetInterface::class)->getMock(); | ||
$this->assetFactory->expects($this->once())->method('create')->willReturn($mediaAssetStub); | ||
|
||
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID); |
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.
Hi @rogyar I don't think the assertion is necessary here as execute method is not supposed to return anything in this specific test (it is supposed to throw an exception instead)
|
||
/** | ||
* Test the GetById command model | ||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
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.
@coderimus can you please remove this warning suppression (and extract some tests to a separate file/class if necessary)
f224328
to
b0fb363
Compare
@sivaschenko, what do you think about the proposed approach? It would be really nice if we could prevent copying the |
@rogyar you mean that it would be nice to initialise the |
ee76ae8
to
4b03395
Compare
@rogyar @coderimus I think in this case there could be unique setUp methods for each test as exception tests should not need all the mocks used in the main case test |
4b03395
to
061d5a3
Compare
@rogyar @sivaschenko From my perspective, I can say that it will be better to have separate classes. Even with the setup method in most cases similar. In the scope of the Unit testing, this could be possible even better and applicable: we encapsulate the logic of every exception and the logical conclusion it produces. With this, we can avoid a situation when a possible change made for any part of the command can be possibly ignored and doesn't take into account. About one class with the setup method and its inheritance: this is not so useful for us because unit tests, as I see them, should be as strict as possible to cover existed implementation. Add the inheritance of the setup method results in the fact that we rely on a parent class for tests in all cases and make the unit test structure more complicated. My latest implementation consists of separate classes. Please, check them when you will have time. |
Hi @sivaschenko, thank you for the review. |
Hi @rogyar, thank you for the review. |
Hi @coderimus, thank you for your contribution! |
Description (*)
Cover with unit test the
GetById
command of the Media Gallery functionality. Provide two tests: successful action execution and fail at the not found entity by id.Fixed Issues (if relevant)
magento/adobe-stock-integration#585
Manual testing scenarios (*)
N/A
Questions or comments
N/A
Contribution checklist (*)