pastebin

Paste Search Dynamic
Recent pastes
ArrayHash
  1. <?php
  2.  
  3. /**
  4.  * Test: NetteUtilsArrayHash basic usage.
  5.  */
  6.  
  7. declare(strict_types=1);
  8.  
  9. use NetteUtilsArrayHash;
  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('', function () {
  35.         $list = new ArrayHash;
  36.         $jack = new Person('Jack');
  37.         $mary = new Person('Mary');
  38.  
  39.         $list['m'] = $mary;
  40.         $list['j'] = $jack;
  41.  
  42.         assert::same($mary, $list['m']);
  43.         assert::same($jack, $list['j']);
  44.  
  45.         assert::same($mary, $list->m);
  46.         assert::same($jack, $list->j);
  47.  
  48.  
  49.         assert::same([
  50.                 'm' => $mary,
  51.                 'j' => $jack,
  52.         ], iterator_to_array($list));
  53.  
  54.  
  55.         assert::same([
  56.                 'm' => $mary,
  57.                 'j' => $jack,
  58.         ], (array) $list);
  59.  
  60.  
  61.         foreach ($list as $key => $person) {
  62.                 $tmp[] = $key . ' => ' . $person->sayHi();
  63.         }
  64.  
  65.         assert::same([
  66.                 'm => My name is Mary',
  67.                 'j => My name is Jack',
  68.         ], $tmp);
  69.  
  70.  
  71.         assert::same(2, $list->count());
  72.         assert::same(2, count($list));
  73.  
  74.  
  75.         unset($list['j']);
  76.         assert::same([
  77.                 'm' => $mary,
  78.         ], iterator_to_array($list));
  79. });
  80.  
  81.  
  82. test('', function () {
  83.         $mary = new Person('Mary');
  84.         $list = ArrayHash::from([
  85.                 'm' => $mary,
  86.                 'j' => 'Jack',
  87.                 'children' => [
  88.                         'c' => 'John',
  89.                 ],
  90.         ], false);
  91.         assert::type(NetteUtilsArrayHash::class, $list);
  92.         assert::type('array', $list['children']);
  93. });
  94.  
  95.  
  96. test('', function () {
  97.         $mary = new Person('Mary');
  98.         $list = ArrayHash::from([
  99.                 'm' => $mary,
  100.                 'j' => 'Jack',
  101.                 'children' => [
  102.                         'c' => 'John',
  103.                 ],
  104.         ]);
  105.         assert::type(NetteUtilsArrayHash::class, $list);
  106.         assert::same($mary, $list['m']);
  107.         assert::same('Jack', $list['j']);
  108.         assert::type(NetteUtilsArrayHash::class, $list['children']);
  109.         assert::same('John', $list['children']['c']);
  110.  
  111.         $list['children']['c'] = 'Jim';
  112.         assert::same('Jim', $list['children']['c']);
  113.  
  114.  
  115.         assert::same([
  116.                 'm' => $mary,
  117.                 'j' => 'Jack',
  118.                 'children' => $list['children'],
  119.                 'c' => 'Jim',
  120.         ], iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($list), RecursiveIteratorIterator::SELF_FIRST)));
  121. });
  122.  
  123.  
  124. test('numeric fields', function () {
  125.         $row = ArrayHash::from([1, 2]);
  126.  
  127.         foreach ($row as $key => $value) {
  128.                 $keys[] = $key;
  129.         }
  130.  
  131.         if (PHP_VERSION_ID < 70200) {
  132.                 assert::same(['0', '1'], $keys);
  133.         } else {
  134.                 assert::same([0, 1], $keys);
  135.         }
  136.  
  137.         assert::same(1, $row->{0});
  138.         assert::same(1, $row->{'0'});
  139.         assert::same(1, $row[0]);
  140.         assert::same(1, $row['0']);
  141.         assert::true(isset($row->{0}));
  142.         assert::true(isset($row->{'0'}));
  143.         assert::true(isset($row[0]));
  144.         assert::true(isset($row['0']));
  145.  
  146.         assert::same(2, $row->{1});
  147.         assert::same(2, $row->{'1'});
  148.         assert::same(2, $row[1]);
  149.         assert::same(2, $row['1']);
  150.         assert::true(isset($row->{1}));
  151.         assert::true(isset($row->{'1'}));
  152.         assert::true(isset($row[1]));
  153.         assert::true(isset($row['1']));
  154.  
  155.         assert::false(isset($row->{2}));
  156.         assert::false(isset($row->{'2'}));
  157.         assert::false(isset($row[2]));
  158.         assert::false(isset($row['2']));
  159.  
  160.         $row[3] = 'new';
  161.         assert::same('new', $row->{3});
  162.         assert::same('new', $row->{'3'});
  163.         assert::same('new', $row[3]);
  164.         assert::same('new', $row['3']);
  165.  
  166.         unset($row[3]);
  167.         assert::false(isset($row->{3}));
  168.         assert::false(isset($row->{'3'}));
  169.         assert::false(isset($row[3]));
  170.         assert::false(isset($row['3']));
  171. });
  172.  
  173.  
  174. test('null fields', function () {
  175.         $row = ArrayHash::from(['null' => null]);
  176.         assert::null($row->null);
  177.         assert::null($row['null']);
  178.         assert::false(isset($row->null));
  179.         assert::false(isset($row['null']));
  180. });
  181.  
  182.  
  183. test('undeclared fields', function () {
  184.         $row = new ArrayHash;
  185.         assert::error(
  186.                 fn() => $row->undef,
  187.                 PHP_VERSION_ID < 80000 ? e_notice : e_warning,
  188.                 'Undefined property: NetteUtilsArrayHash::$undef',
  189.         );
  190.  
  191.         assert::error(
  192.                 fn() => $row['undef'],
  193.                 PHP_VERSION_ID < 80000 ? e_notice : e_warning,
  194.                 'Undefined property: NetteUtilsArrayHash::$undef',
  195.         );
  196. });
  197.  
  198.  
  199. test('PHP 7 changed behavior https://3v4l.org/2A1pf', function () {
  200.         $hash = ArrayHash::from([1, 2, 3]);
  201.         foreach ($hash as $key => $value) {
  202.                 unset($hash->$key);
  203.         }
  204.  
  205.         assert::count(0, $hash);
  206. });
  207.  
  208.  
  209. test('iteration with reference', function () {
  210.         $hash = ArrayHash::from([1, 2, 3]);
  211.         foreach ($hash as $key => &$value) {
  212.                 $value = 'new';
  213.         }
  214.  
  215.         assert::same(['new', 'new', 'new'], (array) $hash);
  216. });
Parsed in 0.574 seconds