GroupTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Config\Source;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. /**
  9. * Class \Magento\Customer\Model\Config\Source\Group
  10. */
  11. class GroupTest extends \PHPUnit\Framework\TestCase
  12. {
  13. public function testToOptionArray()
  14. {
  15. /** @var Group $group */
  16. $group = Bootstrap::getObjectManager()->get(\Magento\Customer\Model\Config\Source\Group::class);
  17. $options = $group->toOptionArray();
  18. $this->assertContainsOptionRecursive('', '-- Please Select --', $options);
  19. }
  20. private function assertContainsOptionRecursive($expectedValue, $expectedLabel, array $values)
  21. {
  22. $this->assertTrue(
  23. $this->hasOptionLabelRecursive($expectedValue, $expectedLabel, $values),
  24. 'Label ' . $expectedLabel . ' not found'
  25. );
  26. }
  27. private function hasOptionLabelRecursive($value, $label, array $values)
  28. {
  29. $hasLabel = false;
  30. foreach ($values as $option) {
  31. $this->assertArrayHasKey('label', $option);
  32. $this->assertArrayHasKey('value', $option);
  33. if (strpos((string)$option['label'], (string)$label) !== false) {
  34. $this->assertEquals($value, $option['value']);
  35. $hasLabel = true;
  36. break;
  37. } elseif (is_array($option['value'])) {
  38. $hasLabel |= $this->hasOptionLabelRecursive($value, $label, $option['value']);
  39. }
  40. }
  41. return (bool)$hasLabel;
  42. }
  43. }