Skip to content

Commit 2cee680

Browse files
author
Joan He
authored
Merge pull request #335 from magento-east/MAGETWO-57726
[EAST] MAGETWO-57726 [GitHub] Exception is created but not thrown #6320
2 parents b5b3bbf + c4dfc42 commit 2cee680

File tree

4 files changed

+139
-3
lines changed

4 files changed

+139
-3
lines changed

app/code/Magento/Reports/Block/Product/AbstractProduct.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ protected function _getProductsToSkip()
8282

8383
/**
8484
* Public method for retrieve Product Index model
85-
*
85+
* @throws \Magento\Framework\Exception\LocalizedException
8686
* @return \Magento\Reports\Model\Product\Index\AbstractIndex
8787
*/
8888
public function getModel()
8989
{
9090
try {
9191
$model = $this->_indexFactory->get($this->_indexType);
9292
} catch (\InvalidArgumentException $e) {
93-
new \Magento\Framework\Exception\LocalizedException(__('Index type is not valid'));
93+
throw new \Magento\Framework\Exception\LocalizedException(__('Index type is not valid'));
9494
}
9595

9696
return $model;

app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,13 @@ public function joinInventoryItem($fields = [])
244244
* Add filter by product type(s)
245245
*
246246
* @param array|string $typeFilter
247+
* @throws \Magento\Framework\Exception\LocalizedException
247248
* @return $this
248249
*/
249250
public function filterByProductType($typeFilter)
250251
{
251252
if (!is_string($typeFilter) && !is_array($typeFilter)) {
252-
new \Magento\Framework\Exception\LocalizedException(__('The product type filter specified is incorrect.'));
253+
throw new \Magento\Framework\Exception\LocalizedException(__('The product type filter specified is incorrect.'));
253254
}
254255
$this->addAttributeToFilter('type_id', $typeFilter);
255256
return $this;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Reports\Test\Unit\Block\Product;
8+
9+
use \Magento\Reports\Block\Product\Compared;
10+
use \Magento\Reports\Model\Product\Index\Factory;
11+
12+
class ComparedTest extends \PHPUnit_Framework_TestCase
13+
{
14+
15+
/**
16+
* @var \Magento\Reports\Block\Product\Compared;
17+
*/
18+
private $sut;
19+
20+
/**
21+
* @var Factory|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $factoryMock;
24+
25+
protected function setUp()
26+
{
27+
$contextMock = $this->getMockBuilder(\Magento\Catalog\Block\Product\Context::class)
28+
->disableOriginalConstructor()
29+
->getMock();
30+
31+
$visibilityMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Visibility::class)
32+
->disableOriginalConstructor()
33+
->getMock();
34+
35+
$this->factoryMock = $this->getMockBuilder(Factory::class)
36+
->disableOriginalConstructor()
37+
->setMethods(['get'])
38+
->getMock();
39+
40+
$this->sut = new Compared($contextMock, $visibilityMock, $this->factoryMock);
41+
}
42+
43+
/**
44+
* Assert that getModel method throws LocalizedException
45+
*
46+
* @expectedException \Magento\Framework\Exception\LocalizedException
47+
*/
48+
public function testGetModelException()
49+
{
50+
$this->factoryMock->expects($this->once())->method('get')->willThrowException(new \InvalidArgumentException);
51+
52+
$this->sut->getModel();
53+
}
54+
55+
/**
56+
* Assert that getModel method returns AbstractIndex
57+
*/
58+
public function testGetModel()
59+
{
60+
$indexMock = $this->getMockBuilder(\Magento\Reports\Model\Product\Index\AbstractIndex::class)
61+
->disableOriginalConstructor()
62+
->getMock();
63+
64+
$this->factoryMock->expects($this->once())->method('get')->willReturn($indexMock);
65+
66+
$this->assertSame($indexMock, $this->sut->getModel());
67+
}
68+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Reports\Model\ResourceModel\Product\Lowstock;
7+
8+
/**
9+
* Class CollectionTest
10+
*/
11+
class CollectionTest extends \PHPUnit_Framework_TestCase
12+
{
13+
14+
/**
15+
* @var \Magento\Reports\Model\ResourceModel\Product\Lowstock\Collection
16+
*/
17+
private $collection;
18+
19+
protected function setUp()
20+
{
21+
/**
22+
* @var \Magento\Reports\Model\ResourceModel\Product\Lowstock\Collection
23+
*/
24+
$this->collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
25+
\Magento\Reports\Model\ResourceModel\Product\Lowstock\Collection::class
26+
);
27+
}
28+
29+
/**
30+
* Assert that filterByProductType method throws LocalizedException if not String or Array is passed to it
31+
*
32+
* @expectedException \Magento\Framework\Exception\LocalizedException
33+
*/
34+
public function testFilterByProductTypeException()
35+
{
36+
$this->collection->filterByProductType(100);
37+
}
38+
39+
/**
40+
* Assert that String argument passed to filterByProductType method is correctly passed to attribute adder
41+
*
42+
*/
43+
public function testFilterByProductTypeString()
44+
{
45+
$this->collection->filterByProductType('simple');
46+
$whereParts = $this->collection->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE);
47+
$this->assertContains('simple', $whereParts[0]);
48+
}
49+
50+
/**
51+
* Assert that Array argument passed to filterByProductType method is correctly passed to attribute adder
52+
*
53+
*/
54+
public function testFilterByProductTypeArray()
55+
{
56+
$this->collection->filterByProductType(['simple', 'configurable']);
57+
$whereParts = $this->collection->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE);
58+
59+
$this->assertThat(
60+
$whereParts[0],
61+
$this->logicalAnd(
62+
$this->stringContains('simple'),
63+
$this->stringContains('configurable')
64+
)
65+
);
66+
}
67+
}

0 commit comments

Comments
 (0)