DataTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 DataTest extends \PHPUnit_Framework_TestCase
  12. {
  13. protected function getSampleData()
  14. {
  15. return array(
  16. 'a' => 'A',
  17. 'b' => array(
  18. 'b' => 'B',
  19. 'c' => array('C1', 'C2', 'C3'),
  20. 'd' => array(
  21. 'd1' => 'D1',
  22. 'd2' => 'D2',
  23. 'd3' => 'D3',
  24. ),
  25. ),
  26. 'c' => array('c1', 'c2', 'c3'),
  27. 'f' => array(
  28. 'g' => array(
  29. 'h' => 'FGH',
  30. ),
  31. ),
  32. 'h' => array(
  33. 'i' => 'I',
  34. ),
  35. 'i' => array(
  36. 'j' => 'J',
  37. ),
  38. );
  39. }
  40. protected function runSampleDataTests(DataInterface $data)
  41. {
  42. $this->assertEquals('A', $data->get('a'));
  43. $this->assertEquals('B', $data->get('b.b'));
  44. $this->assertEquals(array('C1', 'C2', 'C3'), $data->get('b.c'));
  45. $this->assertEquals('D3', $data->get('b.d.d3'));
  46. $this->assertEquals(array('c1', 'c2', 'c3'), $data->get('c'));
  47. $this->assertNull($data->get('foo'), 'Foo should not exist');
  48. $this->assertNull($data->get('f.g.h.i'));
  49. $this->assertEquals($data->get('foo', 'default-value-1'), 'default-value-1', 'Return default value');
  50. $this->assertEquals($data->get('f.g.h.i', 'default-value-2'), 'default-value-2');
  51. }
  52. public function testAppend()
  53. {
  54. $data = new Data($this->getSampleData());
  55. $data->append('a', 'B');
  56. $data->append('c', 'c4');
  57. $data->append('b.c', 'C4');
  58. $data->append('b.d.d3', 'D3b');
  59. $data->append('b.d.d4', 'D');
  60. $data->append('e', 'E');
  61. $data->append('f.a', 'b');
  62. $data->append('h.i', 'I2');
  63. $data->append('i.k.l', 'L');
  64. $this->assertEquals(array('A', 'B'), $data->get('a'));
  65. $this->assertEquals(array('c1', 'c2', 'c3', 'c4'), $data->get('c'));
  66. $this->assertEquals(array('C1', 'C2', 'C3', 'C4'), $data->get('b.c'));
  67. $this->assertEquals(array('D3', 'D3b'), $data->get('b.d.d3'));
  68. $this->assertEquals(array('D'), $data->get('b.d.d4'));
  69. $this->assertEquals(array('E'), $data->get('e'));
  70. $this->assertEquals(array('b'), $data->get('f.a'));
  71. $this->assertEquals(array('I', 'I2'), $data->get('h.i'));
  72. $this->assertEquals(array('L'), $data->get('i.k.l'));
  73. $this->setExpectedException('RuntimeException');
  74. $data->append('', 'broken');
  75. }
  76. public function testSet()
  77. {
  78. $data = new Data;
  79. $this->assertNull($data->get('a'));
  80. $this->assertNull($data->get('b.c'));
  81. $this->assertNull($data->get('d.e'));
  82. $data->set('a', 'A');
  83. $data->set('b.c', 'C');
  84. $data->set('d.e', array('f' => 'F', 'g' => 'G',));
  85. $this->assertEquals('A', $data->get('a'));
  86. $this->assertEquals(array('c' => 'C'), $data->get('b'));
  87. $this->assertEquals('C', $data->get('b.c'));
  88. $this->assertEquals('F', $data->get('d.e.f'));
  89. $this->assertEquals(array('e' => array('f' => 'F', 'g' => 'G',)), $data->get('d'));
  90. $this->setExpectedException('RuntimeException');
  91. $data->set('', 'broken');
  92. }
  93. public function testSetClobberStringInPath()
  94. {
  95. $data = new Data;
  96. $data->set('a.b.c', 'Should not be able to write to a.b.c.d.e');
  97. $this->setExpectedException('RuntimeException');
  98. $data->set('a.b.c.d.e', 'broken');
  99. }
  100. public function testRemove()
  101. {
  102. $data = new Data($this->getSampleData());
  103. $data->remove('a');
  104. $data->remove('b.c');
  105. $data->remove('b.d.d3');
  106. $data->remove('d');
  107. $data->remove('d.e.f');
  108. $data->remove('empty.path');
  109. $this->assertNull($data->get('a'));
  110. $this->assertNull($data->get('b.c'));
  111. $this->assertNull($data->get('b.d.d3'));
  112. $this->assertNull(null);
  113. $this->assertEquals('D2', $data->get('b.d.d2'));
  114. $this->setExpectedException('RuntimeException');
  115. $data->remove('', 'broken');
  116. }
  117. public function testGet()
  118. {
  119. $data = new Data($this->getSampleData());
  120. $this->runSampleDataTests($data);
  121. }
  122. public function testHas()
  123. {
  124. $data = new Data($this->getSampleData());
  125. foreach (
  126. array('a', 'i', 'b.d', 'f.g.h', 'h.i', 'b.d.d1') as $existentKey
  127. ) {
  128. $this->assertTrue($data->has($existentKey));
  129. }
  130. foreach (
  131. array('p', 'b.b1', 'b.c.C1', 'h.i.I', 'b.d.d1.D1') as $notExistentKey
  132. ) {
  133. $this->assertFalse($data->has($notExistentKey));
  134. }
  135. }
  136. public function testGetData()
  137. {
  138. $wrappedData = new Data(array(
  139. 'wrapped' => array(
  140. 'sampleData' => $this->getSampleData()
  141. ),
  142. ));
  143. $data = $wrappedData->getData('wrapped.sampleData');
  144. $this->runSampleDataTests($data);
  145. $this->setExpectedException('RuntimeException');
  146. $data = $wrappedData->getData('wrapped.sampleData.a');
  147. }
  148. public function testImport()
  149. {
  150. $data = new Data();
  151. $data->import($this->getSampleData());
  152. $this->runSampleDataTests($data);
  153. }
  154. public function testImportData()
  155. {
  156. $data = new Data();
  157. $data->importData(new Data($this->getSampleData()));
  158. $this->runSampleDataTests($data);
  159. }
  160. public function testExport()
  161. {
  162. $data = new Data($this->getSampleData());
  163. $this->assertEquals($this->getSampleData(), $data->export());
  164. }
  165. }