Skip to content

Commit 5c101bf

Browse files
committed
Covering the Wishlist classes by integration and Unit Tests
1 parent 345b70e commit 5c101bf

File tree

2 files changed

+179
-1
lines changed

2 files changed

+179
-1
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Wishlist\Test\Unit\Model\Product;
9+
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
13+
use Magento\Framework\DB\Adapter\AdapterInterface;
14+
use Magento\Wishlist\Model\Product\AttributeValueProvider;
15+
use PHPUnit\Framework\TestCase;
16+
use PHPUnit_Framework_MockObject_MockObject;
17+
18+
/**
19+
* @covers CreditCardTokenFormatter
20+
*/
21+
class AttributeValueProviderTest extends TestCase
22+
{
23+
/**
24+
* @var AttributeValueProvider|PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $attributeValueProvider;
27+
28+
/**
29+
* @var CollectionFactory|PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $productCollectionFactoryMock;
32+
33+
/**
34+
* @var @var Product|PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
private $productMock;
37+
38+
/**
39+
* @var AdapterInterface|PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
private $connectionMock;
42+
43+
/**
44+
* Set Up
45+
*
46+
* @return void
47+
*/
48+
protected function setUp()
49+
{
50+
$this->productCollectionFactoryMock = $this->createPartialMock(
51+
CollectionFactory::class,
52+
['create']
53+
);
54+
$this->attributeValueProvider = new AttributeValueProvider(
55+
$this->productCollectionFactoryMock
56+
);
57+
}
58+
59+
/**
60+
* Get attribute text when the flat table is disabled
61+
*
62+
* @param int $productId
63+
* @param string $attributeCode
64+
* @param string $attributeText
65+
* @return void
66+
* @dataProvider attributeDataProvider
67+
*/
68+
public function testGetAttributeTextWhenFlatIsDisabled(int $productId, string $attributeCode, string $attributeText)
69+
{
70+
$this->productMock = $this->getMockBuilder(Product::class)
71+
->disableOriginalConstructor()
72+
->setMethods(['getData'])
73+
->getMock();
74+
75+
$this->productMock->expects($this->any())
76+
->method('getData')
77+
->with($attributeCode)
78+
->willReturn($attributeText);
79+
80+
$productCollection = $this->getMockBuilder(Collection::class)
81+
->disableOriginalConstructor()
82+
->setMethods([
83+
'addIdFilter', 'addStoreFilter', 'addAttributeToSelect', 'isEnabledFlat', 'getFirstItem'
84+
])->getMock();
85+
86+
$productCollection->expects($this->any())
87+
->method('addIdFilter')
88+
->willReturnSelf();
89+
$productCollection->expects($this->any())
90+
->method('addStoreFilter')
91+
->willReturnSelf();
92+
$productCollection->expects($this->any())
93+
->method('addAttributeToSelect')
94+
->willReturnSelf();
95+
$productCollection->expects($this->any())
96+
->method('isEnabledFlat')
97+
->willReturn(false);
98+
$productCollection->expects($this->any())
99+
->method('getFirstItem')
100+
->willReturn($this->productMock);
101+
102+
$this->productCollectionFactoryMock->expects($this->atLeastOnce())
103+
->method('create')
104+
->willReturn($productCollection);
105+
106+
$actual = $this->attributeValueProvider->getRawAttributeValue($productId, $attributeCode);
107+
108+
$this->assertEquals($attributeText, $actual);
109+
}
110+
111+
112+
/**
113+
* Get attribute text when the flat table is enabled
114+
*
115+
* @dataProvider attributeDataProvider
116+
* @param int $productId
117+
* @param string $attributeCode
118+
* @param string $attributeText
119+
* @return void
120+
*/
121+
public function testGetAttributeTextWhenFlatIsEnabled(int $productId, string $attributeCode, string $attributeText)
122+
{
123+
$this->connectionMock = $this->getMockBuilder(AdapterInterface::class)->getMockForAbstractClass();
124+
$this->connectionMock->expects($this->any())
125+
->method('fetchRow')
126+
->willReturn([
127+
$attributeCode => $attributeText
128+
]);
129+
$this->productMock = $this->getMockBuilder(Product::class)
130+
->disableOriginalConstructor()
131+
->setMethods(['getData'])
132+
->getMock();
133+
$this->productMock->expects($this->any())
134+
->method('getData')
135+
->with($attributeCode)
136+
->willReturn($attributeText);
137+
138+
$productCollection = $this->getMockBuilder(Collection::class)
139+
->disableOriginalConstructor()
140+
->setMethods([
141+
'addIdFilter', 'addStoreFilter', 'addAttributeToSelect', 'isEnabledFlat', 'getConnection'
142+
])->getMock();
143+
144+
$productCollection->expects($this->any())
145+
->method('addIdFilter')
146+
->willReturnSelf();
147+
$productCollection->expects($this->any())
148+
->method('addStoreFilter')
149+
->willReturnSelf();
150+
$productCollection->expects($this->any())
151+
->method('addAttributeToSelect')
152+
->willReturnSelf();
153+
$productCollection->expects($this->any())
154+
->method('isEnabledFlat')
155+
->willReturn(true);
156+
$productCollection->expects($this->any())
157+
->method('getConnection')
158+
->willReturn($this->connectionMock);
159+
160+
$this->productCollectionFactoryMock->expects($this->atLeastOnce())
161+
->method('create')
162+
->willReturn($productCollection);
163+
164+
$actual = $this->attributeValueProvider->getRawAttributeValue($productId, $attributeCode);
165+
166+
$this->assertEquals($attributeText, $actual);
167+
}
168+
169+
/**
170+
* @return array
171+
*/
172+
public function attributeDataProvider(): array
173+
{
174+
return [
175+
[1, 'attribute_code', 'Attribute Text']
176+
];
177+
}
178+
}

dev/tests/integration/testsuite/Magento/Wishlist/Controller/ShareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function login($customerId)
7676
private function prepareRequestData($invalidData = false)
7777
{
7878
Bootstrap::getInstance()->loadArea(Area::AREA_FRONTEND);
79-
$emails = !$invalidData ? 'email-1@example.com,email-1@example.com' : '';
79+
$emails = !$invalidData ? 'email-1@example.com,email-2@example.com' : '';
8080

8181
/** @var FormKey $formKey */
8282
$formKey = $this->_objectManager->get(FormKey::class);

0 commit comments

Comments
 (0)