GroupTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Customer\Source;
  7. use Magento\Customer\Model\Customer\Source\Group;
  8. use Magento\Framework\Module\Manager;
  9. use Magento\Customer\Api\GroupRepositoryInterface;
  10. use Magento\Framework\Api\SearchCriteriaBuilder;
  11. use Magento\Framework\Api\SearchCriteria;
  12. use Magento\Customer\Api\Data\GroupSearchResultsInterface;
  13. class GroupTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var Group
  17. */
  18. private $model;
  19. /**
  20. * @var Manager|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $moduleManagerMock;
  23. /**
  24. * @var GroupRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $groupRepositoryMock;
  27. /**
  28. * @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $searchCriteriaBuilderMock;
  31. /**
  32. * @var SearchCriteria|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $searchCriteriaMock;
  35. /**
  36. * @var GroupSearchResultsInterface|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $searchResultMock;
  39. protected function setUp()
  40. {
  41. $this->moduleManagerMock = $this->getMockBuilder(Manager::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->groupRepositoryMock = $this->getMockBuilder(GroupRepositoryInterface::class)
  45. ->setMethods(['getList'])
  46. ->getMockForAbstractClass();
  47. $this->searchCriteriaBuilderMock = $this->getMockBuilder(SearchCriteriaBuilder::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->searchCriteriaMock = $this->getMockBuilder(SearchCriteria::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->searchResultMock = $this->getMockBuilder(GroupSearchResultsInterface::class)
  54. ->getMockForAbstractClass();
  55. $this->model = new Group(
  56. $this->moduleManagerMock,
  57. $this->groupRepositoryMock,
  58. $this->searchCriteriaBuilderMock
  59. );
  60. }
  61. public function testToOptionArray()
  62. {
  63. $customerGroups = [
  64. ['label' => __('ALL GROUPS'), 'value' => '32000'],
  65. ['label' => __('NOT LOGGED IN'), 'value' => '0'],
  66. ];
  67. $this->moduleManagerMock->expects($this->any())
  68. ->method('isEnabled')
  69. ->willReturn(true);
  70. $this->searchCriteriaBuilderMock->expects($this->any())
  71. ->method('create')
  72. ->willReturn($this->searchCriteriaMock);
  73. $this->groupRepositoryMock->expects($this->any())
  74. ->method('getList')
  75. ->with($this->searchCriteriaMock)
  76. ->willReturn($this->searchResultMock);
  77. $this->groupRepositoryMock->expects($this->any())
  78. ->method('getList')
  79. ->with($this->searchCriteriaMock)
  80. ->willReturn($this->searchResultMock);
  81. $groupTest = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class)
  82. ->disableOriginalConstructor()
  83. ->setMethods(['getCode', 'getId'])
  84. ->getMockForAbstractClass();
  85. $groupTest->expects($this->any())->method('getCode')->willReturn(__('NOT LOGGED IN'));
  86. $groupTest->expects($this->any())->method('getId')->willReturn('0');
  87. $groups = [$groupTest];
  88. $this->searchResultMock->expects($this->any())->method('getItems')->willReturn($groups);
  89. $actualCustomerGroups = $this->model->toOptionArray();
  90. $this->assertEquals($customerGroups, $actualCustomerGroups);
  91. foreach ($actualCustomerGroups as $actualCustomerGroup) {
  92. $this->assertInternalType('string', $actualCustomerGroup['value']);
  93. }
  94. }
  95. }