From 60add52752126b002f1598e3f979f0196480d40b Mon Sep 17 00:00:00 2001 From: Oleksandr Dubovyk Date: Mon, 29 Aug 2016 12:42:24 +0300 Subject: [PATCH 1/8] MAGETWO-57726: Exception is created but not thrown #6320 --- app/code/Magento/Reports/Block/Product/AbstractProduct.php | 2 +- .../Reports/Model/ResourceModel/Product/Lowstock/Collection.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Reports/Block/Product/AbstractProduct.php b/app/code/Magento/Reports/Block/Product/AbstractProduct.php index 9a142b2f98217..1bd340cd71847 100644 --- a/app/code/Magento/Reports/Block/Product/AbstractProduct.php +++ b/app/code/Magento/Reports/Block/Product/AbstractProduct.php @@ -90,7 +90,7 @@ 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; diff --git a/app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php b/app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php index c5fd7b09fa252..f3a142a12e596 100644 --- a/app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php +++ b/app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php @@ -249,7 +249,7 @@ public function joinInventoryItem($fields = []) 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; From 873a1231b7ae64beda1b46e6ff06ae6fd1c3d315 Mon Sep 17 00:00:00 2001 From: Oleksandr Dubovyk Date: Mon, 29 Aug 2016 14:05:29 +0300 Subject: [PATCH 2/8] MAGETWO-57726: Exception is created but not thrown #6320 - Updated DocBlock --- app/code/Magento/Reports/Block/Product/AbstractProduct.php | 2 +- .../Reports/Model/ResourceModel/Product/Lowstock/Collection.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Reports/Block/Product/AbstractProduct.php b/app/code/Magento/Reports/Block/Product/AbstractProduct.php index 1bd340cd71847..1fdcc297b28e9 100644 --- a/app/code/Magento/Reports/Block/Product/AbstractProduct.php +++ b/app/code/Magento/Reports/Block/Product/AbstractProduct.php @@ -82,7 +82,7 @@ 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() diff --git a/app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php b/app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php index f3a142a12e596..9c8afd21cd6ef 100644 --- a/app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php +++ b/app/code/Magento/Reports/Model/ResourceModel/Product/Lowstock/Collection.php @@ -244,6 +244,7 @@ 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) From fc20d3e5a12105d03479b42d6f42c0f0632efac8 Mon Sep 17 00:00:00 2001 From: Oleksandr Dubovyk Date: Thu, 1 Sep 2016 11:11:36 +0300 Subject: [PATCH 3/8] MAGETWO-57726: [GitHub] Exception is created but not thrown #6320 - Added unit and integration tests --- .../Test/Unit/Block/Product/ComparedTest.php | 66 ++++++++++++++++++ .../Product/Lowstock/CollectionTest.php | 68 +++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 app/code/Magento/Reports/Test/Unit/Block/Product/ComparedTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php diff --git a/app/code/Magento/Reports/Test/Unit/Block/Product/ComparedTest.php b/app/code/Magento/Reports/Test/Unit/Block/Product/ComparedTest.php new file mode 100644 index 0000000000000..e8913e4adb38f --- /dev/null +++ b/app/code/Magento/Reports/Test/Unit/Block/Product/ComparedTest.php @@ -0,0 +1,66 @@ +getMockBuilder(\Magento\Catalog\Block\Product\Context::class) + ->disableOriginalConstructor() + ->getMock(); + $visibilityMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Visibility::class) + ->disableOriginalConstructor() + ->getMock(); + $this->factoryMock = $this->getMockBuilder(\Magento\Reports\Model\Product\Index\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()); + } +} diff --git a/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php b/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php new file mode 100644 index 0000000000000..aa8d6c2bd65bd --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php @@ -0,0 +1,68 @@ +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 + * + * @expectedException \Magento\Framework\Exception\LocalizedException + */ + public function testFilterByProductTypeString() + { + $this->collection->filterByProductType('simple'); + $whereParts = $this->collection->getSelect()->getPart(\Zend_Db_Select::WHERE); + $this->assertContains('simple', $whereParts[0]); + } + + /** + * Assert that Array argument passed to filterByProductType method is correctly passed to attribute adder + * + * @expectedException \Magento\Framework\Exception\LocalizedException + */ + public function testFilterByProductTypeArray() + { + $this->collection->filterByProductType(['simple', 'configurable']); + $whereParts = $this->collection->getSelect()->getPart(\Zend_Db_Select::WHERE); + + $this->assertThat( + $whereParts[0], + $this->logicalAnd( + $this->stringContains('simple'), + $this->stringContains('configurable') + ) + ); + } +} From 2e5baa12aff6603efe1f9d5b9e15d067ee42b5cd Mon Sep 17 00:00:00 2001 From: Oleksandr Dubovyk Date: Thu, 1 Sep 2016 12:31:57 +0300 Subject: [PATCH 4/8] MAGETWO-57726: [GitHub] Exception is created but not thrown #6320 - Refactor --- .../Reports/Test/Unit/Block/Product/ComparedTest.php | 8 +++++--- .../ResourceModel/Product/Lowstock/CollectionTest.php | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Reports/Test/Unit/Block/Product/ComparedTest.php b/app/code/Magento/Reports/Test/Unit/Block/Product/ComparedTest.php index e8913e4adb38f..f7030b8a731e7 100644 --- a/app/code/Magento/Reports/Test/Unit/Block/Product/ComparedTest.php +++ b/app/code/Magento/Reports/Test/Unit/Block/Product/ComparedTest.php @@ -27,10 +27,12 @@ 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(\Magento\Reports\Model\Product\Index\Factory::class) + + $this->factoryMock = $this->getMockBuilder(Factory::class) ->disableOriginalConstructor() ->setMethods(['get']) ->getMock(); @@ -56,8 +58,8 @@ public function testGetModelException() public function testGetModel() { $indexMock = $this->getMockBuilder(\Magento\Reports\Model\Product\Index\AbstractIndex::class) - ->disableOriginalConstructor() - ->getMock(); + ->disableOriginalConstructor() + ->getMock(); $this->factoryMock->expects($this->once())->method('get')->willReturn($indexMock); diff --git a/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php b/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php index aa8d6c2bd65bd..65a0512b9cf55 100644 --- a/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php +++ b/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php @@ -43,7 +43,7 @@ public function testFilterByProductTypeException() public function testFilterByProductTypeString() { $this->collection->filterByProductType('simple'); - $whereParts = $this->collection->getSelect()->getPart(\Zend_Db_Select::WHERE); + $whereParts = $this->collection->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE); $this->assertContains('simple', $whereParts[0]); } @@ -55,7 +55,7 @@ public function testFilterByProductTypeString() public function testFilterByProductTypeArray() { $this->collection->filterByProductType(['simple', 'configurable']); - $whereParts = $this->collection->getSelect()->getPart(\Zend_Db_Select::WHERE); + $whereParts = $this->collection->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE); $this->assertThat( $whereParts[0], From 9367990e5cf5f59cb376193e56f3707846acf6ba Mon Sep 17 00:00:00 2001 From: Oleksandr Dubovyk Date: Thu, 1 Sep 2016 13:46:41 +0300 Subject: [PATCH 5/8] MAGETWO-57726: [GitHub] Exception is created but not thrown #6320 - Fixed namespace --- .../Model/ResourceModel/Product/Lowstock/CollectionTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php b/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php index 65a0512b9cf55..a44f3a3d9eb65 100644 --- a/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php +++ b/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php @@ -4,8 +4,7 @@ * See COPYING.txt for license details. */ -namespace Magento\Reports\Model\ResourceModel\Product\Lowstock\Collection; - +namespace \Magento\Reports\Model\ResourceModel\Product\Lowstock\Collection; class CollectionTest extends \PHPUnit_Framework_TestCase { From 8af37f7078852d84bc55665d7ae5970e4faa9c3f Mon Sep 17 00:00:00 2001 From: Oleksandr Dubovyk Date: Thu, 1 Sep 2016 13:59:06 +0300 Subject: [PATCH 6/8] MAGETWO-57726: [GitHub] Exception is created but not thrown #6320 --- .../Model/ResourceModel/Product/Lowstock/CollectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php b/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php index a44f3a3d9eb65..bd1f460db83c3 100644 --- a/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php +++ b/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php @@ -4,7 +4,7 @@ * See COPYING.txt for license details. */ -namespace \Magento\Reports\Model\ResourceModel\Product\Lowstock\Collection; +namespace \Magento\Reports\Model\ResourceModel\Product\Lowstock; class CollectionTest extends \PHPUnit_Framework_TestCase { From 22d988716bf9f0b8bcc66f24d3260eec55a826a9 Mon Sep 17 00:00:00 2001 From: Oleksandr Dubovyk Date: Thu, 1 Sep 2016 15:58:16 +0300 Subject: [PATCH 7/8] MAGETWO-57726: [GitHub] Exception is created but not thrown #6320 --- .../Model/ResourceModel/Product/Lowstock/CollectionTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php b/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php index bd1f460db83c3..ab453be83227c 100644 --- a/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php +++ b/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php @@ -3,9 +3,11 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ +namespace Magento\Reports\Model\ResourceModel\Product\Lowstock; -namespace \Magento\Reports\Model\ResourceModel\Product\Lowstock; - +/** + * Class CollectionTest + */ class CollectionTest extends \PHPUnit_Framework_TestCase { From 10299bc2aed2bc23f6fb589633dc5821daad5792 Mon Sep 17 00:00:00 2001 From: Oleksandr Dubovyk Date: Thu, 1 Sep 2016 16:48:07 +0300 Subject: [PATCH 8/8] MAGETWO-57726: [GitHub] Exception is created but not thrown #6320 --- .../Model/ResourceModel/Product/Lowstock/CollectionTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php b/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php index ab453be83227c..bdd0dfac712a9 100644 --- a/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php +++ b/dev/tests/integration/testsuite/Magento/Reports/Model/ResourceModel/Product/Lowstock/CollectionTest.php @@ -39,7 +39,6 @@ public function testFilterByProductTypeException() /** * Assert that String argument passed to filterByProductType method is correctly passed to attribute adder * - * @expectedException \Magento\Framework\Exception\LocalizedException */ public function testFilterByProductTypeString() { @@ -51,7 +50,6 @@ public function testFilterByProductTypeString() /** * Assert that Array argument passed to filterByProductType method is correctly passed to attribute adder * - * @expectedException \Magento\Framework\Exception\LocalizedException */ public function testFilterByProductTypeArray() {