Skip to content

Commit

Permalink
Reorganised test fixtures.
Browse files Browse the repository at this point in the history
  • Loading branch information
phptek committed Jun 10, 2016
1 parent 8777316 commit 72fad78
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 39 deletions.
18 changes: 9 additions & 9 deletions code/models/fieldtypes/JSONText.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class JSONText extends \StringField
/**
* Object cache for performance improvements.
*
* @var RecursiveIteratorIterator
* @var \RecursiveIteratorIterator
*/
protected $data;

Expand Down Expand Up @@ -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)
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -238,7 +238,7 @@ public function last()
*
* @param int $n
* @return mixed array
* @throws JSONTextException
* @throws \JSONText\Exceptions\JSONTextException
*/
public function nth($n)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down
71 changes: 41 additions & 30 deletions tests/JSONTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -94,41 +94,41 @@ 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'));
}

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'));
}
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -170,25 +170,36 @@ 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'));
$this->assertEquals([], $field->query('->', '6')); // strict handling

// 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]);
}

}
Empty file added tests/fixtures/json/empty.json
Empty file.
19 changes: 19 additions & 0 deletions tests/fixtures/json/hash_deep.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"cars":{
"american":
[
"buick",
"oldsmobile"
],
"british":
["vauxhall","morris"]
},
"planes":{
"russian":
[
"antonov",
"mig"
],
"french":"airbus"
}
}
18 changes: 18 additions & 0 deletions tests/fixtures/json/hash_duplicates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"cars":{
"american":
[
"buick",
"oldsmobile"
],
"british":
[
"vauxhall",
"morris"
]
},
"planes":{
"british":"airbus",
"french":"airbus"
}
}
13 changes: 13 additions & 0 deletions tests/fixtures/json/hash_simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"chinese": "great wall",
"american": [
"buick",
"oldsmobile",
"ford"
],
"british":
[
"vauxhall",
"morris"
]
}
9 changes: 9 additions & 0 deletions tests/fixtures/json/indexed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
"great wall",
"ford",
"trabant",
"oldsmobile",
"buick",
"vauxhall",
"morris"
]
13 changes: 13 additions & 0 deletions tests/fixtures/json/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"chinese":
"great wall",
"american":
[
"buick",
"oldsmobile",
"ford"
],
"british":
[
"vauxhall",
"morris"

0 comments on commit 72fad78

Please sign in to comment.