Skip to content

Commit 8b2f466

Browse files
committed
Bugfixes and tests
- Fixed bug in writing to array - Added some tests for write evaluator
1 parent 06d9285 commit 8b2f466

File tree

6 files changed

+163
-9
lines changed

6 files changed

+163
-9
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
This library allows usage of [RFC6901](https://tools.ietf.org/html/rfc6901)-compliant JSON pointers with PHP variables.
1010

1111
##Requirements
12-
* PHP 5.6+/PHP 7.0+/HHVM
12+
* PHP 5.6+
13+
14+
##Features
15+
* Supports PHP 5.6, PHP 7.0 and HHVM
16+
* No extensions required
17+
* SPL exceptions support
1318

1419
#License
1520
PHP JSON Pointer is licensed under MIT license.

phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</testsuites>
1010
<filter>
1111
<whitelist
12-
getUncoveredFilesFromWhitelist="true"
12+
addUncoveredFilesFromWhitelist="true"
1313
processUncoveredFilesFromWhitelist="true">
1414
<directory suffix=".php">src/</directory>
1515
</whitelist>

src/Pointer/Evaluate.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ protected function processArrayIndex(Reference $reference)
276276

277277
protected function processNumericArrayIndex(Reference $reference)
278278
{
279-
$index = (int) $reference->getValue();
279+
$index = $this->getArrayIndex($reference);
280280
return array_key_exists($index, $this->cursor)
281281
? $this->processExistingArrayIndex($reference)
282282
: $this->processNonExistingArrayIndex($reference);
@@ -298,7 +298,7 @@ protected function processNotAllowedNonNumericArrayIndex(Reference $reference)
298298

299299
protected function processAllowedNonNumericArrayIndex(Reference $reference)
300300
{
301-
$index = $reference->getValue();
301+
$index = $this->getArrayIndex($reference);
302302
return array_key_exists($index, $this->cursor)
303303
? $this->processExistingArrayIndex($reference)
304304
: $this->processNonExistingArrayIndex($reference);

src/Pointer/Evaluate/Write.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,15 @@ protected function processNonExistingObjectProperty(Reference $reference)
6161

6262
protected function processNonExistingArrayIndex(Reference $reference)
6363
{
64+
$index = $this->getArrayIndex($reference);
6465
if ($reference->isLast()) {
65-
$this->cursor->{$reference->getValue()} = $this->getValue();
66+
$this->cursor[$index] = $this->getValue();
6667
$result = null;
6768
return $this->setResult($result);
6869
}
69-
throw new EvaluateException("Accessing non-existing index in array");
70+
var_export($this->cursor);
71+
var_export($index);
72+
$indexText = is_int($index) ? "{$index}" : "'{$index}'";
73+
throw new EvaluateException("Accessing non-existing index {$indexText} in array");
7074
}
7175
}

tests/Pointer/Evaluate/WriteTest.php

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}

tests/PointerTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ public function testWriteDataWithValidLocator($text, $data, $newValue, $newData)
9999
public function providerWriteDataWithValidLocator()
100100
{
101101
return [
102-
'objectProperty' => [
102+
'rootProperty' => [
103103
'/a',
104-
(object) ['a' => 1, 'b' => '2'],
104+
(object) ['a' => 1, 'b' => 2],
105105
3,
106-
(object) ['a' => 3, 'b' => '2'],
106+
(object) ['a' => 3, 'b' => 2],
107107
],
108108
];
109109
}

0 commit comments

Comments
 (0)