Skip to content

Commit

Permalink
Fixes based on mutation testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tburry committed Sep 7, 2019
1 parent 7f3c15e commit a5a03b8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
12 changes: 5 additions & 7 deletions src/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,13 @@ private function resolveReference($ref, $context, $root, bool &$found = null) {
$context = $root;
}

if (!is_array($context)) {
$found = false;
return null;
}

$parts = self::explodeRef($ref);
$result = $context;
foreach ($parts as $key) {
if (array_key_exists($key, $result)) {
if (!is_array($result)) {
$found = false;
return null;
} elseif (array_key_exists($key, $result)) {
$result = $result[$key];
} else {
$found = false;
Expand All @@ -146,7 +144,7 @@ private static function escapeRef(string $field): string {
* @param string $str The segment to unescapeRef.
* @return string Returns the unescaped string.
*/
public static function unescapeRef(string $str): string {
private static function unescapeRef(string $str): string {
return str_replace(['~2', '~1', '~0'], ['$', '/', '~'], $str);
}

Expand Down
29 changes: 24 additions & 5 deletions tests/BasicTransformsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public function testTransform(array $spec, array $expected) {
'bar' => 'foo',
],
'a~b' => 'c',
'a/b' => 'c',
'a$b' => 'c',
'a/b' => 'd',
'a$b' => 'e',
'~/$' => 'f'
];

$t = new Transformer($spec);
Expand All @@ -51,12 +52,14 @@ public function provideTransformTests(): array {
'basic ref' => [['foo' => 'nested/bar'], ['foo' => 'foo']],
'basic nested' => [['nested' => ['foo' => '/foo']], ['nested' => ['foo' => 'baz']]],
'escape ~' => [['a' => 'a~0b'], ['a' => 'c']],
'escape /' => [['a' => 'a~1b'], ['a' => 'c']],
'escape $' => [['a' => 'a~2b'], ['a' => 'c']],
'escape /' => [['a' => 'a~1b'], ['a' => 'd']],
'escape $' => [['a' => 'a~2b'], ['a' => 'e']],
'escape all' => [['a' => '~0~1~2'], ['a' => 'f']],
'not found' => [['a' => 'xdsd'], []],
'not found default' => [['a' => ['$ref' => 'xdsd', '$default' => 'b']], ['a' => 'b']],
'literal' => [['a' => ['$literal' => 'abc']], ['a' => 'abc']],

'escaped dest' => [['a/b' => 'foo'], ['a/b' => 'baz']],
'ref to non-array' => [['a' => 'bar/baz'], []],
];

return $r;
Expand All @@ -67,6 +70,7 @@ public function provideTransformTests(): array {
*
* @expectedException \Garden\JSON\InvalidSpecException
* @expectedExceptionMessageRegExp `^Invalid spec value`
* @expectedExceptionCode 500
*/
public function testInvalidSpec() {
$t = new Transformer(['foo' => true]);
Expand All @@ -79,6 +83,7 @@ public function testInvalidSpec() {
*
* @expectedException \Garden\JSON\InvalidSpecException
* @expectedExceptionMessageRegExp `^Invalid control expression`
* @expectedExceptionCode 500
*/
public function testInvalidControlExpression() {
$t = new Transformer(['$foo' => 'bar']);
Expand Down Expand Up @@ -112,9 +117,23 @@ public function testNumericRelativeArray() {
$this->assertSame(['b', 'a'], $actual);
}

/**
* Transformer should be callable on a non-array.
*/
public function testNonArrayContext() {
$t = new Transformer('/foo');
$actual = $t('baz');
$this->assertSame(null, $actual);
}

/**
* Paths should be escaped in error messages.
*
* @expectedException \Garden\JSON\InvalidSpecException
* @expectedExceptionMessage Invalid spec value at /~0~1~2.
*/
public function testEscapePath() {
$t = new Transformer(['~/$' => true]);
$t(['~/$' => 'a']);
}
}

0 comments on commit a5a03b8

Please sign in to comment.