|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Catalog\Test\Unit\Model\Indexer\Product\Price\Plugin; |
| 7 | + |
| 8 | +use Magento\Catalog\Model\Indexer\Product\Price\DimensionModeConfiguration; |
| 9 | +use Magento\Catalog\Model\Indexer\Product\Price\Plugin\CustomerGroup; |
| 10 | +use Magento\Catalog\Model\Indexer\Product\Price\TableMaintainer; |
| 11 | +use Magento\Customer\Api\GroupRepositoryInterface; |
| 12 | +use Magento\Customer\Model\Data\Group; |
| 13 | +use Magento\Customer\Model\Indexer\CustomerGroupDimensionProvider; |
| 14 | +use Magento\Framework\Indexer\DimensionFactory; |
| 15 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | + |
| 18 | +/** |
| 19 | + * Test for CustomerGroup plugin |
| 20 | + */ |
| 21 | +class CustomerGroupTest extends \PHPUnit\Framework\TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var ObjectManager |
| 25 | + */ |
| 26 | + private $objectManager; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var CustomerGroup |
| 30 | + */ |
| 31 | + private $model; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var DimensionFactory|MockObject |
| 35 | + */ |
| 36 | + private $dimensionFactory; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var TableMaintainer|MockObject |
| 40 | + */ |
| 41 | + private $tableMaintainerMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var DimensionModeConfiguration|MockObject |
| 45 | + */ |
| 46 | + private $dimensionModeConfiguration; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var \Callable |
| 50 | + */ |
| 51 | + private $proceedMock; |
| 52 | + |
| 53 | + protected function setUp() |
| 54 | + { |
| 55 | + $this->objectManager = new ObjectManager($this); |
| 56 | + |
| 57 | + $this->dimensionFactory = $this->createPartialMock( |
| 58 | + DimensionFactory::class, |
| 59 | + ['create'] |
| 60 | + ); |
| 61 | + |
| 62 | + $this->dimensionModeConfiguration = $this->createPartialMock( |
| 63 | + DimensionModeConfiguration::class, |
| 64 | + ['getDimensionConfiguration'] |
| 65 | + ); |
| 66 | + |
| 67 | + $this->tableMaintainerMock = $this->createPartialMock( |
| 68 | + TableMaintainer::class, |
| 69 | + ['createTablesForDimensions'] |
| 70 | + ); |
| 71 | + |
| 72 | + $this->model = $this->objectManager->getObject( |
| 73 | + CustomerGroup::class, |
| 74 | + [ |
| 75 | + 'dimensionFactory' => $this->dimensionFactory, |
| 76 | + 'dimensionModeConfiguration' => $this->dimensionModeConfiguration, |
| 77 | + 'tableMaintainer' => $this->tableMaintainerMock, |
| 78 | + ] |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Check of call count createTablesForDimensions() method |
| 84 | + * |
| 85 | + * @param $customerGroupId |
| 86 | + * @param $callTimes |
| 87 | + * |
| 88 | + * @dataProvider aroundSaveDataProvider |
| 89 | + */ |
| 90 | + public function testAroundSave($customerGroupId, $callTimes) |
| 91 | + { |
| 92 | + $subjectMock = $this->createMock(GroupRepositoryInterface::class); |
| 93 | + $customerGroupMock = $this->createPartialMock( |
| 94 | + Group::class, |
| 95 | + ['getId'] |
| 96 | + ); |
| 97 | + $customerGroupMock->method('getId')->willReturn($customerGroupId); |
| 98 | + $this->tableMaintainerMock->expects( |
| 99 | + $this->exactly($callTimes) |
| 100 | + )->method('createTablesForDimensions'); |
| 101 | + $this->proceedMock = function ($customerGroupMock) { |
| 102 | + return $customerGroupMock; |
| 103 | + }; |
| 104 | + $this->dimensionModeConfiguration->method('getDimensionConfiguration')->willReturn( |
| 105 | + [CustomerGroupDimensionProvider::DIMENSION_NAME] |
| 106 | + ); |
| 107 | + $this->model->aroundSave($subjectMock, $this->proceedMock, $customerGroupMock); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Data provider for testAroundSave |
| 112 | + * |
| 113 | + * @return array |
| 114 | + */ |
| 115 | + public function aroundSaveDataProvider() |
| 116 | + { |
| 117 | + return [ |
| 118 | + 'customer_group_id = 0' => [ |
| 119 | + 'customer_group_id' => '0', |
| 120 | + 'create_tables_call_times' => 0 |
| 121 | + ], |
| 122 | + 'customer_group_id = 1' => [ |
| 123 | + 'customer_group_id' => '1', |
| 124 | + 'create_tables_call_times' => 0 |
| 125 | + ], |
| 126 | + 'customer_group_id = null' => [ |
| 127 | + 'customer_group_id' => null, |
| 128 | + 'create_tables_call_times' => 1 |
| 129 | + ], |
| 130 | + ]; |
| 131 | + } |
| 132 | +} |
0 commit comments