-
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
Allow wishlist share when all items are out of stock #26185
Merged
magento-engcom-team
merged 6 commits into
magento:2.4-develop
from
pmclain:hotfix/oos-share-wishlist
Jan 17, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6cf13db
Allow wishlist share when all items are out of stock
860af38
Fix static tests
21bdc67
Cover changes with Unit test
Nazar65 3c3018d
Cover changes with unit test
Nazar65 74a244f
Refactor per review comments, add new test case
Nazar65 1748633
rename const with prefix "stub" move Object Manager to separate
Nazar65 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
290 changes: 290 additions & 0 deletions
290
app/code/Magento/Wishlist/Test/Unit/Controller/Index/UpdateTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,290 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Wishlist\Test\Unit\Controller\Index; | ||
|
||
use Magento\Backend\Model\View\Result\Redirect; | ||
use Magento\Framework\App\Action\Context; | ||
use Magento\Framework\App\RequestInterface; | ||
use Magento\Framework\Controller\ResultFactory; | ||
use Magento\Framework\Data\Form\FormKey\Validator; | ||
use Magento\Framework\Exception\NotFoundException; | ||
use Magento\Framework\Message\ManagerInterface; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; | ||
use Magento\Wishlist\Controller\Index\Update; | ||
use Magento\Wishlist\Controller\WishlistProviderInterface; | ||
use Magento\Wishlist\Helper\Data; | ||
use Magento\Wishlist\Model\Item; | ||
use Magento\Wishlist\Model\LocaleQuantityProcessor; | ||
use Magento\Wishlist\Model\Wishlist; | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit_Framework_MockObject_MockObject as MockObject; | ||
|
||
/** | ||
* Test for upate controller wishlist | ||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) | ||
*/ | ||
class UpdateTest extends TestCase | ||
{ | ||
private const STUB_ITEM_ID = 1; | ||
|
||
private const STUB_WISHLIST_PRODUCT_QTY = 21; | ||
|
||
/** | ||
* @var MockObject|Validator $formKeyValidatorMock | ||
*/ | ||
private $formKeyValidatorMock; | ||
|
||
/** | ||
* @var MockObject|WishlistProviderInterface $wishlistProviderMock | ||
*/ | ||
private $wishlistProviderMock; | ||
|
||
/** | ||
* @var MockObject|LocaleQuantityProcessor $quantityProcessorMock | ||
*/ | ||
private $quantityProcessorMock; | ||
|
||
/** | ||
* @var Update $updateController | ||
*/ | ||
private $updateController; | ||
|
||
/** | ||
* @var MockObject|Context$contextMock | ||
*/ | ||
private $contextMock; | ||
|
||
/** | ||
* @var MockObject|Redirect $resultRedirectMock | ||
*/ | ||
private $resultRedirectMock; | ||
|
||
/** | ||
* @var MockObject|ResultFactory $resultFatoryMock | ||
*/ | ||
private $resultFactoryMock; | ||
|
||
/** | ||
* @var MockObject|RequestInterface $requestMock | ||
*/ | ||
private $requestMock; | ||
|
||
/** | ||
* @var MockObject|ObjectManagerInterface $objectManagerMock | ||
*/ | ||
private $objectManagerMock; | ||
|
||
/** | ||
* @var MockObject|ManagerInterface $messageManagerMock | ||
*/ | ||
private $messageManagerMock; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->formKeyValidatorMock = $this->createMock(Validator::class); | ||
$this->wishlistProviderMock = $this->createMock(WishlistProviderInterface::class); | ||
$this->quantityProcessorMock = $this->createMock(LocaleQuantityProcessor::class); | ||
$this->contextMock = $this->createMock(Context::class); | ||
$this->resultRedirectMock = $this->createMock(Redirect::class); | ||
$this->resultFactoryMock = $this->createPartialMock(ResultFactory::class, ['create']); | ||
$this->messageManagerMock = $this->createMock(ManagerInterface::class); | ||
$this->objectManagerMock = $this->createMock(ObjectManagerInterface::class); | ||
$this->requestMock = $this->getMockBuilder(RequestInterface::class) | ||
->setMethods(['getPostValue']) | ||
->getMockForAbstractClass(); | ||
|
||
$this->resultFactoryMock->expects($this->any()) | ||
->method('create') | ||
->willReturn($this->resultRedirectMock); | ||
$this->contextMock->expects($this->once()) | ||
->method('getResultFactory') | ||
->willReturn($this->resultFactoryMock); | ||
$this->contextMock->expects($this->once()) | ||
->method('getObjectManager') | ||
->willReturn($this->objectManagerMock); | ||
$this->contextMock->expects($this->any()) | ||
->method('getRequest') | ||
->willReturn($this->requestMock); | ||
$this->contextMock->expects($this->any()) | ||
->method('getMessageManager') | ||
->willReturn($this->messageManagerMock); | ||
|
||
$objectManager = new ObjectManagerHelper($this); | ||
|
||
$this->updateController = $objectManager->getObject( | ||
Update::class, | ||
[ | ||
'context' => $this->contextMock, | ||
'_formKeyValidator' => $this->formKeyValidatorMock, | ||
'wishlistProvider' => $this->wishlistProviderMock, | ||
'quantityProcessor' => $this->quantityProcessorMock | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Test for update method Wishlist controller. | ||
* | ||
* @dataProvider getWishlistDataProvider | ||
* @param array $wishlistDataProvider | ||
* @param array $postData | ||
* @return void | ||
*/ | ||
public function testUpdate(array $wishlistDataProvider, array $postData): void | ||
{ | ||
$wishlist = $this->createMock(Wishlist::class); | ||
$itemMock = $this->getMockBuilder(Item::class) | ||
->disableOriginalConstructor() | ||
->setMethods( | ||
[ | ||
'load', | ||
'getId', | ||
'getWishlistId', | ||
'setQty', | ||
'save', | ||
'getDescription', | ||
'setDescription', | ||
'getProduct', | ||
'getName' | ||
] | ||
)->getMock(); | ||
$dataMock = $this->createMock(Data::class); | ||
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->formKeyValidatorMock->expects($this->once()) | ||
->method('validate') | ||
->with($this->requestMock) | ||
->willReturn(true); | ||
$this->wishlistProviderMock->expects($this->once()) | ||
->method('getWishlist') | ||
->willReturn($wishlist); | ||
$wishlist->expects($this->exactly(2)) | ||
->method('getId') | ||
->willReturn($wishlistDataProvider['id']); | ||
$this->requestMock->expects($this->once()) | ||
->method('getPostValue') | ||
->willReturn($postData); | ||
$this->resultRedirectMock->expects($this->once()) | ||
->method('setPath') | ||
->with('*', ['wishlist_id' => $wishlistDataProvider['id']]); | ||
$this->objectManagerMock->expects($this->once()) | ||
->method('create') | ||
->with(Item::class) | ||
->willReturn($itemMock); | ||
$itemMock->expects($this->once()) | ||
->method('load') | ||
->with(1) | ||
->willReturnSelf(); | ||
$itemMock->expects($this->once()) | ||
->method('getWishLIstId') | ||
->willReturn($wishlistDataProvider['id']); | ||
$itemMock->expects($this->once()) | ||
->method('getDescription') | ||
->willReturn(''); | ||
$itemMock->expects($this->once()) | ||
->method('setDescription') | ||
->willReturnSelf(); | ||
$itemMock->expects($this->once()) | ||
->method('setQty') | ||
->willReturnSelf(); | ||
$this->objectManagerMock->expects($this->exactly(2)) | ||
->method('get') | ||
->with(Data::class) | ||
->willReturn($dataMock); | ||
$dataMock->expects($this->once()) | ||
->method('defaultCommentString') | ||
->willReturn(''); | ||
$dataMock->expects($this->once()) | ||
->method('calculate'); | ||
$this->quantityProcessorMock->expects($this->once()) | ||
->method('process') | ||
->willReturn($postData['qty']); | ||
$itemMock->expects($this->once()) | ||
->method('getProduct') | ||
->willReturn($productMock); | ||
$productMock->expects($this->once()) | ||
->method('getName') | ||
->willReturn('product'); | ||
$this->messageManagerMock->expects($this->once()) | ||
->method('addSuccessMessage'); | ||
|
||
$this->assertEquals($this->resultRedirectMock, $this->updateController->execute()); | ||
} | ||
|
||
/** | ||
* Verify update method if post data not available | ||
* | ||
* @dataProvider getWishlistDataProvider | ||
* @param array $wishlistDataProvider | ||
* @return void | ||
*/ | ||
public function testUpdateRedirectWhenNoPostData(array $wishlistDataProvider): void | ||
{ | ||
$wishlist = $this->createMock(Wishlist::class); | ||
|
||
$this->formKeyValidatorMock->expects($this->once()) | ||
->method('validate') | ||
->willReturn(true); | ||
$this->wishlistProviderMock->expects($this->once()) | ||
->method('getWishlist') | ||
->willReturn($wishlist); | ||
$wishlist->expects($this->exactly(1)) | ||
->method('getId') | ||
->willReturn($wishlistDataProvider['id']); | ||
$this->resultRedirectMock->expects($this->once()) | ||
->method('setPath') | ||
->with('*', ['wishlist_id' => $wishlistDataProvider['id']]); | ||
$this->requestMock->expects($this->once()) | ||
->method('getPostValue') | ||
->willReturn(null); | ||
|
||
$this->assertEquals($this->resultRedirectMock, $this->updateController->execute()); | ||
} | ||
|
||
/** | ||
* Check if wishlist not availbale, and exception is shown | ||
* | ||
* @return void | ||
*/ | ||
public function testUpdateThrowsNotFoundExceptionWhenWishlistDoNotExist(): void | ||
{ | ||
$this->formKeyValidatorMock->expects($this->once()) | ||
->method('validate') | ||
->willReturn(true); | ||
$this->wishlistProviderMock->expects($this->once()) | ||
->method('getWishlist') | ||
->willReturn(null); | ||
$this->expectException(NotFoundException::class); | ||
$this->updateController->execute(); | ||
} | ||
|
||
/** | ||
* Dataprovider for Update test | ||
* | ||
* @return array | ||
*/ | ||
public function getWishlistDataProvider(): array | ||
{ | ||
return | ||
[ | ||
[ | ||
[ | ||
'id' => self::STUB_ITEM_ID | ||
], | ||
[ | ||
'qty' => [self::STUB_ITEM_ID => self::STUB_WISHLIST_PRODUCT_QTY], | ||
'description' => [self::STUB_ITEM_ID => 'Description for item_id 1'] | ||
] | ||
] | ||
]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
👍