ArrayUtilsTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Tests\Service\Utils;
  3. use App\Service\Utils\ArrayUtils;
  4. use PHPUnit\Framework\TestCase;
  5. class ArrayUtilsTest extends TestCase
  6. {
  7. /**
  8. * @øee ArrayUtils::getChanges()
  9. */
  10. public function testGetChanges(): void
  11. {
  12. $arrayUtils = new ArrayUtils();
  13. // Non-recursive (default)
  14. $this->assertEquals(
  15. ['b' => -2, 'c' => ['d' => 4, 'e' => ['f' => -5]], 'g' => 7],
  16. $arrayUtils->getChanges(
  17. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  18. ['a' => 1, 'b' => -2, 'c' => ['d' => 4, 'e' => ['f' => -5]], 'g' => 7],
  19. )
  20. );
  21. // Recursive
  22. $this->assertEquals(
  23. ['b' => -2, 'c' => ['e' => ['f' => -5]], 'g' => 7],
  24. $arrayUtils->getChanges(
  25. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  26. ['a' => 1, 'b' => -2, 'c' => ['d' => 4, 'e' => ['f' => -5]], 'g' => 7],
  27. true
  28. )
  29. );
  30. // Recursive with unchanged sub array
  31. $this->assertEquals(
  32. ['b' => -2],
  33. $arrayUtils->getChanges(
  34. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  35. ['a' => 1, 'b' => -2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  36. true
  37. )
  38. );
  39. // No changes
  40. $this->assertEquals(
  41. [],
  42. $arrayUtils->getChanges(
  43. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  44. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  45. )
  46. );
  47. // Empty arrays
  48. $this->assertEquals(
  49. [],
  50. $arrayUtils->getChanges(
  51. [],
  52. [],
  53. )
  54. );
  55. // First array is empty
  56. $this->assertEquals(
  57. ['a' => 1],
  58. $arrayUtils->getChanges(
  59. [],
  60. ['a' => 1],
  61. )
  62. );
  63. // With callback
  64. $this->assertEquals(
  65. ['a' => 2],
  66. $arrayUtils->getChanges(
  67. ['a' => 1, 'b' => ''],
  68. ['a' => 2, 'b' => null],
  69. false,
  70. static function ($v1, $v2) { return ($v1 ?? '') === ($v2 ?? ''); }
  71. )
  72. );
  73. }
  74. }