ObjectTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Convert\Test\Unit;
  7. use \Magento\Framework\Convert\DataObject;
  8. class ObjectTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\Convert\DataObject
  12. */
  13. protected $model;
  14. protected function setUp()
  15. {
  16. $this->model = new DataObject();
  17. }
  18. public function testToOptionArray()
  19. {
  20. $mockFirst = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getId', 'getCode']);
  21. $mockFirst->expects($this->once())
  22. ->method('getId')
  23. ->will($this->returnValue(1));
  24. $mockFirst->expects($this->once())
  25. ->method('getCode')
  26. ->will($this->returnValue('code1'));
  27. $mockSecond = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getId', 'getCode']);
  28. $mockSecond->expects($this->once())
  29. ->method('getId')
  30. ->will($this->returnValue(2));
  31. $mockSecond->expects($this->once())
  32. ->method('getCode')
  33. ->will($this->returnValue('code2'));
  34. $callable = function ($item) {
  35. return $item->getCode();
  36. };
  37. $items = [
  38. $mockFirst,
  39. $mockSecond,
  40. ];
  41. $result = [
  42. ['value' => 1, 'label' => 'code1'],
  43. ['value' => 2, 'label' => 'code2'],
  44. ];
  45. $this->assertEquals($result, $this->model->toOptionArray($items, 'id', $callable));
  46. }
  47. public function testToOptionHash()
  48. {
  49. $mockFirst = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getSome', 'getId']);
  50. $mockFirst->expects($this->once())
  51. ->method('getId')
  52. ->will($this->returnValue(3));
  53. $mockFirst->expects($this->once())
  54. ->method('getSome')
  55. ->will($this->returnValue('code3'));
  56. $mockSecond = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getSome', 'getId']);
  57. $mockSecond->expects($this->once())
  58. ->method('getId')
  59. ->will($this->returnValue(4));
  60. $mockSecond->expects($this->once())
  61. ->method('getSome')
  62. ->will($this->returnValue('code4'));
  63. $callable = function ($item) {
  64. return $item->getId();
  65. };
  66. $items = [
  67. $mockFirst,
  68. $mockSecond,
  69. ];
  70. $result = [
  71. 3 => 'code3',
  72. 4 => 'code4',
  73. ];
  74. $this->assertEquals($result, $this->model->toOptionHash($items, $callable, 'some'));
  75. }
  76. public function testConvertDataToArray()
  77. {
  78. $object = new \stdClass();
  79. $object->a = [[1]];
  80. $mockFirst = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getData']);
  81. $mockSecond = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getData']);
  82. $mockFirst->expects($this->any())
  83. ->method('getData')
  84. ->will($this->returnValue([
  85. 'id' => 1,
  86. 'o' => $mockSecond,
  87. ]));
  88. $mockSecond->expects($this->any())
  89. ->method('getData')
  90. ->will($this->returnValue([
  91. 'id' => 2,
  92. 'o' => $mockFirst,
  93. ]));
  94. $data = [
  95. 'object' => $mockFirst,
  96. 'stdClass' => $object,
  97. 'test' => 'test',
  98. ];
  99. $result = [
  100. 'object' => [
  101. 'id' => 1,
  102. 'o' => [
  103. 'id' => 2,
  104. 'o' => '*** CYCLE DETECTED ***',
  105. ],
  106. ],
  107. 'stdClass' => [
  108. 'a' => [
  109. [1],
  110. ],
  111. ],
  112. 'test' => 'test',
  113. ];
  114. $this->assertEquals($result, $this->model->convertDataToArray($data));
  115. }
  116. }