Arrays.insertBefore().phpt 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Arrays::insertBefore() & insertAfter()
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\Arrays;
  7. use Tester\Assert;
  8. require __DIR__ . '/../bootstrap.php';
  9. $arr = [
  10. '' => 'first',
  11. 0 => 'second',
  12. 1 => 'third',
  13. 7 => 'fourth',
  14. ];
  15. test('First item', function () use ($arr) {
  16. $dolly = $arr;
  17. Arrays::insertBefore($dolly, null, ['new' => 'value']);
  18. Assert::same([
  19. 'new' => 'value',
  20. '' => 'first',
  21. 0 => 'second',
  22. 1 => 'third',
  23. 7 => 'fourth',
  24. ], $dolly);
  25. $dolly = $arr;
  26. Arrays::insertAfter($dolly, null, ['new' => 'value']);
  27. Assert::same([
  28. '' => 'first',
  29. 0 => 'second',
  30. 1 => 'third',
  31. 7 => 'fourth',
  32. 'new' => 'value',
  33. ], $dolly);
  34. });
  35. test('Last item', function () use ($arr) {
  36. $dolly = $arr;
  37. Arrays::insertBefore($dolly, 7, ['new' => 'value']);
  38. Assert::same([
  39. '' => 'first',
  40. 0 => 'second',
  41. 1 => 'third',
  42. 'new' => 'value',
  43. 7 => 'fourth',
  44. ], $dolly);
  45. $dolly = $arr;
  46. Arrays::insertAfter($dolly, 7, ['new' => 'value']);
  47. Assert::same([
  48. '' => 'first',
  49. 0 => 'second',
  50. 1 => 'third',
  51. 7 => 'fourth',
  52. 'new' => 'value',
  53. ], $dolly);
  54. });
  55. test('Undefined item', function () use ($arr) {
  56. $dolly = $arr;
  57. Arrays::insertBefore($dolly, 'undefined', ['new' => 'value']);
  58. Assert::same([
  59. 'new' => 'value',
  60. '' => 'first',
  61. 0 => 'second',
  62. 1 => 'third',
  63. 7 => 'fourth',
  64. ], $dolly);
  65. $dolly = $arr;
  66. Arrays::insertAfter($dolly, 'undefined', ['new' => 'value']);
  67. Assert::same([
  68. '' => 'first',
  69. 0 => 'second',
  70. 1 => 'third',
  71. 7 => 'fourth',
  72. 'new' => 'value',
  73. ], $dolly);
  74. });