123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Model\ResourceModel;
- use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class GroupRepositoryTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Customer\Model\GroupRegistry|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $groupRegistry;
- /**
- * @var \Magento\Customer\Model\GroupFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $groupFactory;
- /**
- * @var \Magento\Customer\Model\Group|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $groupModel;
- /**
- * @var \Magento\Customer\Api\Data\GroupInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $groupDataFactory;
- /**
- * @var \Magento\Customer\Api\Data\GroupInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $group;
- /**
- * @var \Magento\Customer\Api\Data\GroupInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $factoryCreatedGroup;
- /**
- * @var \Magento\Customer\Model\ResourceModel\Group|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $groupResourceModel;
- /**
- * @var \Magento\Framework\Reflection\DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $dataObjectProcessor;
- /**
- * @var \Magento\Customer\Api\Data\GroupSearchResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $searchResultsFactory;
- /**
- * @var \Magento\Customer\Api\Data\GroupSearchResultsInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $searchResults;
- /**
- * @var \Magento\Tax\Api\TaxClassRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $taxClassRepository;
- /**
- * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $extensionAttributesJoinProcessor;
- /**
- * @var CollectionProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $collectionProcessorMock;
- /**
- * @var \Magento\Customer\Model\ResourceModel\GroupRepository
- */
- protected $model;
- protected function setUp()
- {
- $this->setupGroupObjects();
- $this->dataObjectProcessor = $this->createMock(\Magento\Framework\Reflection\DataObjectProcessor::class);
- $this->searchResultsFactory = $this->createPartialMock(
- \Magento\Customer\Api\Data\GroupSearchResultsInterfaceFactory::class,
- ['create']
- );
- $this->searchResults = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\GroupSearchResultsInterface::class,
- [],
- '',
- false
- );
- $this->taxClassRepository = $this->getMockForAbstractClass(
- \Magento\Tax\Api\TaxClassRepositoryInterface::class,
- [],
- '',
- false
- );
- $this->extensionAttributesJoinProcessor = $this->getMockForAbstractClass(
- \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class,
- [],
- '',
- false
- );
- $this->collectionProcessorMock = $this->getMockBuilder(CollectionProcessorInterface::class)
- ->getMock();
- $this->model = new \Magento\Customer\Model\ResourceModel\GroupRepository(
- $this->groupRegistry,
- $this->groupFactory,
- $this->groupDataFactory,
- $this->groupResourceModel,
- $this->dataObjectProcessor,
- $this->searchResultsFactory,
- $this->taxClassRepository,
- $this->extensionAttributesJoinProcessor,
- $this->collectionProcessorMock
- );
- }
- private function setupGroupObjects()
- {
- $this->groupRegistry = $this->createMock(\Magento\Customer\Model\GroupRegistry::class);
- $this->groupFactory = $this->createPartialMock(\Magento\Customer\Model\GroupFactory::class, ['create']);
- $this->groupModel = $this->getMockBuilder(\Magento\Customer\Model\Group::class)
- ->setMethods(
- [
- 'getTaxClassId',
- 'getTaxClassName',
- 'getId',
- 'getCode',
- 'setDataUsingMethod',
- 'setCode',
- 'setTaxClassId',
- 'usesAsDefault',
- 'delete',
- 'getCollection',
- 'getData',
- ]
- )
- ->setMockClassName('groupModel')
- ->disableOriginalConstructor()
- ->getMock();
- $this->groupDataFactory = $this->createPartialMock(
- \Magento\Customer\Api\Data\GroupInterfaceFactory::class,
- ['create']
- );
- $this->group = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\GroupInterface::class,
- [],
- 'group',
- false
- );
- $this->factoryCreatedGroup = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\GroupInterface::class,
- [],
- 'group',
- false
- );
- $this->groupResourceModel = $this->createMock(\Magento\Customer\Model\ResourceModel\Group::class);
- }
- public function testSave()
- {
- $groupId = 0;
- $taxClass = $this->getMockForAbstractClass(\Magento\Tax\Api\Data\TaxClassInterface::class, [], '', false);
- $extensionAttributes = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\GroupExtensionInterface::class
- );
- $this->group->expects($this->atLeastOnce())
- ->method('getCode')
- ->willReturn('Code');
- $this->group->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($groupId);
- $this->group->expects($this->atLeastOnce())
- ->method('getTaxClassId')
- ->willReturn(17);
- $this->group->expects($this->atLeastOnce())
- ->method('getExtensionAttributes')
- ->willReturn($extensionAttributes);
- $this->groupModel->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($groupId);
- $this->groupModel->expects($this->atLeastOnce())
- ->method('getCode')
- ->willReturn('Code');
- $this->groupModel->expects($this->atLeastOnce())
- ->method('getTaxClassId')
- ->willReturn(234);
- $this->groupModel->expects($this->atLeastOnce())
- ->method('getTaxClassName')
- ->willReturn('Tax class name');
- $this->factoryCreatedGroup->expects($this->once())
- ->method('setId')
- ->with($groupId)
- ->willReturnSelf();
- $this->factoryCreatedGroup->expects($this->once())
- ->method('setCode')
- ->with('Code')
- ->willReturnSelf();
- $this->factoryCreatedGroup->expects($this->once())
- ->method('setTaxClassId')
- ->with(234)
- ->willReturnSelf();
- $this->factoryCreatedGroup->expects($this->once())
- ->method('setTaxClassName')
- ->with('Tax class name')
- ->willReturnSelf();
- $this->factoryCreatedGroup->expects($this->once())
- ->method('setExtensionAttributes')
- ->with($extensionAttributes)
- ->willReturnSelf();
- $this->factoryCreatedGroup->expects($this->atLeastOnce())
- ->method('getCode')
- ->willReturn('Code');
- $this->factoryCreatedGroup->expects($this->atLeastOnce())
- ->method('getTaxClassId')
- ->willReturn(17);
- $this->taxClassRepository->expects($this->once())
- ->method('get')
- ->with(17)
- ->willReturn($taxClass);
- $taxClass->expects($this->once())
- ->method('getClassType')
- ->willReturn('CUSTOMER');
- $this->groupRegistry->expects($this->once())
- ->method('retrieve')
- ->with($groupId)
- ->willReturn($this->groupModel);
- $this->dataObjectProcessor->expects($this->once())
- ->method('buildOutputDataArray')
- ->with($this->group, \Magento\Customer\Api\Data\GroupInterface::class)
- ->willReturn(['attributeCode' => 'attributeData']);
- $this->groupModel->expects($this->once())
- ->method('setDataUsingMethod')
- ->with('attributeCode', 'attributeData');
- $this->groupResourceModel->expects($this->once())
- ->method('save')
- ->with($this->groupModel);
- $this->groupRegistry->expects($this->once())
- ->method('remove')
- ->with($groupId);
- $this->groupDataFactory->expects($this->once())
- ->method('create')
- ->willReturn($this->factoryCreatedGroup);
- $updatedGroup = $this->model->save($this->group);
- $this->assertSame($this->group->getCode(), $updatedGroup->getCode());
- $this->assertSame($this->group->getTaxClassId(), $updatedGroup->getTaxClassId());
- }
- /**
- * @expectedException \Magento\Framework\Exception\State\InvalidTransitionException
- */
- public function testSaveWithException()
- {
- $taxClass = $this->getMockForAbstractClass(\Magento\Tax\Api\Data\TaxClassInterface::class, [], '', false);
- $this->groupFactory->expects($this->once())
- ->method('create')
- ->willReturn($this->groupModel);
- $this->group->expects($this->atLeastOnce())
- ->method('getCode')
- ->willReturn('Code');
- $this->group->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn(false);
- $this->group->expects($this->atLeastOnce())
- ->method('getTaxClassId')
- ->willReturn(234);
- $this->group->expects($this->atLeastOnce())
- ->method('getTaxClassId')
- ->willReturn(17);
- $this->groupModel->expects($this->once())
- ->method('setCode')
- ->with('Code');
- $this->groupModel->expects($this->once())
- ->method('setTaxClassId')
- ->with(234);
- $this->taxClassRepository->expects($this->once())
- ->method('get')
- ->with(234)
- ->willReturn($taxClass);
- $taxClass->expects($this->once())
- ->method('getClassType')
- ->willReturn('CUSTOMER');
- $this->groupResourceModel->expects($this->once())
- ->method('save')
- ->with($this->groupModel)
- ->willThrowException(new \Magento\Framework\Exception\LocalizedException(
- new \Magento\Framework\Phrase('Customer Group already exists.')
- ));
- $this->model->save($this->group);
- }
- public function testGetById()
- {
- $groupId = 86;
- $this->groupRegistry->expects($this->once())
- ->method('retrieve')
- ->with($groupId)
- ->willReturn($this->groupModel);
- $this->groupDataFactory->expects($this->once())
- ->method('create')
- ->willReturn($this->group);
- $this->group->expects($this->once())
- ->method('setId')
- ->with($groupId)
- ->willReturnSelf();
- $this->group->expects($this->once())
- ->method('setCode')
- ->with('Code')
- ->willReturnSelf();
- $this->group->expects($this->once())
- ->method('setTaxClassId')
- ->with(234)
- ->willReturnSelf();
- $this->group->expects($this->once())
- ->method('setTaxClassName')
- ->with('Tax class name')
- ->willReturnSelf();
- $this->groupModel->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($groupId);
- $this->groupModel->expects($this->atLeastOnce())
- ->method('getCode')
- ->willReturn('Code');
- $this->groupModel->expects($this->atLeastOnce())
- ->method('getTaxClassId')
- ->willReturn(234);
- $this->groupModel->expects($this->atLeastOnce())
- ->method('getTaxClassName')
- ->willReturn('Tax class name');
- $this->assertSame($this->group, $this->model->getById($groupId));
- }
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testGetList()
- {
- $groupId = 86;
- $groupExtension = $this->createMock(\Magento\Customer\Api\Data\GroupExtensionInterface::class);
- $collection = $this->createMock(\Magento\Customer\Model\ResourceModel\Group\Collection::class);
- $searchCriteria = $this->getMockForAbstractClass(
- \Magento\Framework\Api\SearchCriteriaInterface::class,
- [],
- '',
- false
- );
- $searchResults = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\AddressSearchResultsInterface::class,
- [],
- '',
- false
- );
- $this->searchResultsFactory->expects($this->once())
- ->method('create')
- ->willReturn($searchResults);
- $searchResults->expects($this->once())
- ->method('setSearchCriteria')
- ->with($searchCriteria);
- $this->groupFactory->expects($this->once())
- ->method('create')
- ->willReturn($this->groupModel);
- $this->groupModel->expects($this->once())
- ->method('getCollection')
- ->willReturn($collection);
- $this->extensionAttributesJoinProcessor->expects($this->once())
- ->method('process')
- ->with($collection, \Magento\Customer\Api\Data\GroupInterface::class);
- $collection->expects($this->once())
- ->method('addTaxClass');
- $this->collectionProcessorMock->expects($this->once())
- ->method('process')
- ->with($searchCriteria, $collection);
- $collection->expects($this->once())
- ->method('getIterator')
- ->willReturn(new \ArrayIterator([$this->groupModel]));
- $this->groupDataFactory->expects($this->once())
- ->method('create')
- ->willReturn($this->group);
- $this->group->expects($this->once())
- ->method('setId')
- ->with($groupId)
- ->willReturnSelf();
- $this->group->expects($this->once())
- ->method('setCode')
- ->with('Code')
- ->willReturnSelf();
- $this->group->expects($this->once())
- ->method('setTaxClassId')
- ->with(234)
- ->willReturnSelf();
- $this->group->expects($this->once())
- ->method('setTaxClassName')
- ->with('Tax class name')
- ->willReturnSelf();
- $this->groupModel->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($groupId);
- $this->groupModel->expects($this->atLeastOnce())
- ->method('getCode')
- ->willReturn('Code');
- $this->groupModel->expects($this->atLeastOnce())
- ->method('getTaxClassId')
- ->willReturn(234);
- $this->groupModel->expects($this->atLeastOnce())
- ->method('getTaxClassName')
- ->willReturn('Tax class name');
- $this->groupModel->expects($this->once())
- ->method('getData')
- ->willReturn([]);
- $this->extensionAttributesJoinProcessor->expects($this->once())
- ->method('extractExtensionAttributes')
- ->with(\Magento\Customer\Api\Data\GroupInterface::class, [])
- ->willReturn(['extension_attributes' => $groupExtension]);
- $this->group->expects($this->once())
- ->method('setExtensionAttributes')
- ->with($groupExtension);
- $collection
- ->expects($this->once())
- ->method('getSize')
- ->willReturn(9);
- $searchResults->expects($this->once())
- ->method('setTotalCount')
- ->with(9);
- $searchResults->expects($this->once())
- ->method('setItems')
- ->with([$this->group])
- ->willReturnSelf();
- $this->assertSame($searchResults, $this->model->getList($searchCriteria));
- }
- public function testDeleteById()
- {
- $groupId = 6;
- $this->groupRegistry->expects($this->once())
- ->method('retrieve')
- ->with($groupId)
- ->willReturn($this->groupModel);
- $this->groupModel->expects($this->once())
- ->method('usesAsDefault')
- ->willReturn(false);
- $this->groupModel->expects($this->once())
- ->method('delete');
- $this->groupRegistry
- ->expects($this->once())
- ->method('remove')
- ->with($groupId);
- $this->assertTrue($this->model->deleteById($groupId));
- }
- public function testDelete()
- {
- $groupId = 6;
- $this->group->expects($this->once())
- ->method('getId')
- ->willReturn($groupId);
- $this->groupRegistry->expects($this->once())
- ->method('retrieve')
- ->with($groupId)
- ->willReturn($this->groupModel);
- $this->groupModel->expects($this->once())
- ->method('usesAsDefault')
- ->willReturn(false);
- $this->groupModel->expects($this->once())
- ->method('delete');
- $this->groupRegistry
- ->expects($this->once())
- ->method('remove')
- ->with($groupId);
- $this->assertTrue($this->model->delete($this->group));
- }
- }
|