GroupTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\ResourceModel;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class GroupTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Customer\Model\ResourceModel\Group */
  15. protected $groupResourceModel;
  16. /** @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $resource;
  18. /** @var \Magento\Customer\Model\Vat|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $customerVat;
  20. /** @var \Magento\Customer\Model\Group|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $groupModel;
  22. /** @var \PHPUnit_Framework_MockObject_MockObject */
  23. protected $customersFactory;
  24. /** @var \PHPUnit_Framework_MockObject_MockObject */
  25. protected $groupManagement;
  26. /** @var \PHPUnit_Framework_MockObject_MockObject */
  27. protected $relationProcessorMock;
  28. /**
  29. * @var Snapshot|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $snapshotMock;
  32. /**
  33. * Setting up dependencies.
  34. *
  35. * @return void
  36. */
  37. protected function setUp()
  38. {
  39. $this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  40. $this->customerVat = $this->createMock(\Magento\Customer\Model\Vat::class);
  41. $this->customersFactory = $this->createPartialMock(
  42. \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory::class,
  43. ['create']
  44. );
  45. $this->groupManagement = $this->createPartialMock(
  46. \Magento\Customer\Api\GroupManagementInterface::class,
  47. ['getDefaultGroup', 'getNotLoggedInGroup', 'isReadOnly', 'getLoggedInGroups', 'getAllCustomersGroup']
  48. );
  49. $this->groupModel = $this->createMock(\Magento\Customer\Model\Group::class);
  50. $contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
  51. $contextMock->expects($this->once())->method('getResources')->willReturn($this->resource);
  52. $this->relationProcessorMock = $this->createMock(
  53. \Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor::class
  54. );
  55. $this->snapshotMock = $this->createMock(
  56. \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot::class
  57. );
  58. $transactionManagerMock = $this->createMock(
  59. \Magento\Framework\Model\ResourceModel\Db\TransactionManagerInterface::class
  60. );
  61. $transactionManagerMock->expects($this->any())
  62. ->method('start')
  63. ->willReturn($this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class));
  64. $contextMock->expects($this->once())
  65. ->method('getTransactionManager')
  66. ->willReturn($transactionManagerMock);
  67. $contextMock->expects($this->once())
  68. ->method('getObjectRelationProcessor')
  69. ->willReturn($this->relationProcessorMock);
  70. $this->groupResourceModel = (new ObjectManagerHelper($this))->getObject(
  71. \Magento\Customer\Model\ResourceModel\Group::class,
  72. [
  73. 'context' => $contextMock,
  74. 'groupManagement' => $this->groupManagement,
  75. 'customersFactory' => $this->customersFactory,
  76. 'entitySnapshot' => $this->snapshotMock
  77. ]
  78. );
  79. }
  80. /**
  81. * Test for save() method when we try to save entity with system's reserved ID.
  82. * @return void
  83. */
  84. public function testSaveWithReservedId()
  85. {
  86. $expectedId = 55;
  87. $this->snapshotMock->expects($this->once())->method('isModified')->willReturn(true);
  88. $this->snapshotMock->expects($this->once())->method('registerSnapshot')->willReturnSelf();
  89. $this->groupModel->expects($this->any())->method('getId')
  90. ->willReturn(\Magento\Customer\Model\Group::CUST_GROUP_ALL);
  91. $this->groupModel->expects($this->any())->method('getData')
  92. ->willReturn([]);
  93. $this->groupModel->expects($this->any())->method('isSaveAllowed')
  94. ->willReturn(true);
  95. $this->groupModel->expects($this->any())->method('getStoredData')
  96. ->willReturn([]);
  97. $this->groupModel->expects($this->once())->method('setId')
  98. ->with($expectedId);
  99. $dbAdapter = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  100. ->disableOriginalConstructor()
  101. ->setMethods(
  102. [
  103. 'lastInsertId',
  104. 'describeTable',
  105. 'update',
  106. 'select'
  107. ]
  108. )
  109. ->getMockForAbstractClass();
  110. $dbAdapter->expects($this->any())->method('describeTable')->willReturn([]);
  111. $dbAdapter->expects($this->any())->method('update')->willReturnSelf();
  112. $dbAdapter->expects($this->once())->method('lastInsertId')->willReturn($expectedId);
  113. $selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
  114. ->disableOriginalConstructor()
  115. ->getMock();
  116. $dbAdapter->expects($this->any())->method('select')->willReturn($selectMock);
  117. $selectMock->expects($this->any())->method('from')->willReturnSelf();
  118. $this->resource->expects($this->any())->method('getConnection')->willReturn($dbAdapter);
  119. $this->groupResourceModel->save($this->groupModel);
  120. }
  121. /**
  122. * Test for delete() method when we try to save entity with system's reserved ID.
  123. *
  124. * @return void
  125. */
  126. public function testDelete()
  127. {
  128. $dbAdapter = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  129. $this->resource->expects($this->any())->method('getConnection')->will($this->returnValue($dbAdapter));
  130. $customer = $this->createPartialMock(
  131. \Magento\Customer\Model\Customer::class,
  132. ['__wakeup', 'load', 'getId', 'getStoreId', 'setGroupId', 'save']
  133. );
  134. $customerId = 1;
  135. $customer->expects($this->once())->method('getId')->will($this->returnValue($customerId));
  136. $customer->expects($this->once())->method('load')->with($customerId)->will($this->returnSelf());
  137. $defaultCustomerGroup = $this->createPartialMock(\Magento\Customer\Model\Group::class, ['getId']);
  138. $this->groupManagement->expects($this->once())->method('getDefaultGroup')
  139. ->will($this->returnValue($defaultCustomerGroup));
  140. $defaultCustomerGroup->expects($this->once())->method('getId')
  141. ->will($this->returnValue(1));
  142. $customer->expects($this->once())->method('setGroupId')->with(1);
  143. $customerCollection = $this->createMock(\Magento\Customer\Model\ResourceModel\Customer\Collection::class);
  144. $customerCollection->expects($this->once())->method('addAttributeToFilter')->will($this->returnSelf());
  145. $customerCollection->expects($this->once())->method('load')->will($this->returnValue([$customer]));
  146. $this->customersFactory->expects($this->once())->method('create')
  147. ->will($this->returnValue($customerCollection));
  148. $this->relationProcessorMock->expects($this->once())->method('delete');
  149. $this->groupModel->expects($this->any())->method('getData')->willReturn(['data' => 'value']);
  150. $this->groupResourceModel->delete($this->groupModel);
  151. }
  152. }