ArrayUtilsTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. public function testGetChanges(): void
  8. {
  9. // Non-recursive (default)
  10. $this->assertEquals(
  11. ['b' => -2, 'c' => ['d' => 4, 'e' => ['f' => -5]], 'g' => 7],
  12. ArrayUtils::getChanges(
  13. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  14. ['a' => 1, 'b' => -2, 'c' => ['d' => 4, 'e' => ['f' => -5]], 'g' => 7],
  15. )
  16. );
  17. // Recursive
  18. $this->assertEquals(
  19. ['b' => -2, 'c' => ['e' => ['f' => -5]], 'g' => 7],
  20. ArrayUtils::getChanges(
  21. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  22. ['a' => 1, 'b' => -2, 'c' => ['d' => 4, 'e' => ['f' => -5]], 'g' => 7],
  23. true
  24. )
  25. );
  26. // Recursive with unchanged sub array
  27. $this->assertEquals(
  28. ['b' => -2],
  29. ArrayUtils::getChanges(
  30. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  31. ['a' => 1, 'b' => -2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  32. true
  33. )
  34. );
  35. // No changes
  36. $this->assertEquals(
  37. [],
  38. ArrayUtils::getChanges(
  39. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  40. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  41. )
  42. );
  43. // Empty arrays
  44. $this->assertEquals(
  45. [],
  46. ArrayUtils::getChanges(
  47. [],
  48. [],
  49. )
  50. );
  51. // First array is empty
  52. $this->assertEquals(
  53. ['a' => 1],
  54. ArrayUtils::getChanges(
  55. [],
  56. ['a' => 1],
  57. )
  58. );
  59. // With callback
  60. $this->assertEquals(
  61. ['a' => 2],
  62. ArrayUtils::getChanges(
  63. ['a' => 1, 'b' => ''],
  64. ['a' => 2, 'b' => null],
  65. false,
  66. static function ($v1, $v2) { return ($v1 ?? '') === ($v2 ?? ''); }
  67. )
  68. );
  69. }
  70. }