Skip to content
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

[Captcha] Covering the CheckUserLoginBackendObserver by Unit Test #19175

Conversation

eduard13
Copy link
Contributor

Description

This PR adds missing unit tests for the following class:

  • \Magento\Captcha\Observer\CheckUserLoginBackendObserver

Fixed Issues (if relevant)

N/A

Manual testing scenarios

N/A

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@eduard13 eduard13 requested a review from rogyar November 12, 2018 17:04
@magento-engcom-team magento-engcom-team added Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner Component: Captcha labels Nov 12, 2018
@magento-engcom-team
Copy link
Contributor

Hi @eduard13. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.3-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

Copy link
Contributor

@orlangur orlangur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eduard13 thanks, I didn't meet anything really bad during review, just some polishing up is needed. I believe you'll be able to get used soon, just didn't pay attention to some nuances before (and you're not alone - there is a HUGE amount of really fragile unit tests written in Magento 2).

Please consider using Prophecy with your next unit test ;)

* @param bool $isCorrect
* @param int $invokedTimes
* @return void
* @throws AuthenticationException
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What? Test throwing exception?

$this->helperMock->expects($this->once())
->method('getCaptcha')
->with($formId)
->willReturn($captcha);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this assignments would be much more readable as one-liners.

$captcha->expects($this->once())->method('isRequired')
->with($login)
->willReturn($isRequired);
$captcha->expects($this->exactly($invokedTimes))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such assertion does not test observer behavior, please remove it.

$observerMock->expects($this->any())
->method('getEvent')
->willReturn($eventMock);
$captcha->expects($this->once())->method('isRequired')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->once() does not test observer behavior, remove it.

->method('isCorrect')
->with($captchaValue)
->willReturn($isCorrect);
$this->helperMock->expects($this->once())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->once() does not test observer behavior, remove it.

->with($this->requestMock, $formId)
->willReturn($captchaValue);

$this->expectException(AuthenticationException::class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to PHPDoc.

$observerMock->expects($this->any())
->method('getEvent')
->willReturn($eventMock);
$captcha->expects($this->once())->method('isRequired')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too strict, irrelevant assertion.

$captcha->expects($this->once())->method('isRequired')
->with($login)
->willReturn(true);
$captcha->expects($this->exactly(1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too strict, irrelevant assertion.

->method('isCorrect')
->with($captchaValue)
->willReturn(false);
$this->helperMock->expects($this->once())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too strict, irrelevant assertion.

->with($formId)
->willReturn($captcha);

$this->captchaStringResolverMock->expects($this->exactly(1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too strict, irrelevant assertion.

Copy link
Contributor Author

@eduard13 eduard13 Nov 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @orlangur thank you for all the suggestions. By 'Too strict', do you mean that we should try to avoid the expects strictness in some cases, and use it only when is really needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eduard13 yes, such checks control execution flow rather than code behavior. When method changes, they may break even when method still works correctly. It is better in each scenario assert only what is really essential for a particular case.

@eduard13
Copy link
Contributor Author

Hi @orlangur, could you please review the latest changes?
Thanks.

Copy link
Contributor

@orlangur orlangur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent @eduard13 !!

@magento-engcom-team magento-engcom-team added this to the Release: 2.3.1 milestone Nov 13, 2018
@magento-engcom-team
Copy link
Contributor

Hi @orlangur, thank you for the review.
ENGCOM-3454 has been created to process this Pull Request

@magento-engcom-team magento-engcom-team merged commit d84929a into magento:2.3-develop Nov 14, 2018
@magento-engcom-team
Copy link
Contributor

Hi @eduard13. Thank you for your contribution.
We will aim to release these changes as part of 2.3.1.
Please check the release notes for final confirmation.

@eduard13 eduard13 deleted the 2.3-develop-captcha-user-login-ut branch November 25, 2018 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Captcha Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner Progress: accept Release Line: 2.3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants