Skip to content

Commit 8d44a36

Browse files
committed
[Downloadable Product] Cover Sample DataProvider by Unit Test
1 parent 36ea828 commit 8d44a36

File tree

1 file changed

+158
-0
lines changed
  • app/code/Magento/Downloadable/Test/Unit/Ui/DataProvider/Product/Form/Modifier/Data

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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\Downloadable\Test\Unit\Ui\DataProvider\Product\Form\Modifier\Data;
9+
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
11+
use Magento\Downloadable\Ui\DataProvider\Product\Form\Modifier\Data\Samples;
12+
use \Magento\Framework\Escaper;
13+
use Magento\Downloadable\Model\Product\Type;
14+
use Magento\Catalog\Model\Locator\LocatorInterface;
15+
use Magento\Framework\App\Config\ScopeConfigInterface;
16+
use Magento\Downloadable\Helper\File as DownloadableFile;
17+
use Magento\Framework\UrlInterface;
18+
use Magento\Catalog\Api\Data\ProductInterface;
19+
20+
/**
21+
* Test class to cover Sample Modifier
22+
*
23+
* Class \Magento\Downloadable\Test\Unit\Ui\DataProvider\Product\Form\Modifier\Data\SampleTest
24+
*/
25+
class SamplesTest extends \PHPUnit\Framework\TestCase
26+
{
27+
/**
28+
* @var ObjectManagerHelper
29+
*/
30+
private $objectManagerHelper;
31+
32+
/**
33+
* @var LocatorInterface|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
private $locatorMock;
36+
37+
/**
38+
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
private $scopeConfigMock;
41+
42+
/**
43+
* @var Escaper|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
private $escaperMock;
46+
47+
/**
48+
* @var DownloadableFile|\PHPUnit_Framework_MockObject_MockObject
49+
*/
50+
private $downloadableFileMock;
51+
52+
/**
53+
* @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject
54+
*/
55+
private $urlBuilderMock;
56+
57+
/**
58+
* @var ProductInterface|\PHPUnit_Framework_MockObject_MockObject
59+
*/
60+
private $productMock;
61+
62+
/**
63+
* @var Samples
64+
*/
65+
private $samples;
66+
67+
/**
68+
* @return void
69+
*/
70+
protected function setUp()
71+
{
72+
$this->objectManagerHelper = new ObjectManagerHelper($this);
73+
$this->productMock = $this->getMockBuilder(ProductInterface::class)
74+
->setMethods(['getSamplesTitle', 'getId', 'getTypeId'])
75+
->getMockForAbstractClass();
76+
$this->locatorMock = $this->createMock(LocatorInterface::class);
77+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
78+
$this->escaperMock = $this->createMock(Escaper::class);
79+
$this->downloadableFileMock = $this->createMock(DownloadableFile::class);
80+
$this->urlBuilderMock = $this->createMock(UrlInterface::class);
81+
$this->samples = $this->objectManagerHelper->getObject(
82+
Samples::class,
83+
[
84+
'escaper' => $this->escaperMock,
85+
'locator' => $this->locatorMock,
86+
'scopeConfig' => $this->scopeConfigMock,
87+
'downloadableFile' => $this->downloadableFileMock,
88+
'urlBuilder' => $this->urlBuilderMock
89+
]
90+
);
91+
}
92+
93+
/**
94+
* Test getSamplesTitle()
95+
*
96+
* @param int|null $id
97+
* @param string $typeId
98+
* @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expectedGetTitle
99+
* @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expectedGetValue
100+
* @return void
101+
* @dataProvider getSamplesTitleDataProvider
102+
*/
103+
public function testGetSamplesTitle($id, $typeId, $expectedGetTitle, $expectedGetValue)
104+
{
105+
$title = 'My Title';
106+
$this->locatorMock->expects($this->any())
107+
->method('getProduct')
108+
->willReturn($this->productMock);
109+
$this->productMock->expects($this->once())
110+
->method('getId')
111+
->willReturn($id);
112+
$this->productMock->expects($this->any())
113+
->method('getTypeId')
114+
->willReturn($typeId);
115+
$this->productMock->expects($expectedGetTitle)
116+
->method('getSamplesTitle')
117+
->willReturn($title);
118+
$this->scopeConfigMock->expects($expectedGetValue)
119+
->method('getValue')
120+
->willReturn($title);
121+
122+
/* Assert Result */
123+
$this->assertEquals($title, $this->samples->getSamplesTitle());
124+
}
125+
126+
/**
127+
* @return array
128+
*/
129+
public function getSamplesTitleDataProvider()
130+
{
131+
return [
132+
[
133+
'id' => 1,
134+
'typeId' => Type::TYPE_DOWNLOADABLE,
135+
'expectedGetTitle' => $this->once(),
136+
'expectedGetValue' => $this->never(),
137+
],
138+
[
139+
'id' => null,
140+
'typeId' => Type::TYPE_DOWNLOADABLE,
141+
'expectedGetTitle' => $this->never(),
142+
'expectedGetValue' => $this->once(),
143+
],
144+
[
145+
'id' => 1,
146+
'typeId' => 'someType',
147+
'expectedGetTitle' => $this->never(),
148+
'expectedGetValue' => $this->once(),
149+
],
150+
[
151+
'id' => null,
152+
'typeId' => 'someType',
153+
'expectedGetTitle' => $this->never(),
154+
'expectedGetValue' => $this->once(),
155+
],
156+
];
157+
}
158+
}

0 commit comments

Comments
 (0)