diff --git a/code/models/fieldtypes/JSONText.php b/code/models/fieldtypes/JSONText.php index 83bd662..e55ddb9 100644 --- a/code/models/fieldtypes/JSONText.php +++ b/code/models/fieldtypes/JSONText.php @@ -58,7 +58,7 @@ class JSONText extends \StringField /** * Object cache for performance improvements. * - * @var RecursiveIteratorIterator + * @var \RecursiveIteratorIterator */ protected $data; @@ -118,8 +118,8 @@ public function scaffoldFormField($title = null) * Tell all class methods to return data as JSON or an array. * * @param string $type - * @return JSONText - * @throws JSONTextException + * @return \JSONText + * @throws \JSONText\Exceptions\JSONTextException */ public function setReturnType($type) { @@ -142,8 +142,8 @@ public function getReturnType() /** * Returns the value of this field as an iterable. * - * @return RecursiveIteratorIterator - * @throws JSONTextException + * @return \RecursiveIteratorIterator + * @throws \JSONText\Exceptions\JSONTextException */ public function getValueAsIterable() { @@ -238,7 +238,7 @@ public function last() * * @param int $n * @return mixed array - * @throws JSONTextException + * @throws \JSONText\Exceptions\JSONTextException */ public function nth($n) { @@ -271,7 +271,7 @@ public function nth($n) * @param string $operator * @param string $operand * @return mixed null|array - * @throws JSONTextException + * @throws \JSONText\Exceptions\JSONTextException * @todo How to increment an interator for each depth using $data->getDepth() and $i ?? */ public function query($operator, $operand) @@ -304,7 +304,7 @@ public function query($operator, $operand) * * @param string $operator * @return mixed string|array - * @throws JSONTextException + * @throws \JSONText\Exceptions\JSONTextException */ public function extract($operator) { @@ -317,7 +317,7 @@ public function extract($operator) * @param int $idx * @param array $args * @return array - * @throws JSONTextException + * @throws \JSONText\Exceptions\JSONTextException */ private function marshallQuery($key, $val, $idx, $args) { diff --git a/tests/JSONTextTest.php b/tests/JSONTextTest.php index 06ad37f..52a725a 100644 --- a/tests/JSONTextTest.php +++ b/tests/JSONTextTest.php @@ -12,38 +12,38 @@ class JSONTextTest extends SapphireTest { /** - * @var string + * @var array */ - protected $fixture = [ - 'indexed' => '["great wall", "ford", "trabant", "oldsmobile", "buick", "vauxhall", "morris"]', - 'hashed' => '{"chinese":"great wall","american":["buick","oldsmobile","ford"],"british":["vauxhall","morris"]}', - 'invalid' => '{"chinese":"great wall","american":["buick","oldsmobile","ford"],"british":["vauxhall","morris]', - 'nested' => '{"cars":{"american":["buick","oldsmobile"],"british":["vauxhall","morris"]},"planes":{"russian":["antonov","mig"],"french":"airbus"}}', - 'multiple' => '{"cars":{"american":["buick","oldsmobile"],"british":["vauxhall","morris"]},"planes":{"british":"airbus","french":"airbus"}}', - 'empty' => '' + protected $fixtures = [ + 'indexed' => MODULE_DIR . '/tests/fixtures/json/indexed.json', + 'hash_simple' => MODULE_DIR . '/tests/fixtures/json/hash_simple.json', + 'invalid' => MODULE_DIR . '/tests/fixtures/json/invalid.json', + 'hash_deep' => MODULE_DIR . '/tests/fixtures/json/hash_deep.json', + 'hash_dupes' => MODULE_DIR . '/tests/fixtures/json/hash_duplicates.json', + 'empty' => MODULE_DIR . '/tests/fixtures/json/empty.json' ]; public function testgetValueAsIterable() { $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['invalid']); + $field->setValue($this->getFixture('invalid')); $this->setExpectedException('\JSONText\Exceptions\JSONTextException'); $this->assertEquals(['chinese' => 'great wall'], $field->getValueAsIterable()); $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['empty']); + $field->setValue($this->getFixture('empty')); $this->assertEquals([], $field->getValueAsIterable()); } public function testFirst_AsArray() { $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['indexed']); + $field->setValue($this->getFixture('indexed')); $field->setReturnType('array'); $this->assertEquals([0 => 'great wall'], $field->first()); $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['empty']); + $field->setValue($this->getFixture('empty')); $field->setReturnType('array'); $this->assertInternalType('array', $field->first()); $this->assertCount(0, $field->first()); @@ -52,12 +52,12 @@ public function testFirst_AsArray() public function testFirst_AsJson() { $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['indexed']); + $field->setValue($this->getFixture('indexed')); $field->setReturnType('json'); $this->assertEquals('["great wall"]', $field->first()); $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['empty']); + $field->setValue($this->getFixture('empty')); $field->setReturnType('json'); $this->assertInternalType('string', $field->first()); $this->assertEquals('[]', $field->first()); @@ -66,12 +66,12 @@ public function testFirst_AsJson() public function testLast_AsArray() { $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['indexed']); + $field->setValue($this->getFixture('indexed')); $field->setReturnType('array'); $this->assertEquals([6 => 'morris'], $field->last()); $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['empty']); + $field->setValue($this->getFixture('empty')); $field->setReturnType('array'); $this->assertInternalType('array', $field->first()); $this->assertCount(0, $field->first()); @@ -80,12 +80,12 @@ public function testLast_AsArray() public function testLast_AsJson() { $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['indexed']); + $field->setValue($this->getFixture('indexed')); $field->setReturnType('json'); $this->assertEquals('{"6":"morris"}', $field->last()); $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['empty']); + $field->setValue($this->getFixture('empty')); $field->setReturnType('json'); $this->assertInternalType('string', $field->first()); $this->assertEquals('[]', $field->first()); @@ -94,20 +94,20 @@ public function testLast_AsJson() public function testNth_AsArray() { $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['indexed']); + $field->setValue($this->getFixture('indexed')); $field->setReturnType('array'); $this->assertEquals([0 => 'great wall'], $field->nth(0)); $this->assertEquals([2 => 'trabant'], $field->nth(2)); $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['empty']); + $field->setValue($this->getFixture('empty')); $field->setReturnType('array'); $this->assertInternalType('array', $field->first()); $this->assertCount(0, $field->first()); $this->setExpectedException('\JSONText\Exceptions\JSONTextException'); $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['hashed']); + $field->setValue($this->getFixture('hash_simple')); $field->setReturnType('array'); $this->assertEquals(['british' => ['vauxhall', 'morris']], $field->nth('2')); } @@ -115,20 +115,20 @@ public function testNth_AsArray() public function testNth_AsJson() { $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['indexed']); + $field->setValue($this->getFixture('indexed')); $field->setReturnType('json'); $this->assertEquals('["great wall"]', $field->nth(0)); $this->assertEquals('{"2":"trabant"}', $field->nth(2)); $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['empty']); + $field->setValue($this->getFixture('empty')); $field->setReturnType('json'); $this->assertInternalType('string', $field->first()); $this->assertEquals('[]', $field->first()); $this->setExpectedException('\JSONText\Exceptions\JSONTextException'); $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['hashed']); + $field->setValue($this->getFixture('hash_simple')); $field->setReturnType('json'); $this->assertEquals('{"british":["vauxhall","morris"]}', $field->nth('2')); } @@ -140,7 +140,7 @@ public function testquery_AsInt_AsArray() { // Hashed $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['hashed']); + $field->setValue($this->getFixture('hash_simple')); $field->setReturnType('array'); $this->assertEquals(['british' => ['vauxhall', 'morris']], $field->query('->', 5)); @@ -149,13 +149,13 @@ public function testquery_AsInt_AsArray() // Empty $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['empty']); + $field->setValue($this->getFixture('empty')); $field->setReturnType('array'); $this->assertEquals([], $field->query('->', 42)); // Nested $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['nested']); + $field->setValue($this->getFixture('hash_deep')); $field->setReturnType('array'); $this->assertEquals(['planes' => ['russian' => ['antonov', 'mig'], 'french' => 'airbus']], $field->query('->', 7)); @@ -170,7 +170,7 @@ public function testquery_AsStr_AsArray() { // Hashed $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['hashed']); + $field->setValue($this->getFixture('hash_simple')); $field->setReturnType('array'); $this->assertEquals(['british' => ['vauxhall', 'morris']], $field->query('->>', 'british')); @@ -178,17 +178,28 @@ public function testquery_AsStr_AsArray() // Empty $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['empty']); + $field->setValue($this->getFixture('empty')); $field->setReturnType('array'); $this->assertEquals([], $field->query('->>', 'british')); // Nested $field = JSONText\Fields\JSONText::create('MyJSON'); - $field->setValue($this->fixture['nested']); + $field->setValue($this->getFixture('hash_deep')); $field->setReturnType('array'); $this->assertEquals(['planes' => ['russian' => ['antonov', 'mig'], 'french' => 'airbus']], $field->query('->>', 'planes')); $this->assertEquals([], $field->query('->', '7')); // Attempt to match a string using the int matcher } + + /** + * Get the contents of a fixture + * + * @return string + */ + private function getFixture($fixture) + { + $files = $this->fixtures; + return file_get_contents($files[$fixture]); + } } diff --git a/tests/fixtures/json/empty.json b/tests/fixtures/json/empty.json new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/json/hash_deep.json b/tests/fixtures/json/hash_deep.json new file mode 100644 index 0000000..35e48d1 --- /dev/null +++ b/tests/fixtures/json/hash_deep.json @@ -0,0 +1,19 @@ +{ + "cars":{ + "american": + [ + "buick", + "oldsmobile" + ], + "british": + ["vauxhall","morris"] + }, + "planes":{ + "russian": + [ + "antonov", + "mig" + ], + "french":"airbus" + } +} \ No newline at end of file diff --git a/tests/fixtures/json/hash_duplicates.json b/tests/fixtures/json/hash_duplicates.json new file mode 100644 index 0000000..289f38f --- /dev/null +++ b/tests/fixtures/json/hash_duplicates.json @@ -0,0 +1,18 @@ +{ + "cars":{ + "american": + [ + "buick", + "oldsmobile" + ], + "british": + [ + "vauxhall", + "morris" + ] + }, + "planes":{ + "british":"airbus", + "french":"airbus" + } +} \ No newline at end of file diff --git a/tests/fixtures/json/hash_simple.json b/tests/fixtures/json/hash_simple.json new file mode 100644 index 0000000..6d5d633 --- /dev/null +++ b/tests/fixtures/json/hash_simple.json @@ -0,0 +1,13 @@ +{ + "chinese": "great wall", + "american": [ + "buick", + "oldsmobile", + "ford" + ], + "british": + [ + "vauxhall", + "morris" + ] +} \ No newline at end of file diff --git a/tests/fixtures/json/indexed.json b/tests/fixtures/json/indexed.json new file mode 100644 index 0000000..f1d51d8 --- /dev/null +++ b/tests/fixtures/json/indexed.json @@ -0,0 +1,9 @@ +[ + "great wall", + "ford", + "trabant", + "oldsmobile", + "buick", + "vauxhall", + "morris" +] \ No newline at end of file diff --git a/tests/fixtures/json/invalid.json b/tests/fixtures/json/invalid.json new file mode 100644 index 0000000..7acd045 --- /dev/null +++ b/tests/fixtures/json/invalid.json @@ -0,0 +1,13 @@ +{ + "chinese": + "great wall", + "american": + [ + "buick", + "oldsmobile", + "ford" + ], + "british": + [ + "vauxhall", + "morris" \ No newline at end of file