Skip to content

Commit

Permalink
Merge pull request #335 from magento-east/MAGETWO-57726
Browse files Browse the repository at this point in the history
[EAST] MAGETWO-57726 [GitHub] Exception is created but not thrown #6320
  • Loading branch information
Joan He authored Sep 6, 2016
2 parents b5b3bbf + c4dfc42 commit 2cee680
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/code/Magento/Reports/Block/Product/AbstractProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ protected function _getProductsToSkip()

/**
* Public method for retrieve Product Index model
*
* @throws \Magento\Framework\Exception\LocalizedException
* @return \Magento\Reports\Model\Product\Index\AbstractIndex
*/
public function getModel()
{
try {
$model = $this->_indexFactory->get($this->_indexType);
} catch (\InvalidArgumentException $e) {
new \Magento\Framework\Exception\LocalizedException(__('Index type is not valid'));
throw new \Magento\Framework\Exception\LocalizedException(__('Index type is not valid'));
}

return $model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,13 @@ public function joinInventoryItem($fields = [])
* Add filter by product type(s)
*
* @param array|string $typeFilter
* @throws \Magento\Framework\Exception\LocalizedException
* @return $this
*/
public function filterByProductType($typeFilter)
{
if (!is_string($typeFilter) && !is_array($typeFilter)) {
new \Magento\Framework\Exception\LocalizedException(__('The product type filter specified is incorrect.'));
throw new \Magento\Framework\Exception\LocalizedException(__('The product type filter specified is incorrect.'));
}
$this->addAttributeToFilter('type_id', $typeFilter);
return $this;
Expand Down
68 changes: 68 additions & 0 deletions app/code/Magento/Reports/Test/Unit/Block/Product/ComparedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Reports\Test\Unit\Block\Product;

use \Magento\Reports\Block\Product\Compared;
use \Magento\Reports\Model\Product\Index\Factory;

class ComparedTest extends \PHPUnit_Framework_TestCase
{

/**
* @var \Magento\Reports\Block\Product\Compared;
*/
private $sut;

/**
* @var Factory|\PHPUnit_Framework_MockObject_MockObject
*/
private $factoryMock;

protected function setUp()
{
$contextMock = $this->getMockBuilder(\Magento\Catalog\Block\Product\Context::class)
->disableOriginalConstructor()
->getMock();

$visibilityMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Visibility::class)
->disableOriginalConstructor()
->getMock();

$this->factoryMock = $this->getMockBuilder(Factory::class)
->disableOriginalConstructor()
->setMethods(['get'])
->getMock();

$this->sut = new Compared($contextMock, $visibilityMock, $this->factoryMock);
}

/**
* Assert that getModel method throws LocalizedException
*
* @expectedException \Magento\Framework\Exception\LocalizedException
*/
public function testGetModelException()
{
$this->factoryMock->expects($this->once())->method('get')->willThrowException(new \InvalidArgumentException);

$this->sut->getModel();
}

/**
* Assert that getModel method returns AbstractIndex
*/
public function testGetModel()
{
$indexMock = $this->getMockBuilder(\Magento\Reports\Model\Product\Index\AbstractIndex::class)
->disableOriginalConstructor()
->getMock();

$this->factoryMock->expects($this->once())->method('get')->willReturn($indexMock);

$this->assertSame($indexMock, $this->sut->getModel());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Reports\Model\ResourceModel\Product\Lowstock;

/**
* Class CollectionTest
*/
class CollectionTest extends \PHPUnit_Framework_TestCase
{

/**
* @var \Magento\Reports\Model\ResourceModel\Product\Lowstock\Collection
*/
private $collection;

protected function setUp()
{
/**
* @var \Magento\Reports\Model\ResourceModel\Product\Lowstock\Collection
*/
$this->collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Reports\Model\ResourceModel\Product\Lowstock\Collection::class
);
}

/**
* Assert that filterByProductType method throws LocalizedException if not String or Array is passed to it
*
* @expectedException \Magento\Framework\Exception\LocalizedException
*/
public function testFilterByProductTypeException()
{
$this->collection->filterByProductType(100);
}

/**
* Assert that String argument passed to filterByProductType method is correctly passed to attribute adder
*
*/
public function testFilterByProductTypeString()
{
$this->collection->filterByProductType('simple');
$whereParts = $this->collection->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE);
$this->assertContains('simple', $whereParts[0]);
}

/**
* Assert that Array argument passed to filterByProductType method is correctly passed to attribute adder
*
*/
public function testFilterByProductTypeArray()
{
$this->collection->filterByProductType(['simple', 'configurable']);
$whereParts = $this->collection->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE);

$this->assertThat(
$whereParts[0],
$this->logicalAnd(
$this->stringContains('simple'),
$this->stringContains('configurable')
)
);
}
}

0 comments on commit 2cee680

Please sign in to comment.