diff --git a/app/code/Magento/Quote/Model/ChangeQuoteControl.php b/app/code/Magento/Quote/Model/ChangeQuoteControl.php index b88898a816d66..92e25ca6c7d3a 100644 --- a/app/code/Magento/Quote/Model/ChangeQuoteControl.php +++ b/app/code/Magento/Quote/Model/ChangeQuoteControl.php @@ -3,7 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\Quote\Model; @@ -12,13 +11,10 @@ use Magento\Quote\Api\ChangeQuoteControlInterface; use Magento\Quote\Api\Data\CartInterface; -/** - * {@inheritdoc} - */ class ChangeQuoteControl implements ChangeQuoteControlInterface { /** - * @var UserContextInterface $userContext + * @var UserContextInterface */ private $userContext; @@ -31,25 +27,20 @@ public function __construct(UserContextInterface $userContext) } /** - * {@inheritdoc} + * @inheritdoc */ public function isAllowed(CartInterface $quote): bool { switch ($this->userContext->getUserType()) { case UserContextInterface::USER_TYPE_CUSTOMER: - $isAllowed = ($quote->getCustomerId() == $this->userContext->getUserId()); - break; + return ($quote->getCustomerId() == $this->userContext->getUserId()); case UserContextInterface::USER_TYPE_GUEST: - $isAllowed = ($quote->getCustomerId() === null); - break; + return ($quote->getCustomerId() === null); case UserContextInterface::USER_TYPE_ADMIN: case UserContextInterface::USER_TYPE_INTEGRATION: - $isAllowed = true; - break; - default: - $isAllowed = false; + return true; } - return $isAllowed; + return false; } } diff --git a/app/code/Magento/Quote/Test/Unit/Model/ChangeQuoteControlTest.php b/app/code/Magento/Quote/Test/Unit/Model/ChangeQuoteControlTest.php index f302372344c11..a467f3e25d698 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/ChangeQuoteControlTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/ChangeQuoteControlTest.php @@ -16,130 +16,94 @@ /** * Unit test for \Magento\Quote\Model\ChangeQuoteControl - * - * Class \Magento\Quote\Test\Unit\Model\ChangeQuoteControlTest */ class ChangeQuoteControlTest extends TestCase { - /** - * @var ObjectManager - */ - protected $objectManager; - /** * @var ChangeQuoteControl */ protected $model; /** - * @var MockObject + * @var MockObject|UserContextInterface */ protected $userContextMock; /** - * @var MockObject + * @var MockObject|CartInterface */ protected $quoteMock; protected function setUp(): void { - $this->objectManager = new ObjectManager($this); $this->userContextMock = $this->getMockForAbstractClass(UserContextInterface::class); - $this->model = $this->objectManager->getObject( - ChangeQuoteControl::class, - [ - 'userContext' => $this->userContextMock - ] - ); - - $this->quoteMock = $this->getMockForAbstractClass( - CartInterface::class, - [], - '', - false, - true, - true, - ['getCustomerId'] - ); + $this->model = new ChangeQuoteControl($this->userContextMock); + + $this->quoteMock = $this->getMockBuilder(CartInterface::class) + ->disableOriginalConstructor() + ->addMethods(['getCustomerId']) + ->getMockForAbstractClass(); } - /** - * Test if the quote is belonged to customer - */ public function testIsAllowedIfTheQuoteIsBelongedToCustomer() { $quoteCustomerId = 1; - $this->quoteMock->expects($this->any())->method('getCustomerId') + $this->quoteMock->method('getCustomerId') ->willReturn($quoteCustomerId); - $this->userContextMock->expects($this->any())->method('getUserType') + $this->userContextMock->method('getUserType') ->willReturn(UserContextInterface::USER_TYPE_CUSTOMER); - $this->userContextMock->expects($this->any())->method('getUserId') + $this->userContextMock->method('getUserId') ->willReturn($quoteCustomerId); $this->assertTrue($this->model->isAllowed($this->quoteMock)); } - /** - * Test if the quote is not belonged to customer - */ public function testIsAllowedIfTheQuoteIsNotBelongedToCustomer() { $currentCustomerId = 1; $quoteCustomerId = 2; - $this->quoteMock->expects($this->any())->method('getCustomerId') + $this->quoteMock->method('getCustomerId') ->willReturn($quoteCustomerId); - $this->userContextMock->expects($this->any())->method('getUserType') + $this->userContextMock->method('getUserType') ->willReturn(UserContextInterface::USER_TYPE_CUSTOMER); - $this->userContextMock->expects($this->any())->method('getUserId') + $this->userContextMock->method('getUserId') ->willReturn($currentCustomerId); $this->assertFalse($this->model->isAllowed($this->quoteMock)); } - /** - * Test if the quote is belonged to guest and the context is guest - */ public function testIsAllowedIfQuoteIsBelongedToGuestAndContextIsGuest() { $quoteCustomerId = null; - $this->quoteMock->expects($this->any())->method('getCustomerId') + $this->quoteMock->method('getCustomerId') ->willReturn($quoteCustomerId); - $this->userContextMock->expects($this->any())->method('getUserType') + $this->userContextMock->method('getUserType') ->willReturn(UserContextInterface::USER_TYPE_GUEST); $this->assertTrue($this->model->isAllowed($this->quoteMock)); } - /** - * Test if the quote is belonged to customer and the context is guest - */ public function testIsAllowedIfQuoteIsBelongedToCustomerAndContextIsGuest() { $quoteCustomerId = 1; - $this->quoteMock->expects($this->any())->method('getCustomerId') + $this->quoteMock->method('getCustomerId') ->willReturn($quoteCustomerId); - $this->userContextMock->expects($this->any())->method('getUserType') + $this->userContextMock->method('getUserType') ->willReturn(UserContextInterface::USER_TYPE_GUEST); $this->assertFalse($this->model->isAllowed($this->quoteMock)); } - /** - * Test if the context is admin - */ public function testIsAllowedIfContextIsAdmin() { - $this->userContextMock->expects($this->any())->method('getUserType') + $this->userContextMock->method('getUserType') ->willReturn(UserContextInterface::USER_TYPE_ADMIN); $this->assertTrue($this->model->isAllowed($this->quoteMock)); } - /** - * Test if the context is integration - */ public function testIsAllowedIfContextIsIntegration() { - $this->userContextMock->expects($this->any())->method('getUserType') + $this->userContextMock->method('getUserType') ->willReturn(UserContextInterface::USER_TYPE_INTEGRATION); $this->assertTrue($this->model->isAllowed($this->quoteMock)); }