ArrayList.phpt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Test: Nette\Utils\ArrayList basic usage.
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\ArrayList;
  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('ArrayList::from', function () {
  22. Assert::exception(
  23. fn() => ArrayList::from(['a' => 1, 'b' => 2]),
  24. Nette\InvalidArgumentException::class,
  25. 'Array is not valid list.',
  26. );
  27. $mary = new Person('Mary');
  28. $list = ArrayList::from([$mary, 'Jack']);
  29. Assert::type(Nette\Utils\ArrayList::class, $list);
  30. Assert::same([$mary, 'Jack'], iterator_to_array($list));
  31. });
  32. test('', function () {
  33. $list = new ArrayList;
  34. $jack = new Person('Jack');
  35. $mary = new Person('Mary');
  36. $list[] = $mary;
  37. $list[] = $jack;
  38. Assert::same($mary, $list[0]);
  39. Assert::same($jack, $list[1]);
  40. Assert::true(isset($list[0]));
  41. Assert::false(isset($list[500]));
  42. Assert::false(isset($list['fake']));
  43. Assert::same([
  44. $mary,
  45. $jack,
  46. ], iterator_to_array($list));
  47. foreach ($list as $key => $person) {
  48. $tmp[] = $key . ' => ' . $person->sayHi();
  49. }
  50. Assert::same([
  51. '0 => My name is Mary',
  52. '1 => My name is Jack',
  53. ], $tmp);
  54. Assert::same(2, $list->count());
  55. Assert::same(2, count($list));
  56. unset($list[1]);
  57. Assert::same([
  58. $mary,
  59. ], iterator_to_array($list));
  60. $list->prepend('First');
  61. Assert::same('First', $list[0], 'Value "First" should be on the start of the array');
  62. });
  63. test('', function () {
  64. $list = new ArrayList;
  65. $list[] = 'a';
  66. $list[] = 'b';
  67. Assert::exception(
  68. fn() => $list[-1] = true,
  69. OutOfRangeException::class,
  70. 'Offset invalid or out of range',
  71. );
  72. Assert::exception(
  73. fn() => $list[2] = true,
  74. OutOfRangeException::class,
  75. 'Offset invalid or out of range',
  76. );
  77. Assert::exception(
  78. fn() => $list['key'] = true,
  79. OutOfRangeException::class,
  80. 'Offset invalid or out of range',
  81. );
  82. });
  83. test('', function () {
  84. $list = new ArrayList;
  85. $list[] = 'a';
  86. $list[] = 'b';
  87. Assert::exception(
  88. fn() => $list[-1],
  89. OutOfRangeException::class,
  90. 'Offset invalid or out of range',
  91. );
  92. Assert::exception(
  93. fn() => $list[2],
  94. OutOfRangeException::class,
  95. 'Offset invalid or out of range',
  96. );
  97. Assert::exception(
  98. fn() => $list['key'],
  99. OutOfRangeException::class,
  100. 'Offset invalid or out of range',
  101. );
  102. });
  103. test('', function () {
  104. $list = new ArrayList;
  105. $list[] = 'a';
  106. $list[] = 'b';
  107. Assert::exception(function () use ($list) {
  108. unset($list[-1]);
  109. }, OutOfRangeException::class, 'Offset invalid or out of range');
  110. Assert::exception(function () use ($list) {
  111. unset($list[2]);
  112. }, OutOfRangeException::class, 'Offset invalid or out of range');
  113. Assert::exception(function () use ($list) {
  114. unset($list['key']);
  115. }, OutOfRangeException::class, 'Offset invalid or out of range');
  116. });
  117. test('iteration with reference', function () {
  118. $list = ArrayList::from([1, 2, 3]);
  119. foreach ($list as $key => &$value) {
  120. $value = 'new';
  121. }
  122. Assert::same(['new', 'new', 'new'], iterator_to_array($list));
  123. });