From 129771e606bcadca1b7cc7e5e698b8de9e37e35e Mon Sep 17 00:00:00 2001 From: Eden Date: Thu, 21 Nov 2019 21:30:34 +0700 Subject: [PATCH 1/3] Unit Test to cover class \Magento\Captcha\CustomerData\Captcha --- .../Test/Unit/CustomerData/CaptchaTest.php | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php diff --git a/app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php b/app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php new file mode 100644 index 0000000000000..0c2b1975e6cf4 --- /dev/null +++ b/app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php @@ -0,0 +1,107 @@ +helper = $this->createMock(CaptchaHelper::class); + $this->customerSession = $this->createMock(CustomerSession::class); + $this->formIds = [ + 'user_login' + ]; + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->model = $this->objectManagerHelper->getObject( + Captcha::class, + [ + 'helper' => $this->helper, + 'formIds' => $this->formIds, + 'customerSession' => $this->customerSession + ] + ); + } + + /** + * Test getSectionData() when user is login and require captcha + */ + public function testGetSectionData() + { + $emailLogin = 'test@localhost.com'; + + $userLoginModel = $this->createMock(DefaultModel::class); + $userLoginModel->expects($this->any())->method('isRequired')->with($emailLogin) + ->willReturn(true); + $this->helper->expects($this->any())->method('getCaptcha')->with('user_login')->willReturn($userLoginModel); + + $this->customerSession->expects($this->any())->method('isLoggedIn') + ->willReturn(true); + + $this->customerData = $this->createMock(CustomerData::class); + $this->customerData->expects($this->any())->method('getEmail')->willReturn($emailLogin); + $this->customerSession->expects($this->any())->method('getCustomerData') + ->willReturn($this->customerData); + + /* Assert to test */ + $this->assertEquals( + [ + "user_login" => [ + "isRequired" => true, + "timestamp" => time() + ] + ], + $this->model->getSectionData() + ); + } +} From 7534f33b2bbed6d1bb86e6b3fb22218d45c97158 Mon Sep 17 00:00:00 2001 From: Eden Date: Fri, 22 Nov 2019 13:01:32 +0700 Subject: [PATCH 2/3] Refactor to pass review --- .../Captcha/Test/Unit/CustomerData/CaptchaTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php b/app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php index 0c2b1975e6cf4..b34de7778e176 100644 --- a/app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php +++ b/app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php @@ -23,17 +23,17 @@ class CaptchaTest extends \PHPUnit\Framework\TestCase { /** - * @var CaptchaHelper | \PHPUnit_Framework_MockObject_MockObject + * @var CaptchaHelper|\PHPUnit_Framework_MockObject_MockObject */ private $helper; /** - * @var CustomerSession | \PHPUnit_Framework_MockObject_MockObject + * @var CustomerSession|\PHPUnit_Framework_MockObject_MockObject */ private $customerSession; /** - * @var CustomerData | \PHPUnit_Framework_MockObject_MockObject + * @var CustomerData|\PHPUnit_Framework_MockObject_MockObject */ private $customerData; @@ -76,7 +76,7 @@ protected function setUp() /** * Test getSectionData() when user is login and require captcha */ - public function testGetSectionData() + public function testGetSectionDataWhenLoginAndRequireCaptcha() { $emailLogin = 'test@localhost.com'; From 9992c0e30b4e368ca978be334c541996c66207cc Mon Sep 17 00:00:00 2001 From: Eden Date: Sat, 23 Nov 2019 22:41:29 +0700 Subject: [PATCH 3/3] refactor code to pass review --- .../Test/Unit/CustomerData/CaptchaTest.php | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php b/app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php index b34de7778e176..a791039fe27f9 100644 --- a/app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php +++ b/app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php @@ -14,28 +14,19 @@ use Magento\Captcha\Model\DefaultModel; use Magento\Customer\Api\Data\CustomerInterface as CustomerData; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use PHPUnit\Framework\TestCase; -/** - * Test class to cover \Magento\Captcha\CustomerData\Captcha - * - * Class \Magento\Captcha\Test\Unit\CustomerData\CaptchaTest - */ -class CaptchaTest extends \PHPUnit\Framework\TestCase +class CaptchaTest extends TestCase { /** * @var CaptchaHelper|\PHPUnit_Framework_MockObject_MockObject */ - private $helper; + private $helperMock; /** * @var CustomerSession|\PHPUnit_Framework_MockObject_MockObject */ - private $customerSession; - - /** - * @var CustomerData|\PHPUnit_Framework_MockObject_MockObject - */ - private $customerData; + private $customerSessionMock; /** * @var Captcha @@ -57,8 +48,8 @@ class CaptchaTest extends \PHPUnit\Framework\TestCase */ protected function setUp() { - $this->helper = $this->createMock(CaptchaHelper::class); - $this->customerSession = $this->createMock(CustomerSession::class); + $this->helperMock = $this->createMock(CaptchaHelper::class); + $this->customerSessionMock = $this->createMock(CustomerSession::class); $this->formIds = [ 'user_login' ]; @@ -66,9 +57,9 @@ protected function setUp() $this->model = $this->objectManagerHelper->getObject( Captcha::class, [ - 'helper' => $this->helper, + 'helper' => $this->helperMock, 'formIds' => $this->formIds, - 'customerSession' => $this->customerSession + 'customerSession' => $this->customerSessionMock ] ); } @@ -83,15 +74,15 @@ public function testGetSectionDataWhenLoginAndRequireCaptcha() $userLoginModel = $this->createMock(DefaultModel::class); $userLoginModel->expects($this->any())->method('isRequired')->with($emailLogin) ->willReturn(true); - $this->helper->expects($this->any())->method('getCaptcha')->with('user_login')->willReturn($userLoginModel); + $this->helperMock->expects($this->any())->method('getCaptcha')->with('user_login')->willReturn($userLoginModel); - $this->customerSession->expects($this->any())->method('isLoggedIn') + $this->customerSessionMock->expects($this->any())->method('isLoggedIn') ->willReturn(true); - $this->customerData = $this->createMock(CustomerData::class); - $this->customerData->expects($this->any())->method('getEmail')->willReturn($emailLogin); - $this->customerSession->expects($this->any())->method('getCustomerData') - ->willReturn($this->customerData); + $customerDataMock = $this->createMock(CustomerData::class); + $customerDataMock->expects($this->any())->method('getEmail')->willReturn($emailLogin); + $this->customerSessionMock->expects($this->any())->method('getCustomerData') + ->willReturn($customerDataMock); /* Assert to test */ $this->assertEquals(