From 0a193ae2086c6e28a9740ff7404d251aa6ab09ff Mon Sep 17 00:00:00 2001 From: Nazarn96 Date: Fri, 18 Oct 2019 15:58:23 +0300 Subject: [PATCH 1/3] Unit test coverage request --- .../Ui/Component/Listing/Filter/ColorTest.php | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php diff --git a/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php b/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php new file mode 100644 index 000000000000..e1f21138bfa9 --- /dev/null +++ b/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php @@ -0,0 +1,115 @@ +contextInterface = $this->createMock(ContextInterface::class); + $this->uiComponentFactory = $this->createMock(UiComponentFactory::class); + $this->filterBuilder = $this->createMock(FilterBuilder::class); + $this->filterModifier = $this->createMock(FilterModifier::class); + $this->colorModesProvider = $this->createMock(ColorModesProvider::class); + + $this->color = new Color( + $this->contextInterface, + $this->uiComponentFactory, + $this->filterBuilder, + $this->filterModifier, + $this->colorModesProvider, + [], + ['name' => 'name_one', 'config/colorFormat' => 'format_one'] + ); + + $reflection = new \ReflectionClass($this->color); + $property = $reflection->getProperty('filterData'); + $property->setAccessible(true); + $property->setValue($this->color, ['name_one' => 'value_one']); + } + + /** + * Prepare test + */ + public function testPrepare() + { + $componentInterface = $this->createMock(\Magento\Framework\View\Element\UiComponentInterface::class); + $this->uiComponentFactory->expects($this->once())->method('create')->willReturn($componentInterface); + $componentInterface->expects($this->once())->method('prepare')->willReturn(null); + $contextInterface = $this->createMock(\Magento\Framework\View\Element\UiComponent\ContextInterface::class); + $componentInterface->expects($this->once())->method('getContext')->willReturn($contextInterface); + $contextInterface->expects($this->once())->method('getNamespace')->willReturn('name_space'); + $componentInterface->expects($this->exactly(2))->method('getData')->willReturn(['data']); + $this->filterBuilder->expects($this->once())->method('setConditionType')->willReturnSelf(); + $this->filterBuilder->expects($this->once())->method('setField')->willReturnSelf(); + $this->filterBuilder->expects($this->once())->method('setValue')->willReturnSelf(); + $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class); + $this->filterBuilder->expects($this->once())->method('create')->willReturn($filterMock); + $this->colorModesProvider->expects($this->once()) + ->method('getModes') + ->willReturn(['full' => ['preferredFormat' => 'one']]); + $dataProvider = $this->createMock( + \Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class + ); + $this->contextInterface->expects($this->exactly(2)) + ->method('getDataProvider') + ->willReturn($dataProvider); + $dataProvider->expects($this->once())->method('addFilter'); + $processorMock = $this->createMock(\Magento\Framework\View\Element\UiComponent\Processor::class); + $this->contextInterface->expects($this->exactly(2))->method('getProcessor')->willReturn($processorMock); + $processorMock->expects($this->exactly(1))->method('register')->willReturn(null); + + $this->color->prepare(); + } +} From ea50040160e205afbfb936f98c5ea7f461aa915a Mon Sep 17 00:00:00 2001 From: Nazar Klovanych Date: Fri, 18 Oct 2019 20:50:10 +0300 Subject: [PATCH 2/3] Refactoring --- .../Ui/Component/Listing/Filter/ColorTest.php | 139 +++++++++++++++--- 1 file changed, 116 insertions(+), 23 deletions(-) diff --git a/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php b/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php index e1f21138bfa9..e8179d374c11 100644 --- a/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php +++ b/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php @@ -9,9 +9,11 @@ namespace Magento\AdobeStockImageAdminUi\Test\Unit\Model; use Magento\AdobeStockImageAdminUi\Ui\Component\Listing\Filter\Color; +use Magento\Framework\Api\Filter; use Magento\Framework\Api\FilterBuilder; use Magento\Framework\View\Element\UiComponent\ContextInterface; use Magento\Framework\View\Element\UiComponentFactory; +use Magento\Framework\View\Element\UiComponentInterface; use Magento\Ui\Component\Filters\FilterModifier; use Magento\Ui\Model\ColorPicker\ColorModesProvider; use PHPUnit\Framework\MockObject\MockObject; @@ -62,7 +64,9 @@ protected function setUp() $this->filterBuilder = $this->createMock(FilterBuilder::class); $this->filterModifier = $this->createMock(FilterModifier::class); $this->colorModesProvider = $this->createMock(ColorModesProvider::class); - + $this->contextInterface->expects($this->once()) + ->method('getFiltersParams') + ->willReturn(['placeholder' => true, 'colors_filter' => "#21ffff"]); $this->color = new Color( $this->contextInterface, $this->uiComponentFactory, @@ -70,35 +74,44 @@ protected function setUp() $this->filterModifier, $this->colorModesProvider, [], - ['name' => 'name_one', 'config/colorFormat' => 'format_one'] + $this->getData() ); - - $reflection = new \ReflectionClass($this->color); - $property = $reflection->getProperty('filterData'); - $property->setAccessible(true); - $property->setValue($this->color, ['name_one' => 'value_one']); } /** * Prepare test + * + * @param array $testData + * @dataProvider getTestData + * @throws \Magento\Framework\Exception\LocalizedException */ - public function testPrepare() + public function testPrepare(array $testData) { - $componentInterface = $this->createMock(\Magento\Framework\View\Element\UiComponentInterface::class); - $this->uiComponentFactory->expects($this->once())->method('create')->willReturn($componentInterface); - $componentInterface->expects($this->once())->method('prepare')->willReturn(null); - $contextInterface = $this->createMock(\Magento\Framework\View\Element\UiComponent\ContextInterface::class); - $componentInterface->expects($this->once())->method('getContext')->willReturn($contextInterface); - $contextInterface->expects($this->once())->method('getNamespace')->willReturn('name_space'); - $componentInterface->expects($this->exactly(2))->method('getData')->willReturn(['data']); - $this->filterBuilder->expects($this->once())->method('setConditionType')->willReturnSelf(); - $this->filterBuilder->expects($this->once())->method('setField')->willReturnSelf(); - $this->filterBuilder->expects($this->once())->method('setValue')->willReturnSelf(); - $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class); - $this->filterBuilder->expects($this->once())->method('create')->willReturn($filterMock); + $componentInterface = $this->createMock(UiComponentInterface::class); + $this->uiComponentFactory->expects($this->once()) + ->method('create') + ->willReturn($componentInterface); + $componentInterface->expects($this->once()) + ->method('getContext') + ->willReturn($this->contextInterface); + $componentInterface->expects($this->exactly(2)) + ->method('getData'); + $this->filterBuilder->expects($this->once()) + ->method('setConditionType') + ->willReturnSelf(); + $this->filterBuilder->expects($this->once()) + ->method('setField') + ->willReturnSelf(); + $this->filterBuilder->expects($this->once()) + ->method('setValue') + ->willReturnSelf(); + $filterMock = $this->createMock(Filter::class); + $this->filterBuilder->expects($this->once()) + ->method('create') + ->willReturn($filterMock); $this->colorModesProvider->expects($this->once()) ->method('getModes') - ->willReturn(['full' => ['preferredFormat' => 'one']]); + ->willReturn($testData['modes']); $dataProvider = $this->createMock( \Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class ); @@ -107,9 +120,89 @@ public function testPrepare() ->willReturn($dataProvider); $dataProvider->expects($this->once())->method('addFilter'); $processorMock = $this->createMock(\Magento\Framework\View\Element\UiComponent\Processor::class); - $this->contextInterface->expects($this->exactly(2))->method('getProcessor')->willReturn($processorMock); - $processorMock->expects($this->exactly(1))->method('register')->willReturn(null); + $this->contextInterface->expects($this->exactly(2)) + ->method('getProcessor') + ->willReturn($processorMock); + $processorMock->expects($this->exactly(1)) + ->method('register') + ->willReturn(null); $this->color->prepare(); } + + /** + * Test data + * + * @return array + */ + public function getTestData() + { + return + [ + [ + [ + 'modes' => [ + 'full' => [ + 'showInput' => true, + 'showInitial' => false, + 'showPalette' => true, + 'showAlpha' => true, + 'showSelectionPalette' => true, + ], + 'simple' => [ + 'showInput' => false, + 'showInitial' => false, + 'showPalette' => false, + 'showAlpha' => false, + 'showSelectionPalette' => true, + ], + 'noalpha' => [ + 'showInput' => true, + 'showInitial' => false, + 'showPalette' => true, + 'showAlpha' => false, + 'showSelectionPalette' => true, + ], + 'palette' => [ + 'showInput' => false, + 'showInitial' => false, + 'showPalette' => true, + 'showPaletteOnly' => true, + 'showAlpha' => false, + 'showSelectionPalette' => false, + ], + ] + ] + ] + + ]; + } + + /** + * Data for Color class + * + * @return array + */ + private function getData() + { + return [ + 'config' => [ + 'component' => 'Magento_Ui/js/form/element/color-picker', + 'template' => 'Magento_AdobeStockImageAdminUi/grid/filter/color', + 'label' => 'Color', + 'provider' => '${ $.parentName }', + 'sortOrder' => '30', + 'colorFormat' => 'HEX', + 'dataScope' => 'colors_filter', + 'placeholder' => 'HEX color', + ], + 'name' => 'colors_filter', + 'template' => 'Magento_AdobeStockImageAdminUi/grid/filter/color', + 'provider' => '${ $.parentName }', + 'sortOrder' => '30', + 'js_config' => [ + 'extends' => 'adobe_stock_images_listing', + ], + ]; + } } From b44179320e6de7275ac6f35e13844432d13daeca Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Sun, 20 Oct 2019 13:04:13 +0100 Subject: [PATCH 3/3] magento/adobe-stock-integration#549: Added additional verification to a unit test --- .../Ui/Component/Listing/Filter/ColorTest.php | 241 ++++++++++-------- 1 file changed, 133 insertions(+), 108 deletions(-) diff --git a/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php b/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php index e8179d374c11..68efc81ed542 100644 --- a/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php +++ b/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php @@ -3,7 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ - declare(strict_types=1); namespace Magento\AdobeStockImageAdminUi\Test\Unit\Model; @@ -18,16 +17,48 @@ use Magento\Ui\Model\ColorPicker\ColorModesProvider; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Magento\Ui\Component\Filters\Type\Input; +use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface; +use Magento\Framework\View\Element\UiComponent\Processor; /** - * SignInConfigProviderTest test. + * ColorTest test. */ class ColorTest extends TestCase { - /** - * @var MockObject|ContextInterface $contextInterface - */ - private $contextInterface; + private const FILTER_NAME = 'colors_filter'; + + private const COLOR_PICKER_MODES = [ + 'full' => [ + 'showInput' => true, + 'showInitial' => false, + 'showPalette' => true, + 'showAlpha' => true, + 'showSelectionPalette' => true, + ], + 'simple' => [ + 'showInput' => false, + 'showInitial' => false, + 'showPalette' => false, + 'showAlpha' => false, + 'showSelectionPalette' => true, + ], + 'noalpha' => [ + 'showInput' => true, + 'showInitial' => false, + 'showPalette' => true, + 'showAlpha' => false, + 'showSelectionPalette' => true, + ], + 'palette' => [ + 'showInput' => false, + 'showInitial' => false, + 'showPalette' => true, + 'showPaletteOnly' => true, + 'showAlpha' => false, + 'showSelectionPalette' => false, + ] + ]; /** * @var MockObject|UiComponentFactory $uiComponentFactory @@ -50,52 +81,98 @@ class ColorTest extends TestCase private $colorModesProvider; /** - * @var Color + * Create Color filter object */ - private $color; - - /** - * Set Up - */ - protected function setUp() + private function createObject(array $data, ContextInterface $context): Color { - $this->contextInterface = $this->createMock(ContextInterface::class); + $this->uiComponentFactory = $this->createMock(UiComponentFactory::class); $this->filterBuilder = $this->createMock(FilterBuilder::class); $this->filterModifier = $this->createMock(FilterModifier::class); $this->colorModesProvider = $this->createMock(ColorModesProvider::class); - $this->contextInterface->expects($this->once()) - ->method('getFiltersParams') - ->willReturn(['placeholder' => true, 'colors_filter' => "#21ffff"]); - $this->color = new Color( - $this->contextInterface, + return new Color( + $context, $this->uiComponentFactory, $this->filterBuilder, $this->filterModifier, $this->colorModesProvider, [], - $this->getData() + $data ); } + /** + * Get context + * + * @param array $filterParams + * @return ContextInterface + */ + private function getContext(array $filterParams): ContextInterface + { + $context = $this->createMock(ContextInterface::class); + $context->expects($this->once()) + ->method('getFiltersParams') + ->willReturn($filterParams); + $context->expects($this->any()) + ->method('getNamespace'); + + $processorMock = $this->createMock(Processor::class); + $context->expects($this->exactly(2)) + ->method('getProcessor') + ->willReturn($processorMock); + $processorMock->expects($this->exactly(1)) + ->method('register') + ->willReturn(null); + + return $context; + } + /** * Prepare test * * @param array $testData - * @dataProvider getTestData + * @dataProvider colorPickerModeProvider * @throws \Magento\Framework\Exception\LocalizedException */ - public function testPrepare(array $testData) + public function testPrepare(?string $colorPickerMode, string $appliedValue): void { - $componentInterface = $this->createMock(UiComponentInterface::class); + $filter = $this->createMock(Filter::class); + $context = $this->getContext( + [ + self::FILTER_NAME => $appliedValue + ] + ); + + $color = $this->createObject( + [ + 'config' => [ + 'colorPickerMode' => $colorPickerMode + ], + 'name' => self::FILTER_NAME + ], + $context + ); + $this->uiComponentFactory->expects($this->once()) ->method('create') - ->willReturn($componentInterface); - $componentInterface->expects($this->once()) - ->method('getContext') - ->willReturn($this->contextInterface); - $componentInterface->expects($this->exactly(2)) - ->method('getData'); + ->with( + self::FILTER_NAME, + Input::COMPONENT, + ['context' => $context] + ) + ->willReturn($this->getWrappedComponent($context)); + + $this->verifyApplyFilter($appliedValue, $filter, $context); + + $this->colorModesProvider->expects($this->once()) + ->method('getModes') + ->willReturn(self::COLOR_PICKER_MODES); + + $color->prepare(); + } + + private function verifyApplyFilter(string $appliedValue, Filter $filter, ContextInterface $context): void + { $this->filterBuilder->expects($this->once()) ->method('setConditionType') ->willReturnSelf(); @@ -104,105 +181,53 @@ public function testPrepare(array $testData) ->willReturnSelf(); $this->filterBuilder->expects($this->once()) ->method('setValue') + ->with(str_replace('#', '', $appliedValue)) ->willReturnSelf(); - $filterMock = $this->createMock(Filter::class); + $this->filterBuilder->expects($this->once()) ->method('create') - ->willReturn($filterMock); - $this->colorModesProvider->expects($this->once()) - ->method('getModes') - ->willReturn($testData['modes']); - $dataProvider = $this->createMock( - \Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class - ); - $this->contextInterface->expects($this->exactly(2)) + ->willReturn($filter); + + $dataProvider = $this->createMock(DataProviderInterface::class); + $context->expects($this->any()) ->method('getDataProvider') ->willReturn($dataProvider); - $dataProvider->expects($this->once())->method('addFilter'); - $processorMock = $this->createMock(\Magento\Framework\View\Element\UiComponent\Processor::class); - $this->contextInterface->expects($this->exactly(2)) - ->method('getProcessor') - ->willReturn($processorMock); - $processorMock->expects($this->exactly(1)) - ->method('register') - ->willReturn(null); - - $this->color->prepare(); + $dataProvider->expects($this->once()) + ->method('addFilter') + ->with($filter); } /** - * Test data + * Get wrapped component * - * @return array + * @return MockObject|UiComponentInterface */ - public function getTestData() + private function getWrappedComponent(ContextInterface $context): UiComponentInterface { - return - [ - [ - [ - 'modes' => [ - 'full' => [ - 'showInput' => true, - 'showInitial' => false, - 'showPalette' => true, - 'showAlpha' => true, - 'showSelectionPalette' => true, - ], - 'simple' => [ - 'showInput' => false, - 'showInitial' => false, - 'showPalette' => false, - 'showAlpha' => false, - 'showSelectionPalette' => true, - ], - 'noalpha' => [ - 'showInput' => true, - 'showInitial' => false, - 'showPalette' => true, - 'showAlpha' => false, - 'showSelectionPalette' => true, - ], - 'palette' => [ - 'showInput' => false, - 'showInitial' => false, - 'showPalette' => true, - 'showPaletteOnly' => true, - 'showAlpha' => false, - 'showSelectionPalette' => false, - ], - ] - ] - ] - - ]; + $wrappedComponent = $this->createMock(UiComponentInterface::class); + $wrappedComponent->expects($this->once()) + ->method('prepare'); + $wrappedComponent->expects($this->once()) + ->method('getContext') + ->willReturn($context); + + return $wrappedComponent; } /** - * Data for Color class + * Data provider for testPrepare * * @return array */ - private function getData() + public function colorPickerModeProvider(): array { return [ - 'config' => [ - 'component' => 'Magento_Ui/js/form/element/color-picker', - 'template' => 'Magento_AdobeStockImageAdminUi/grid/filter/color', - 'label' => 'Color', - 'provider' => '${ $.parentName }', - 'sortOrder' => '30', - 'colorFormat' => 'HEX', - 'dataScope' => 'colors_filter', - 'placeholder' => 'HEX color', - ], - 'name' => 'colors_filter', - 'template' => 'Magento_AdobeStockImageAdminUi/grid/filter/color', - 'provider' => '${ $.parentName }', - 'sortOrder' => '30', - 'js_config' => [ - 'extends' => 'adobe_stock_images_listing', + [ + 'full', '#21ffff' ], + [ + null, '#ffffff' + ] ]; } }