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

[Backport] [Wishlist] Covering the Wishlist classes by integration and unit tests #21859

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Wishlist\Test\Unit\Model\Product;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Wishlist\Model\Product\AttributeValueProvider;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;

/**
* AttributeValueProviderTest
*/
class AttributeValueProviderTest extends TestCase
{
/**
* @var AttributeValueProvider|PHPUnit_Framework_MockObject_MockObject
*/
private $attributeValueProvider;

/**
* @var CollectionFactory|PHPUnit_Framework_MockObject_MockObject
*/
private $productCollectionFactoryMock;

/**
* @var Product|PHPUnit_Framework_MockObject_MockObject
*/
private $productMock;

/**
* @var AdapterInterface|PHPUnit_Framework_MockObject_MockObject
*/
private $connectionMock;

/**
* Set Up
*
* @return void
*/
protected function setUp()
{
$this->productCollectionFactoryMock = $this->createPartialMock(
CollectionFactory::class,
['create']
);
$this->attributeValueProvider = new AttributeValueProvider(
$this->productCollectionFactoryMock
);
}

/**
* Get attribute text when the flat table is disabled
*
* @param int $productId
* @param string $attributeCode
* @param string $attributeText
* @return void
* @dataProvider attributeDataProvider
*/
public function testGetAttributeTextWhenFlatIsDisabled(int $productId, string $attributeCode, string $attributeText)
{
$this->productMock = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->setMethods(['getData'])
->getMock();

$this->productMock->expects($this->any())
->method('getData')
->with($attributeCode)
->willReturn($attributeText);

$productCollection = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->setMethods([
'addIdFilter', 'addStoreFilter', 'addAttributeToSelect', 'isEnabledFlat', 'getFirstItem'
])->getMock();

$productCollection->expects($this->any())
->method('addIdFilter')
->willReturnSelf();
$productCollection->expects($this->any())
->method('addStoreFilter')
->willReturnSelf();
$productCollection->expects($this->any())
->method('addAttributeToSelect')
->willReturnSelf();
$productCollection->expects($this->any())
->method('isEnabledFlat')
->willReturn(false);
$productCollection->expects($this->any())
->method('getFirstItem')
->willReturn($this->productMock);

$this->productCollectionFactoryMock->expects($this->atLeastOnce())
->method('create')
->willReturn($productCollection);

$actual = $this->attributeValueProvider->getRawAttributeValue($productId, $attributeCode);

$this->assertEquals($attributeText, $actual);
}

/**
* Get attribute text when the flat table is enabled
*
* @dataProvider attributeDataProvider
* @param int $productId
* @param string $attributeCode
* @param string $attributeText
* @return void
*/
public function testGetAttributeTextWhenFlatIsEnabled(int $productId, string $attributeCode, string $attributeText)
{
$this->connectionMock = $this->getMockBuilder(AdapterInterface::class)->getMockForAbstractClass();
$this->connectionMock->expects($this->any())
->method('fetchRow')
->willReturn([
$attributeCode => $attributeText
]);
$this->productMock = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->setMethods(['getData'])
->getMock();
$this->productMock->expects($this->any())
->method('getData')
->with($attributeCode)
->willReturn($attributeText);

$productCollection = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->setMethods([
'addIdFilter', 'addStoreFilter', 'addAttributeToSelect', 'isEnabledFlat', 'getConnection'
])->getMock();

$productCollection->expects($this->any())
->method('addIdFilter')
->willReturnSelf();
$productCollection->expects($this->any())
->method('addStoreFilter')
->willReturnSelf();
$productCollection->expects($this->any())
->method('addAttributeToSelect')
->willReturnSelf();
$productCollection->expects($this->any())
->method('isEnabledFlat')
->willReturn(true);
$productCollection->expects($this->any())
->method('getConnection')
->willReturn($this->connectionMock);

$this->productCollectionFactoryMock->expects($this->atLeastOnce())
->method('create')
->willReturn($productCollection);

$actual = $this->attributeValueProvider->getRawAttributeValue($productId, $attributeCode);

$this->assertEquals($attributeText, $actual);
}

/**
* @return array
*/
public function attributeDataProvider(): array
{
return [
[1, 'attribute_code', 'Attribute Text']
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Wishlist\Controller;

use Magento\Customer\Model\Session;
use Magento\Framework\App\Area;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Request;
use Magento\TestFramework\TestCase\AbstractController;

/**
* @magentoAppIsolation enabled
*/
class ShareTest extends AbstractController
{
/**
* Test share wishlist with correct data
*
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
*/
public function testSuccessfullyShareWishlist()
{
$this->login(1);
$this->prepareRequestData();
$this->dispatch('wishlist/index/send/');

$this->assertSessionMessages(
$this->equalTo(['Your wish list has been shared.']),
MessageInterface::TYPE_SUCCESS
);
}

/**
* Test share wishlist with incorrect data
*
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
*/
public function testShareWishlistWithoutEmails()
{
$this->login(1);
$this->prepareRequestData(true);
$this->dispatch('wishlist/index/send/');

$this->assertSessionMessages(
$this->equalTo(['Please enter an email address.']),
MessageInterface::TYPE_ERROR
);
}

/**
* Login the user
*
* @param string $customerId Customer to mark as logged in for the session
* @return void
*/
protected function login($customerId)
{
/** @var Session $session */
$session = $this->_objectManager->get(Session::class);
$session->loginById($customerId);
}

/**
* Prepares the request with data
*
* @param bool $invalidData
* @return void
*/
private function prepareRequestData($invalidData = false)
{
Bootstrap::getInstance()->loadArea(Area::AREA_FRONTEND);
$emails = !$invalidData ? 'email-1@example.com,email-2@example.com' : '';

/** @var FormKey $formKey */
$formKey = $this->_objectManager->get(FormKey::class);
$post = [
'emails' => $emails,
'message' => '',
'form_key' => $formKey->getFormKey(),
];

$this->getRequest()->setMethod(Request::METHOD_POST);
$this->getRequest()->setPostValue($post);
}
}