Skip to content

Commit

Permalink
Merge pull request #252 from magento-nord/develop
Browse files Browse the repository at this point in the history
[NORD] Sample Data tests coverage. Bugfixes
  • Loading branch information
Onischenko, Yaroslav(yonischenko) committed Dec 22, 2015
2 parents 9b08b0b + f4a097a commit 33b13b6
Show file tree
Hide file tree
Showing 14 changed files with 394 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private function processQueryWithField(FilterInterface $filter, $isNegation, $qu
$query
);
} elseif ($filter->getField() === 'category_ids') {
return 'category_ids_index.category_id = ' . $filter->getValue();
return 'category_ids_index.category_id = ' . (int) $filter->getValue();
} elseif ($attribute->isStatic()) {
$alias = $this->tableMapper->getMappingAlias($filter);
$resultQuery = str_replace(
Expand Down Expand Up @@ -194,10 +194,10 @@ private function processTermSelect(FilterInterface $filter, $isNegation)
$value = sprintf(
'%s IN (%s)',
($isNegation ? 'NOT' : ''),
implode(',', $filter->getValue())
implode(',', array_map([$this->connection, 'quote'], $filter->getValue()))
);
} else {
$value = ($isNegation ? '!' : '') . '= ' . $filter->getValue();
$value = ($isNegation ? '!' : '') . '= ' . $this->connection->quote($filter->getValue());
}
$resultQuery = sprintf(
'%1$s.value %2$s',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function setUp()
->getMock();
$this->connection = $this->getMockBuilder('\Magento\Framework\DB\Adapter\AdapterInterface')
->disableOriginalConstructor()
->setMethods(['select', 'getIfNullSql'])
->setMethods(['select', 'getIfNullSql', 'quote'])
->getMockForAbstractClass();
$this->select = $this->getMockBuilder('\Magento\Framework\DB\Select')
->disableOriginalConstructor()
Expand Down Expand Up @@ -170,9 +170,25 @@ public function testProcessPrice()
$this->assertSame($expectedResult, $this->removeWhitespaces($actualResult));
}

public function testProcessCategoryIds()
/**
* @return array
*/
public function processCategoryIdsDataProvider()
{
return [
['5', 'category_ids_index.category_id = 5'],
[3, 'category_ids_index.category_id = 3'],
["' and 1 = 0", 'category_ids_index.category_id = 0'],
];
}

/**
* @param string|int $categoryId
* @param string $expectedResult
* @dataProvider processCategoryIdsDataProvider
*/
public function testProcessCategoryIds($categoryId, $expectedResult)
{
$expectedResult = 'category_ids_index.category_id = FilterValue';
$isNegation = false;
$query = 'SELECT category_ids FROM catalog_product_entity';

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

$this->filter->expects($this->once())
->method('getValue')
->will($this->returnValue('FilterValue'));
->will($this->returnValue($categoryId));

$this->config->expects($this->exactly(1))
->method('getAttribute')
Expand Down Expand Up @@ -249,6 +265,7 @@ public function testProcessTermFilter($frontendInput, $fieldValue, $isNegation,
->method('getValue')
->willReturn($fieldValue);

$this->connection->expects($this->atLeastOnce())->method('quote')->willReturnArgument(0);
$actualResult = $this->target->process($this->filter, $isNegation, 'This filter is not depends on used query');
$this->assertSame($expected, $this->removeWhitespaces($actualResult));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ class TransportBuilderTest extends \Magento\Framework\Mail\Test\Unit\Template\Tr
* @param int $templateType
* @param string $messageType
* @param string $bodyText
* @param string $templateNamespace
* @return void
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function testGetTransport(
$templateType = TemplateTypesInterface::TYPE_HTML,
$messageType = MessageInterface::TYPE_HTML,
$bodyText = '<h1>Html message</h1>'
$bodyText = '<h1>Html message</h1>',
$templateNamespace = ''
) {
$filter = $this->getMock('Magento\Email\Model\Template\Filter', [], [], '', false);
$data = [
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Review/Controller/Product/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public function execute()
$data = $this->getRequest()->getPostValue();
$rating = $this->getRequest()->getParam('ratings', []);
}

if (($product = $this->initProduct()) && !empty($data)) {
/** @var \Magento\Review\Model\Review $review */
$review = $this->reviewFactory->create()->setData($data);
$review->unsetData('review_id');

$validate = $review->validate();
if ($validate === true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function setUp()
'\Magento\Review\Model\Review',
[
'setData', 'validate', 'setEntityId', 'getEntityIdByCode', 'setEntityPkValue', 'setStatusId',
'setCustomerId', 'setStoreId', 'setStores', 'save', 'getId', 'aggregate'
'setCustomerId', 'setStoreId', 'setStores', 'save', 'getId', 'aggregate', 'unsetData'
],
[],
'',
Expand Down Expand Up @@ -219,7 +219,10 @@ public function setUp()
*/
public function testExecute()
{
$ratingsData = ['ratings' => [1 => 1]];
$reviewData = [
'ratings' => [1 => 1],
'review_id' => 2
];
$productId = 1;
$customerId = 1;
$storeId = 1;
Expand All @@ -230,7 +233,7 @@ public function testExecute()
->willReturn(true);
$this->reviewSession->expects($this->any())->method('getFormData')
->with(true)
->willReturn($ratingsData);
->willReturn($reviewData);
$this->request->expects($this->at(0))->method('getParam')
->with('category', false)
->willReturn(false);
Expand Down Expand Up @@ -260,7 +263,7 @@ public function testExecute()
->with('product', $product)
->willReturnSelf();
$this->review->expects($this->once())->method('setData')
->with($ratingsData)
->with($reviewData)
->willReturnSelf();
$this->review->expects($this->once())->method('validate')
->willReturn(true);
Expand All @@ -270,6 +273,7 @@ public function testExecute()
$this->review->expects($this->once())->method('setEntityId')
->with(1)
->willReturnSelf();
$this->review->expects($this->once())->method('unsetData')->with('review_id');
$product->expects($this->exactly(2))
->method('getId')
->willReturn($productId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->updateMemoryLimit();
$sampleDataPackages = $this->sampleDataDependency->getSampleDataPackages();
if (!empty($sampleDataPackages)) {
$baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
Expand All @@ -101,4 +102,39 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln('<info>' . 'There is no sample data for current set of modules.' . '</info>');
}
}

/**
* @return void
*/
private function updateMemoryLimit()
{
if (function_exists('ini_set')) {
@ini_set('display_errors', 1);
$memoryLimit = trim(ini_get('memory_limit'));
if ($memoryLimit != -1 && $this->getMemoryInBytes($memoryLimit) < 768 * 1024 * 1024) {
@ini_set('memory_limit', '768M');
}
}
}

/**
* @param string $value
* @return int
*/
private function getMemoryInBytes($value)
{
$unit = strtolower(substr($value, -1, 1));
$value = (int) $value;
switch($unit) {
case 'g':
$value *= 1024 * 1024 * 1024;
break;
case 'm':
$value *= 1024 * 1024;
break;
case 'k':
$value *= 1024;
}
return $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,92 @@

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

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

$sampleDataDependency = $this->getMock('Magento\SampleData\Model\Dependency', [], [], '', false);
$sampleDataDependency->expects($this->atLeastOnce())->method('getSampleDataPackages')->willReturn(
[
'magento/module-cms-sample-data' => '1.0.0-beta'
]
);
$sampleDataDependency
->expects($this->any())
->method('getSampleDataPackages')
->willReturn($sampleDataPackages);

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

$arrayInputFactory = $this
->getMock('Symfony\Component\Console\Input\ArrayInputFactory', ['create'], [], '', false);

array_walk($sampleDataPackages, function (&$v, $k) {
$v = "$k:$v";
});

$packages = array_values($sampleDataPackages);

$requireArgs = [
'command' => 'require',
'command' => 'require',
'--working-dir' => '/path/to/composer.json',
'--no-interaction' => 1,
'--no-progress' => 1,
'packages' => ['magento/module-cms-sample-data:1.0.0-beta']
'packages' => $packages,
];
$arrayInputFactory->expects($this->atLeastOnce())
$arrayInputFactory->expects($this->any())
->method('create')
->with(['parameters' => $requireArgs])
->willReturn($arrayInput);
->willReturn($commandInput);

$application = $this->getMock('Composer\Console\Application', [], [], '', false);
$application->expects($this->atLeastOnce())->method('run')->with($arrayInput, $this->anything())
->willReturnCallback(function ($input, $output) {
$input->getFirstArgument();
$output->writeln('./composer.json has been updated');
});
$application->expects($this->any())->method('run')
->with($commandInput, $this->anything())
->willReturn($appRunResult);

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

$commandTester = new CommandTester(
new SampleDataDeployCommand($filesystem, $sampleDataDependency, $arrayInputFactory, $applicationFactory)
);
$commandTester->execute([]);

$expectedMsg = './composer.json has been updated' . PHP_EOL;

$this->assertEquals($expectedMsg, $commandTester->getDisplay());
}

/**
* @return array
*/
public function processDataProvider()
{
return [
[
'sampleDataPackages' => [],
'appRunResult' => 1,
'expectedMsg' => 'There is no sample data for current set of modules.' . PHP_EOL,
],
[
'sampleDataPackages' => [
'magento/module-cms-sample-data' => '1.0.0-beta',
],
'appRunResult' => 1,
'expectedMsg' => 'There is an error during sample data deployment.' . PHP_EOL,
],
[
'sampleDataPackages' => [
'magento/module-cms-sample-data' => '1.0.0-beta',
],
'appRunResult' => 0,
'expectedMsg' => '',
],
];
}
}
Loading

0 comments on commit 33b13b6

Please sign in to comment.