ArrayUtilsTest.php 2.2 KB

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