UtilTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is a part of dflydev/dot-access-data.
  4. *
  5. * (c) Dragonfly Development Inc.
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Dflydev\DotAccessData;
  11. class UtilTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testIsAssoc()
  14. {
  15. $this->assertTrue(Util::isAssoc(array('a' => 'A',)));
  16. $this->assertTrue(Util::isAssoc(array()));
  17. $this->assertFalse(Util::isAssoc(array(1 => 'One',)));
  18. }
  19. /**
  20. * @dataProvider mergeAssocArrayProvider
  21. */
  22. public function testMergeAssocArray($message, $to, $from, $clobber, $expectedResult)
  23. {
  24. $result = Util::mergeAssocArray($to, $from, $clobber);
  25. $this->assertEquals($expectedResult, $result, $message);
  26. }
  27. public function mergeAssocArrayProvider()
  28. {
  29. return array(
  30. array(
  31. 'Clobber should replace to value with from value for strings (shallow)',
  32. // to
  33. array('a' => 'A'),
  34. // from
  35. array('a' => 'B'),
  36. // clobber
  37. true,
  38. // expected result
  39. array('a' => 'B'),
  40. ),
  41. array(
  42. 'Clobber should replace to value with from value for strings (deep)',
  43. // to
  44. array('a' => array('b' => 'B',),),
  45. // from
  46. array('a' => array('b' => 'C',),),
  47. // clobber
  48. true,
  49. // expected result
  50. array('a' => array('b' => 'C',),),
  51. ),
  52. array(
  53. 'Clobber should NOTreplace to value with from value for strings (shallow)',
  54. // to
  55. array('a' => 'A'),
  56. // from
  57. array('a' => 'B'),
  58. // clobber
  59. false,
  60. // expected result
  61. array('a' => 'A'),
  62. ),
  63. array(
  64. 'Clobber should NOT replace to value with from value for strings (deep)',
  65. // to
  66. array('a' => array('b' => 'B',),),
  67. // from
  68. array('a' => array('b' => 'C',),),
  69. // clobber
  70. false,
  71. // expected result
  72. array('a' => array('b' => 'B',),),
  73. ),
  74. array(
  75. 'Associative arrays should be combined',
  76. // to
  77. array('a' => array('b' => 'B',),),
  78. // from
  79. array('a' => array('c' => 'C',),),
  80. // clobber
  81. null,
  82. // expected result
  83. array('a' => array('b' => 'B', 'c' => 'C',),),
  84. ),
  85. array(
  86. 'Arrays should be replaced (with clobber enabled)',
  87. // to
  88. array('a' => array('b', 'c',)),
  89. // from
  90. array('a' => array('B', 'C',),),
  91. // clobber
  92. true,
  93. // expected result
  94. array('a' => array('B', 'C',),),
  95. ),
  96. array(
  97. 'Arrays should be NOT replaced (with clobber disabled)',
  98. // to
  99. array('a' => array('b', 'c',)),
  100. // from
  101. array('a' => array('B', 'C',),),
  102. // clobber
  103. false,
  104. // expected result
  105. array('a' => array('b', 'c',),),
  106. ),
  107. );
  108. }
  109. }