|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Remorhaz\JSONPointer\Test\Pointer\Evaluate; |
| 4 | + |
| 5 | +use Remorhaz\JSONPointer\Locator; |
| 6 | +use Remorhaz\JSONPointer\Parser; |
| 7 | +use Remorhaz\JSONPointer\Pointer\Evaluate\Write; |
| 8 | + |
| 9 | +class WriteTest extends \PHPUnit_Framework_TestCase |
| 10 | +{ |
| 11 | + |
| 12 | + |
| 13 | + /** |
| 14 | + * @param string $text |
| 15 | + * @param mixed $data |
| 16 | + * @dataProvider providerSingleDataWithValidLocator |
| 17 | + * @expectedException \Remorhaz\JSONPointer\Pointer\Evaluate\Exception |
| 18 | + */ |
| 19 | + public function testPerformWithNoValueSetThrowsException($text, $data) |
| 20 | + { |
| 21 | + $locator = Parser::factory() |
| 22 | + ->setText($text) |
| 23 | + ->getLocator(); |
| 24 | + Write::factory() |
| 25 | + ->setData($data) |
| 26 | + ->setLocator($locator) |
| 27 | + ->perform(); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + /** |
| 32 | + * @param string $text |
| 33 | + * @param mixed $data |
| 34 | + * @dataProvider providerSingleDataWithValidLocator |
| 35 | + * @expectedException \LogicException |
| 36 | + */ |
| 37 | + public function testPerformWithNoValueSetThrowsSplException($text, $data) |
| 38 | + { |
| 39 | + $locator = Parser::factory() |
| 40 | + ->setText($text) |
| 41 | + ->getLocator(); |
| 42 | + Write::factory() |
| 43 | + ->setData($data) |
| 44 | + ->setLocator($locator) |
| 45 | + ->perform(); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public function providerSingleDataWithValidLocator() |
| 50 | + { |
| 51 | + return [ |
| 52 | + [ |
| 53 | + '/a/b', |
| 54 | + (object) [ |
| 55 | + 'a' => (object) ['b' => 1, 'c' => 2], |
| 56 | + ] |
| 57 | + ], |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + /** |
| 63 | + * @param string $text |
| 64 | + * @param mixed $data |
| 65 | + * @param mixed $value |
| 66 | + * @param mixed $expectedData |
| 67 | + * @dataProvider providerDataWithValidLokator |
| 68 | + */ |
| 69 | + public function testWriteDataWithValidLokator($text, $data, $value, $expectedData) |
| 70 | + { |
| 71 | + $locator = Parser::factory() |
| 72 | + ->setText($text) |
| 73 | + ->getLocator(); |
| 74 | + Write::factory() |
| 75 | + ->setData($data) |
| 76 | + ->setLocator($locator) |
| 77 | + ->setValue($value) |
| 78 | + ->perform(); |
| 79 | + $this->assertEquals($expectedData, $data, "Incorrect data after modification"); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + public function providerDataWithValidLokator() |
| 84 | + { |
| 85 | + return [ |
| 86 | + 'rootScalar' => ['', 1, 2, 2], |
| 87 | + 'rootObjectExistingProperty' => [ |
| 88 | + '/a', |
| 89 | + (object) ['a' => 1, 'b' => 2], |
| 90 | + 3, |
| 91 | + (object) ['a' => 3, 'b' => 2], |
| 92 | + ], |
| 93 | + 'rootObjectNewProperty' => [ |
| 94 | + '/c', |
| 95 | + (object) ['a' => 1, 'b' => 2], |
| 96 | + 3, |
| 97 | + (object) ['a' => 1, 'b' => 2, 'c' => 3], |
| 98 | + ], |
| 99 | + 'nestedObjectExistingProperty' => [ |
| 100 | + '/a/b', |
| 101 | + (object) [ |
| 102 | + 'a' => (object) ['b' => 1, 'c' => 2], |
| 103 | + ], |
| 104 | + 3, |
| 105 | + (object) [ |
| 106 | + 'a' => (object) ['b' => 3, 'c' => 2], |
| 107 | + ], |
| 108 | + ], |
| 109 | + 'nestedObjectNewProperty' => [ |
| 110 | + '/a/d', |
| 111 | + (object) [ |
| 112 | + 'a' => (object) ['b' => 1, 'c' => 2], |
| 113 | + ], |
| 114 | + 3, |
| 115 | + (object) [ |
| 116 | + 'a' => (object) ['b' => 1, 'c' => 2, 'd' => 3], |
| 117 | + ], |
| 118 | + ], |
| 119 | + 'rootArrayExistingIndex' => [ |
| 120 | + '/1', |
| 121 | + [1, 2, 3], |
| 122 | + 4, |
| 123 | + [1, 4, 3], |
| 124 | + ], |
| 125 | + 'rootArrayNextIndex' => [ |
| 126 | + '/-', |
| 127 | + [1, 2, 3], |
| 128 | + 4, |
| 129 | + [1, 2, 3, 4], |
| 130 | + ], |
| 131 | + 'nestedArrayExistingIndex' => [ |
| 132 | + '/1/0', |
| 133 | + [1, [3, 4], 5], |
| 134 | + 6, |
| 135 | + [1, [6, 4], 5], |
| 136 | + ], |
| 137 | + 'nestedArrayNextIndex' => [ |
| 138 | + '/1/-', |
| 139 | + [1, [3, 4], 5], |
| 140 | + 6, |
| 141 | + [1, [3, 4, 6], 5], |
| 142 | + ], |
| 143 | + ]; |
| 144 | + } |
| 145 | +} |
0 commit comments