Skip to content

Commit

Permalink
Merge pull request #17 from GitHubHubus/tests
Browse files Browse the repository at this point in the history
Add test for several methods
  • Loading branch information
nahid authored May 27, 2018
2 parents 0354bf9 + bebbebc commit c409bc3
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 18 deletions.
41 changes: 28 additions & 13 deletions src/JsonQueriable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Nahid\JsonQ\Exceptions\ConditionNotAllowedException;
use Nahid\JsonQ\Exceptions\FileNotFoundException;
use Nahid\JsonQ\Exceptions\InvalidJsonException;

trait JsonQueriable
{
Expand Down Expand Up @@ -118,9 +119,9 @@ protected function prepare()
}

/**
* parse object to array
* Parse object to array
*
* @param $obj object
* @param object $obj
* @return array|mixed
*/
protected function objectToArray($obj)
Expand All @@ -141,9 +142,9 @@ protected function objectToArray($obj)
}

/**
* check given value is multidimensional array
* Check given value is multidimensional array
*
* @param $arr array
* @param array $arr
* @return bool
*/
protected function isMultiArray($arr)
Expand All @@ -158,16 +159,26 @@ protected function isMultiArray($arr)
}

/**
* check given value is valid JSON
* @param $value string
* @param $return_map bool
* @return bool|array|string
* Check given value is valid JSON
*
* @param string $value
* @param bool $isReturnMap
*
* @return bool|array
*/
public function isJson($value, $return_map = false)
public function isJson($value, $isReturnMap = false)
{
if (is_array($value) || is_object($value)) {
return false;
}

$data = json_decode($value, true);

return (json_last_error() == JSON_ERROR_NONE) ? ($return_map ? $data : true) : json_last_error_msg();
if (json_last_error() !== JSON_ERROR_NONE) {
return false;
}

return $isReturnMap ? $data : true;
}

/**
Expand Down Expand Up @@ -209,10 +220,14 @@ protected function getDataFromFile($file, $type = 'application/json')
];

$context = stream_context_create($opts);

$data = file_get_contents($file, 0, $context);

return $this->isJson($data, true);
$json = $this->isJson($data, true);

if (!$json) {
throw new InvalidJsonException();
}

return $json;
}

throw new FileNotFoundException();
Expand Down
8 changes: 4 additions & 4 deletions src/Jsonq.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,10 @@ public function then($node)
*/
public function json($data)
{
if (is_string($data)) {
if ($json = $this->isJson($data, true)) {
return $this->collect($json);
}
$json = $this->isJson($data, true);

if ($json) {
return $this->collect($json);
}

return $this;
Expand Down
21 changes: 21 additions & 0 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Nahid\JsonQ\Tests;

abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
{
/**
* Make private and protected function callable
*
* @param mixed $object
* @param string $function
* @return \ReflectionMethod
*/
protected function makeCallable($object, $function)
{
$method = new \ReflectionMethod($object, $function);
$method->setAccessible(true);

return $method;
}
}
92 changes: 91 additions & 1 deletion tests/JsonQueriableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Nahid\JsonQ\Jsonq;
use Nahid\JsonQ\Exceptions\FileNotFoundException;

class JsonQueriableTest extends \PHPUnit_Framework_TestCase
class JsonQueriableTest extends AbstractTestCase
{
const FILE_NAME = 'data.json';

Expand Down Expand Up @@ -68,6 +68,19 @@ protected function removeFile()
$this->file = null;
}

/**
* @return \StdClass
*/
private function getTestObject()
{
$object = new \stdClass();
$object->testField = 'test';
$object->test_field2 = 'test2';
$object->testfield3 = 'test3';

return $object;
}

protected function setUp()
{
$this->createFile();
Expand Down Expand Up @@ -95,6 +108,71 @@ public function testImport($file, $result)
}
}

/**
* @param mixed $input
* @param mixed $result
*
* @dataProvider objectToArrayProvider
*/
public function testObjectToArray($input, $result)
{
$method = $this->makeCallable($this->jsonq, 'objectToArray');

$this->assertEquals($result, $method->invokeArgs($this->jsonq, [$input]));
}

/**
* @param mixed $input
* @param bool $result
*
* @dataProvider isMultiArrayProvider
*/
public function testIsMultiArray($input, $result)
{
$method = $this->makeCallable($this->jsonq, 'isMultiArray');

$this->assertEquals($result, $method->invokeArgs($this->jsonq, [$input]));
}

/**
* @param mixed $input
* @param bool $isReturnMap
* @param mixed $result
*
* @dataProvider isJsonProvider
*/
public function testIsJson($input, $isReturnMap, $result = null)
{
$this->assertEquals($result, $this->jsonq->isJson($input, $isReturnMap));
}

public function isJsonProvider()
{
return [
[null, false, false],
[true, false, true],
[1, false, true],
[new \StdClass(), false, false],
[['test'], false, false],
['invalid_json_string', false, false],
[json_encode('valid_json_string'), false, true],
[json_encode('valid_json_string'), true, 'valid_json_string']
];
}

public function isMultiArrayProvider()
{
return [
[null, false],
[true, false],
[1, false],
['test', false],
[['test', 'test'], false],
[['test',['test']], true],
[[['test'], 'test'], true]
];
}

public function importProvider()
{
return [
Expand All @@ -105,4 +183,16 @@ public function importProvider()
['invalid_path.json', false]
];
}

public function objectToArrayProvider()
{
return [
[null, null],
[true, true],
[1, 1],
['test', 'test'],
[['data1', 'data2'],['data1', 'data2']],
[$this->getTestObject(), ['testField' => 'test', 'test_field2' => 'test2', 'testfield3' => 'test3']]
];
}
}

0 comments on commit c409bc3

Please sign in to comment.