ArrayHash.phpt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Test: Nette\Utils\ArrayHash basic usage.
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\ArrayHash;
  7. use Tester\Assert;
  8. require __DIR__ . '/../bootstrap.php';
  9. class Person
  10. {
  11. private $name;
  12. public function __construct($name)
  13. {
  14. $this->name = $name;
  15. }
  16. public function sayHi()
  17. {
  18. return "My name is $this->name";
  19. }
  20. }
  21. test('', function () {
  22. $list = new ArrayHash;
  23. $jack = new Person('Jack');
  24. $mary = new Person('Mary');
  25. $list['m'] = $mary;
  26. $list['j'] = $jack;
  27. Assert::same($mary, $list['m']);
  28. Assert::same($jack, $list['j']);
  29. Assert::same($mary, $list->m);
  30. Assert::same($jack, $list->j);
  31. Assert::same([
  32. 'm' => $mary,
  33. 'j' => $jack,
  34. ], iterator_to_array($list));
  35. Assert::same([
  36. 'm' => $mary,
  37. 'j' => $jack,
  38. ], (array) $list);
  39. foreach ($list as $key => $person) {
  40. $tmp[] = $key . ' => ' . $person->sayHi();
  41. }
  42. Assert::same([
  43. 'm => My name is Mary',
  44. 'j => My name is Jack',
  45. ], $tmp);
  46. Assert::same(2, $list->count());
  47. Assert::same(2, count($list));
  48. unset($list['j']);
  49. Assert::same([
  50. 'm' => $mary,
  51. ], iterator_to_array($list));
  52. });
  53. test('', function () {
  54. $mary = new Person('Mary');
  55. $list = ArrayHash::from([
  56. 'm' => $mary,
  57. 'j' => 'Jack',
  58. 'children' => [
  59. 'c' => 'John',
  60. ],
  61. ], recursive: false);
  62. Assert::type(Nette\Utils\ArrayHash::class, $list);
  63. Assert::type('array', $list['children']);
  64. });
  65. test('', function () {
  66. $mary = new Person('Mary');
  67. $list = ArrayHash::from([
  68. 'm' => $mary,
  69. 'j' => 'Jack',
  70. 'children' => [
  71. 'c' => 'John',
  72. ],
  73. ]);
  74. Assert::type(Nette\Utils\ArrayHash::class, $list);
  75. Assert::same($mary, $list['m']);
  76. Assert::same('Jack', $list['j']);
  77. Assert::type(Nette\Utils\ArrayHash::class, $list['children']);
  78. Assert::same('John', $list['children']['c']);
  79. $list['children']['c'] = 'Jim';
  80. Assert::same('Jim', $list['children']['c']);
  81. Assert::same([
  82. 'm' => $mary,
  83. 'j' => 'Jack',
  84. 'children' => $list['children'],
  85. 'c' => 'Jim',
  86. ], iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($list), RecursiveIteratorIterator::SELF_FIRST)));
  87. });
  88. test('numeric fields', function () {
  89. $row = ArrayHash::from([1, 2]);
  90. foreach ($row as $key => $value) {
  91. $keys[] = $key;
  92. }
  93. if (PHP_VERSION_ID < 70200) {
  94. Assert::same(['0', '1'], $keys);
  95. } else {
  96. Assert::same([0, 1], $keys);
  97. }
  98. Assert::same(1, $row->{0});
  99. Assert::same(1, $row->{'0'});
  100. Assert::same(1, $row[0]);
  101. Assert::same(1, $row['0']);
  102. Assert::true(isset($row->{0}));
  103. Assert::true(isset($row->{'0'}));
  104. Assert::true(isset($row[0]));
  105. Assert::true(isset($row['0']));
  106. Assert::same(2, $row->{1});
  107. Assert::same(2, $row->{'1'});
  108. Assert::same(2, $row[1]);
  109. Assert::same(2, $row['1']);
  110. Assert::true(isset($row->{1}));
  111. Assert::true(isset($row->{'1'}));
  112. Assert::true(isset($row[1]));
  113. Assert::true(isset($row['1']));
  114. Assert::false(isset($row->{2}));
  115. Assert::false(isset($row->{'2'}));
  116. Assert::false(isset($row[2]));
  117. Assert::false(isset($row['2']));
  118. $row[3] = 'new';
  119. Assert::same('new', $row->{3});
  120. Assert::same('new', $row->{'3'});
  121. Assert::same('new', $row[3]);
  122. Assert::same('new', $row['3']);
  123. unset($row[3]);
  124. Assert::false(isset($row->{3}));
  125. Assert::false(isset($row->{'3'}));
  126. Assert::false(isset($row[3]));
  127. Assert::false(isset($row['3']));
  128. });
  129. test('null fields', function () {
  130. $row = ArrayHash::from(['null' => null]);
  131. Assert::null($row->null);
  132. Assert::null($row['null']);
  133. Assert::false(isset($row->null));
  134. Assert::false(isset($row['null']));
  135. });
  136. test('undeclared fields', function () {
  137. $row = new ArrayHash;
  138. Assert::error(
  139. fn() => $row->undef,
  140. PHP_VERSION_ID < 80000 ? E_NOTICE : E_WARNING,
  141. 'Undefined property: Nette\Utils\ArrayHash::$undef',
  142. );
  143. Assert::error(
  144. fn() => $row['undef'],
  145. PHP_VERSION_ID < 80000 ? E_NOTICE : E_WARNING,
  146. 'Undefined property: Nette\Utils\ArrayHash::$undef',
  147. );
  148. });
  149. test('PHP 7 changed behavior https://3v4l.org/2A1pf', function () {
  150. $hash = ArrayHash::from([1, 2, 3]);
  151. foreach ($hash as $key => $value) {
  152. unset($hash->$key);
  153. }
  154. Assert::count(0, $hash);
  155. });
  156. test('iteration with reference', function () {
  157. $hash = ArrayHash::from([1, 2, 3]);
  158. foreach ($hash as $key => &$value) {
  159. $value = 'new';
  160. }
  161. Assert::same(['new', 'new', 'new'], (array) $hash);
  162. });