pastebin

Paste Search Dynamic
Recent pastes
ArrayList
  1. <?php
  2.  
  3. /**
  4.  * Test: NetteUtilsArrayList basic usage.
  5.  */
  6.  
  7. declare(strict_types=1);
  8.  
  9. use NetteUtilsArrayList;
  10. use TesterAssert;
  11.  
  12.  
  13. require __DIR__ . '/../bootstrap.php';
  14.  
  15.  
  16. class Person
  17. {
  18.         private $name;
  19.  
  20.  
  21.         public function __construct($name)
  22.         {
  23.                 $this->name = $name;
  24.         }
  25.  
  26.  
  27.         public function sayHi()
  28.         {
  29.                 return "My name is $this->name";
  30.         }
  31. }
  32.  
  33.  
  34. test('ArrayList::from', function () {
  35.         assert::exception(
  36.                 fn() => ArrayList::from(['a' => 1, 'b' => 2]),
  37.                 NetteInvalidArgumentException::class,
  38.                 'Array is not valid list.',
  39.         );
  40.  
  41.         $mary = new Person('Mary');
  42.         $list = ArrayList::from([$mary, 'Jack']);
  43.  
  44.         assert::type(NetteUtilsArrayList::class, $list);
  45.         assert::same([$mary, 'Jack'], iterator_to_array($list));
  46. });
  47.  
  48.  
  49. test('', function () {
  50.         $list = new ArrayList;
  51.         $jack = new Person('Jack');
  52.         $mary = new Person('Mary');
  53.  
  54.         $list[] = $mary;
  55.         $list[] = $jack;
  56.  
  57.         assert::same($mary, $list[0]);
  58.         assert::same($jack, $list[1]);
  59.  
  60.         assert::true(isset($list[0]));
  61.         assert::false(isset($list[500]));
  62.         assert::false(isset($list['fake']));
  63.  
  64.         assert::same([
  65.                 $mary,
  66.                 $jack,
  67.         ], iterator_to_array($list));
  68.  
  69.  
  70.         foreach ($list as $key => $person) {
  71.                 $tmp[] = $key . ' => ' . $person->sayHi();
  72.         }
  73.  
  74.         assert::same([
  75.                 '0 => My name is Mary',
  76.                 '1 => My name is Jack',
  77.         ], $tmp);
  78.  
  79.  
  80.         assert::same(2, $list->count());
  81.         assert::same(2, count($list));
  82.  
  83.         unset($list[1]);
  84.         assert::same([
  85.                 $mary,
  86.         ], iterator_to_array($list));
  87.  
  88.         $list->prepend('First');
  89.         assert::same('First', $list[0], 'Value "First" should be on the start of the array');
  90. });
  91.  
  92.  
  93. test('', function () {
  94.         $list = new ArrayList;
  95.         $list[] = 'a';
  96.         $list[] = 'b';
  97.  
  98.         assert::exception(
  99.                 fn() => $list[-1] = true,
  100.                 OutOfRangeException::class,
  101.                 'Offset invalid or out of range',
  102.         );
  103.  
  104.         assert::exception(
  105.                 fn() => $list[2] = true,
  106.                 OutOfRangeException::class,
  107.                 'Offset invalid or out of range',
  108.         );
  109.  
  110.         assert::exception(
  111.                 fn() => $list['key'] = true,
  112.                 OutOfRangeException::class,
  113.                 'Offset invalid or out of range',
  114.         );
  115. });
  116.  
  117.  
  118. test('', function () {
  119.         $list = new ArrayList;
  120.         $list[] = 'a';
  121.         $list[] = 'b';
  122.  
  123.         assert::exception(
  124.                 fn() => $list[-1],
  125.                 OutOfRangeException::class,
  126.                 'Offset invalid or out of range',
  127.         );
  128.  
  129.         assert::exception(
  130.                 fn() => $list[2],
  131.                 OutOfRangeException::class,
  132.                 'Offset invalid or out of range',
  133.         );
  134.  
  135.         assert::exception(
  136.                 fn() => $list['key'],
  137.                 OutOfRangeException::class,
  138.                 'Offset invalid or out of range',
  139.         );
  140. });
  141.  
  142.  
  143. test('', function () {
  144.         $list = new ArrayList;
  145.         $list[] = 'a';
  146.         $list[] = 'b';
  147.  
  148.         assert::exception(function () use ($list) {
  149.                 unset($list[-1]);
  150.         }, OutOfRangeException::class, 'Offset invalid or out of range');
  151.  
  152.         assert::exception(function () use ($list) {
  153.                 unset($list[2]);
  154.         }, OutOfRangeException::class, 'Offset invalid or out of range');
  155.  
  156.         assert::exception(function () use ($list) {
  157.                 unset($list['key']);
  158.         }, OutOfRangeException::class, 'Offset invalid or out of range');
  159. });
  160.  
  161.  
  162. test('iteration with reference', function () {
  163.         $list = ArrayList::from([1, 2, 3]);
  164.         foreach ($list as $key => &$value) {
  165.                 $value = 'new';
  166.         }
  167.  
  168.         assert::same(['new', 'new', 'new'], iterator_to_array($list));
  169. });
Parsed in 0.201 seconds