Skip to content

Commit 33b13b6

Browse files
author
Onischenko, Yaroslav(yonischenko)
committed
Merge pull request #252 from magento-nord/develop
[NORD] Sample Data tests coverage. Bugfixes
2 parents 9b08b0b + f4a097a commit 33b13b6

File tree

14 files changed

+394
-54
lines changed

14 files changed

+394
-54
lines changed

app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Filter/Preprocessor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private function processQueryWithField(FilterInterface $filter, $isNegation, $qu
107107
$query
108108
);
109109
} elseif ($filter->getField() === 'category_ids') {
110-
return 'category_ids_index.category_id = ' . $filter->getValue();
110+
return 'category_ids_index.category_id = ' . (int) $filter->getValue();
111111
} elseif ($attribute->isStatic()) {
112112
$alias = $this->tableMapper->getMappingAlias($filter);
113113
$resultQuery = str_replace(
@@ -194,10 +194,10 @@ private function processTermSelect(FilterInterface $filter, $isNegation)
194194
$value = sprintf(
195195
'%s IN (%s)',
196196
($isNegation ? 'NOT' : ''),
197-
implode(',', $filter->getValue())
197+
implode(',', array_map([$this->connection, 'quote'], $filter->getValue()))
198198
);
199199
} else {
200-
$value = ($isNegation ? '!' : '') . '= ' . $filter->getValue();
200+
$value = ($isNegation ? '!' : '') . '= ' . $this->connection->quote($filter->getValue());
201201
}
202202
$resultQuery = sprintf(
203203
'%1$s.value %2$s',

app/code/Magento/CatalogSearch/Test/Unit/Model/Adapter/Mysql/Filter/PreprocessorTest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected function setUp()
104104
->getMock();
105105
$this->connection = $this->getMockBuilder('\Magento\Framework\DB\Adapter\AdapterInterface')
106106
->disableOriginalConstructor()
107-
->setMethods(['select', 'getIfNullSql'])
107+
->setMethods(['select', 'getIfNullSql', 'quote'])
108108
->getMockForAbstractClass();
109109
$this->select = $this->getMockBuilder('\Magento\Framework\DB\Select')
110110
->disableOriginalConstructor()
@@ -170,9 +170,25 @@ public function testProcessPrice()
170170
$this->assertSame($expectedResult, $this->removeWhitespaces($actualResult));
171171
}
172172

173-
public function testProcessCategoryIds()
173+
/**
174+
* @return array
175+
*/
176+
public function processCategoryIdsDataProvider()
177+
{
178+
return [
179+
['5', 'category_ids_index.category_id = 5'],
180+
[3, 'category_ids_index.category_id = 3'],
181+
["' and 1 = 0", 'category_ids_index.category_id = 0'],
182+
];
183+
}
184+
185+
/**
186+
* @param string|int $categoryId
187+
* @param string $expectedResult
188+
* @dataProvider processCategoryIdsDataProvider
189+
*/
190+
public function testProcessCategoryIds($categoryId, $expectedResult)
174191
{
175-
$expectedResult = 'category_ids_index.category_id = FilterValue';
176192
$isNegation = false;
177193
$query = 'SELECT category_ids FROM catalog_product_entity';
178194

@@ -182,7 +198,7 @@ public function testProcessCategoryIds()
182198

183199
$this->filter->expects($this->once())
184200
->method('getValue')
185-
->will($this->returnValue('FilterValue'));
201+
->will($this->returnValue($categoryId));
186202

187203
$this->config->expects($this->exactly(1))
188204
->method('getAttribute')
@@ -249,6 +265,7 @@ public function testProcessTermFilter($frontendInput, $fieldValue, $isNegation,
249265
->method('getValue')
250266
->willReturn($fieldValue);
251267

268+
$this->connection->expects($this->atLeastOnce())->method('quote')->willReturnArgument(0);
252269
$actualResult = $this->target->process($this->filter, $isNegation, 'This filter is not depends on used query');
253270
$this->assertSame($expected, $this->removeWhitespaces($actualResult));
254271
}

app/code/Magento/Newsletter/Test/Unit/Model/Queue/TransportBuilderTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ class TransportBuilderTest extends \Magento\Framework\Mail\Test\Unit\Template\Tr
2424
* @param int $templateType
2525
* @param string $messageType
2626
* @param string $bodyText
27+
* @param string $templateNamespace
2728
* @return void
2829
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
30+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
2931
*/
3032
public function testGetTransport(
3133
$templateType = TemplateTypesInterface::TYPE_HTML,
3234
$messageType = MessageInterface::TYPE_HTML,
33-
$bodyText = '<h1>Html message</h1>'
35+
$bodyText = '<h1>Html message</h1>',
36+
$templateNamespace = ''
3437
) {
3538
$filter = $this->getMock('Magento\Email\Model\Template\Filter', [], [], '', false);
3639
$data = [

app/code/Magento/Review/Controller/Product/Post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public function execute()
3737
$data = $this->getRequest()->getPostValue();
3838
$rating = $this->getRequest()->getParam('ratings', []);
3939
}
40-
4140
if (($product = $this->initProduct()) && !empty($data)) {
4241
/** @var \Magento\Review\Model\Review $review */
4342
$review = $this->reviewFactory->create()->setData($data);
43+
$review->unsetData('review_id');
4444

4545
$validate = $review->validate();
4646
if ($validate === true) {

app/code/Magento/Review/Test/Unit/Controller/Product/PostTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function setUp()
127127
'\Magento\Review\Model\Review',
128128
[
129129
'setData', 'validate', 'setEntityId', 'getEntityIdByCode', 'setEntityPkValue', 'setStatusId',
130-
'setCustomerId', 'setStoreId', 'setStores', 'save', 'getId', 'aggregate'
130+
'setCustomerId', 'setStoreId', 'setStores', 'save', 'getId', 'aggregate', 'unsetData'
131131
],
132132
[],
133133
'',
@@ -219,7 +219,10 @@ public function setUp()
219219
*/
220220
public function testExecute()
221221
{
222-
$ratingsData = ['ratings' => [1 => 1]];
222+
$reviewData = [
223+
'ratings' => [1 => 1],
224+
'review_id' => 2
225+
];
223226
$productId = 1;
224227
$customerId = 1;
225228
$storeId = 1;
@@ -230,7 +233,7 @@ public function testExecute()
230233
->willReturn(true);
231234
$this->reviewSession->expects($this->any())->method('getFormData')
232235
->with(true)
233-
->willReturn($ratingsData);
236+
->willReturn($reviewData);
234237
$this->request->expects($this->at(0))->method('getParam')
235238
->with('category', false)
236239
->willReturn(false);
@@ -260,7 +263,7 @@ public function testExecute()
260263
->with('product', $product)
261264
->willReturnSelf();
262265
$this->review->expects($this->once())->method('setData')
263-
->with($ratingsData)
266+
->with($reviewData)
264267
->willReturnSelf();
265268
$this->review->expects($this->once())->method('validate')
266269
->willReturn(true);
@@ -270,6 +273,7 @@ public function testExecute()
270273
$this->review->expects($this->once())->method('setEntityId')
271274
->with(1)
272275
->willReturnSelf();
276+
$this->review->expects($this->once())->method('unsetData')->with('review_id');
273277
$product->expects($this->exactly(2))
274278
->method('getId')
275279
->willReturn($productId);

app/code/Magento/SampleData/Console/Command/SampleDataDeployCommand.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ protected function configure()
7777
*/
7878
protected function execute(InputInterface $input, OutputInterface $output)
7979
{
80+
$this->updateMemoryLimit();
8081
$sampleDataPackages = $this->sampleDataDependency->getSampleDataPackages();
8182
if (!empty($sampleDataPackages)) {
8283
$baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
@@ -101,4 +102,39 @@ protected function execute(InputInterface $input, OutputInterface $output)
101102
$output->writeln('<info>' . 'There is no sample data for current set of modules.' . '</info>');
102103
}
103104
}
105+
106+
/**
107+
* @return void
108+
*/
109+
private function updateMemoryLimit()
110+
{
111+
if (function_exists('ini_set')) {
112+
@ini_set('display_errors', 1);
113+
$memoryLimit = trim(ini_get('memory_limit'));
114+
if ($memoryLimit != -1 && $this->getMemoryInBytes($memoryLimit) < 768 * 1024 * 1024) {
115+
@ini_set('memory_limit', '768M');
116+
}
117+
}
118+
}
119+
120+
/**
121+
* @param string $value
122+
* @return int
123+
*/
124+
private function getMemoryInBytes($value)
125+
{
126+
$unit = strtolower(substr($value, -1, 1));
127+
$value = (int) $value;
128+
switch($unit) {
129+
case 'g':
130+
$value *= 1024 * 1024 * 1024;
131+
break;
132+
case 'm':
133+
$value *= 1024 * 1024;
134+
break;
135+
case 'k':
136+
$value *= 1024;
137+
}
138+
return $value;
139+
}
104140
}

app/code/Magento/SampleData/Test/Unit/Console/Command/SampleDataDeployCommandTest.php

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,92 @@
1111

1212
class SampleDataDeployCommandTest extends \PHPUnit_Framework_TestCase
1313
{
14-
public function testExecute()
14+
/**
15+
* @param array $sampleDataPackages
16+
* @param int $appRunResult - int 0 if everything went fine, or an error code
17+
* @param string $expectedMsg
18+
* @return void
19+
*
20+
* @dataProvider processDataProvider
21+
*/
22+
public function testExecute(array $sampleDataPackages, $appRunResult, $expectedMsg)
1523
{
16-
$this->markTestSkipped('MAGETWO-43636: This test should be fixed by NORD team');
1724
$directoryRead = $this->getMock('\Magento\Framework\Filesystem\Directory\ReadInterface', [], [], '', false);
18-
$directoryRead->expects($this->atLeastOnce())->method('getAbsolutePath')->willReturn('/path/to/composer.json');
25+
$directoryRead->expects($this->any())->method('getAbsolutePath')->willReturn('/path/to/composer.json');
1926

2027
$filesystem = $this->getMock('Magento\Framework\Filesystem', [], [], '', false);
21-
$filesystem->expects($this->atLeastOnce())->method('getDirectoryRead')->with(DirectoryList::ROOT)
28+
$filesystem->expects($this->any())->method('getDirectoryRead')->with(DirectoryList::ROOT)
2229
->willReturn($directoryRead);
2330

2431
$sampleDataDependency = $this->getMock('Magento\SampleData\Model\Dependency', [], [], '', false);
25-
$sampleDataDependency->expects($this->atLeastOnce())->method('getSampleDataPackages')->willReturn(
26-
[
27-
'magento/module-cms-sample-data' => '1.0.0-beta'
28-
]
29-
);
32+
$sampleDataDependency
33+
->expects($this->any())
34+
->method('getSampleDataPackages')
35+
->willReturn($sampleDataPackages);
3036

31-
$arrayInput = $this->getMock('Symfony\Component\Console\Input\ArrayInput', [], [], '', false);
37+
$commandInput = $this->getMock('Symfony\Component\Console\Input\ArrayInput', [], [], '', false);
3238

3339
$arrayInputFactory = $this
3440
->getMock('Symfony\Component\Console\Input\ArrayInputFactory', ['create'], [], '', false);
41+
42+
array_walk($sampleDataPackages, function (&$v, $k) {
43+
$v = "$k:$v";
44+
});
45+
46+
$packages = array_values($sampleDataPackages);
47+
3548
$requireArgs = [
36-
'command' => 'require',
49+
'command' => 'require',
3750
'--working-dir' => '/path/to/composer.json',
38-
'--no-interaction' => 1,
3951
'--no-progress' => 1,
40-
'packages' => ['magento/module-cms-sample-data:1.0.0-beta']
52+
'packages' => $packages,
4153
];
42-
$arrayInputFactory->expects($this->atLeastOnce())
54+
$arrayInputFactory->expects($this->any())
4355
->method('create')
4456
->with(['parameters' => $requireArgs])
45-
->willReturn($arrayInput);
57+
->willReturn($commandInput);
58+
4659
$application = $this->getMock('Composer\Console\Application', [], [], '', false);
47-
$application->expects($this->atLeastOnce())->method('run')->with($arrayInput, $this->anything())
48-
->willReturnCallback(function ($input, $output) {
49-
$input->getFirstArgument();
50-
$output->writeln('./composer.json has been updated');
51-
});
60+
$application->expects($this->any())->method('run')
61+
->with($commandInput, $this->anything())
62+
->willReturn($appRunResult);
5263

5364
$applicationFactory = $this->getMock('Composer\Console\ApplicationFactory', ['create'], [], '', false);
54-
$applicationFactory->expects($this->atLeastOnce())->method('create')->willReturn($application);
65+
$applicationFactory->expects($this->any())->method('create')->willReturn($application);
5566

5667
$commandTester = new CommandTester(
5768
new SampleDataDeployCommand($filesystem, $sampleDataDependency, $arrayInputFactory, $applicationFactory)
5869
);
5970
$commandTester->execute([]);
6071

61-
$expectedMsg = './composer.json has been updated' . PHP_EOL;
62-
6372
$this->assertEquals($expectedMsg, $commandTester->getDisplay());
6473
}
74+
75+
/**
76+
* @return array
77+
*/
78+
public function processDataProvider()
79+
{
80+
return [
81+
[
82+
'sampleDataPackages' => [],
83+
'appRunResult' => 1,
84+
'expectedMsg' => 'There is no sample data for current set of modules.' . PHP_EOL,
85+
],
86+
[
87+
'sampleDataPackages' => [
88+
'magento/module-cms-sample-data' => '1.0.0-beta',
89+
],
90+
'appRunResult' => 1,
91+
'expectedMsg' => 'There is an error during sample data deployment.' . PHP_EOL,
92+
],
93+
[
94+
'sampleDataPackages' => [
95+
'magento/module-cms-sample-data' => '1.0.0-beta',
96+
],
97+
'appRunResult' => 0,
98+
'expectedMsg' => '',
99+
],
100+
];
101+
}
65102
}

0 commit comments

Comments
 (0)