Skip to content

Commit c409bc3

Browse files
authored
Merge pull request #17 from GitHubHubus/tests
Add test for several methods
2 parents 0354bf9 + bebbebc commit c409bc3

File tree

4 files changed

+144
-18
lines changed

4 files changed

+144
-18
lines changed

src/JsonQueriable.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Nahid\JsonQ\Exceptions\ConditionNotAllowedException;
66
use Nahid\JsonQ\Exceptions\FileNotFoundException;
7+
use Nahid\JsonQ\Exceptions\InvalidJsonException;
78

89
trait JsonQueriable
910
{
@@ -118,9 +119,9 @@ protected function prepare()
118119
}
119120

120121
/**
121-
* parse object to array
122+
* Parse object to array
122123
*
123-
* @param $obj object
124+
* @param object $obj
124125
* @return array|mixed
125126
*/
126127
protected function objectToArray($obj)
@@ -141,9 +142,9 @@ protected function objectToArray($obj)
141142
}
142143

143144
/**
144-
* check given value is multidimensional array
145+
* Check given value is multidimensional array
145146
*
146-
* @param $arr array
147+
* @param array $arr
147148
* @return bool
148149
*/
149150
protected function isMultiArray($arr)
@@ -158,16 +159,26 @@ protected function isMultiArray($arr)
158159
}
159160

160161
/**
161-
* check given value is valid JSON
162-
* @param $value string
163-
* @param $return_map bool
164-
* @return bool|array|string
162+
* Check given value is valid JSON
163+
*
164+
* @param string $value
165+
* @param bool $isReturnMap
166+
*
167+
* @return bool|array
165168
*/
166-
public function isJson($value, $return_map = false)
169+
public function isJson($value, $isReturnMap = false)
167170
{
171+
if (is_array($value) || is_object($value)) {
172+
return false;
173+
}
174+
168175
$data = json_decode($value, true);
169176

170-
return (json_last_error() == JSON_ERROR_NONE) ? ($return_map ? $data : true) : json_last_error_msg();
177+
if (json_last_error() !== JSON_ERROR_NONE) {
178+
return false;
179+
}
180+
181+
return $isReturnMap ? $data : true;
171182
}
172183

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

211222
$context = stream_context_create($opts);
212-
213223
$data = file_get_contents($file, 0, $context);
214-
215-
return $this->isJson($data, true);
224+
$json = $this->isJson($data, true);
225+
226+
if (!$json) {
227+
throw new InvalidJsonException();
228+
}
229+
230+
return $json;
216231
}
217232

218233
throw new FileNotFoundException();

src/Jsonq.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,10 @@ public function then($node)
523523
*/
524524
public function json($data)
525525
{
526-
if (is_string($data)) {
527-
if ($json = $this->isJson($data, true)) {
528-
return $this->collect($json);
529-
}
526+
$json = $this->isJson($data, true);
527+
528+
if ($json) {
529+
return $this->collect($json);
530530
}
531531

532532
return $this;

tests/AbstractTestCase.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Nahid\JsonQ\Tests;
4+
5+
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
6+
{
7+
/**
8+
* Make private and protected function callable
9+
*
10+
* @param mixed $object
11+
* @param string $function
12+
* @return \ReflectionMethod
13+
*/
14+
protected function makeCallable($object, $function)
15+
{
16+
$method = new \ReflectionMethod($object, $function);
17+
$method->setAccessible(true);
18+
19+
return $method;
20+
}
21+
}

tests/JsonQueriableTest.php

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Nahid\JsonQ\Jsonq;
66
use Nahid\JsonQ\Exceptions\FileNotFoundException;
77

8-
class JsonQueriableTest extends \PHPUnit_Framework_TestCase
8+
class JsonQueriableTest extends AbstractTestCase
99
{
1010
const FILE_NAME = 'data.json';
1111

@@ -68,6 +68,19 @@ protected function removeFile()
6868
$this->file = null;
6969
}
7070

71+
/**
72+
* @return \StdClass
73+
*/
74+
private function getTestObject()
75+
{
76+
$object = new \stdClass();
77+
$object->testField = 'test';
78+
$object->test_field2 = 'test2';
79+
$object->testfield3 = 'test3';
80+
81+
return $object;
82+
}
83+
7184
protected function setUp()
7285
{
7386
$this->createFile();
@@ -95,6 +108,71 @@ public function testImport($file, $result)
95108
}
96109
}
97110

111+
/**
112+
* @param mixed $input
113+
* @param mixed $result
114+
*
115+
* @dataProvider objectToArrayProvider
116+
*/
117+
public function testObjectToArray($input, $result)
118+
{
119+
$method = $this->makeCallable($this->jsonq, 'objectToArray');
120+
121+
$this->assertEquals($result, $method->invokeArgs($this->jsonq, [$input]));
122+
}
123+
124+
/**
125+
* @param mixed $input
126+
* @param bool $result
127+
*
128+
* @dataProvider isMultiArrayProvider
129+
*/
130+
public function testIsMultiArray($input, $result)
131+
{
132+
$method = $this->makeCallable($this->jsonq, 'isMultiArray');
133+
134+
$this->assertEquals($result, $method->invokeArgs($this->jsonq, [$input]));
135+
}
136+
137+
/**
138+
* @param mixed $input
139+
* @param bool $isReturnMap
140+
* @param mixed $result
141+
*
142+
* @dataProvider isJsonProvider
143+
*/
144+
public function testIsJson($input, $isReturnMap, $result = null)
145+
{
146+
$this->assertEquals($result, $this->jsonq->isJson($input, $isReturnMap));
147+
}
148+
149+
public function isJsonProvider()
150+
{
151+
return [
152+
[null, false, false],
153+
[true, false, true],
154+
[1, false, true],
155+
[new \StdClass(), false, false],
156+
[['test'], false, false],
157+
['invalid_json_string', false, false],
158+
[json_encode('valid_json_string'), false, true],
159+
[json_encode('valid_json_string'), true, 'valid_json_string']
160+
];
161+
}
162+
163+
public function isMultiArrayProvider()
164+
{
165+
return [
166+
[null, false],
167+
[true, false],
168+
[1, false],
169+
['test', false],
170+
[['test', 'test'], false],
171+
[['test',['test']], true],
172+
[[['test'], 'test'], true]
173+
];
174+
}
175+
98176
public function importProvider()
99177
{
100178
return [
@@ -105,4 +183,16 @@ public function importProvider()
105183
['invalid_path.json', false]
106184
];
107185
}
186+
187+
public function objectToArrayProvider()
188+
{
189+
return [
190+
[null, null],
191+
[true, true],
192+
[1, 1],
193+
['test', 'test'],
194+
[['data1', 'data2'],['data1', 'data2']],
195+
[$this->getTestObject(), ['testField' => 'test', 'test_field2' => 'test2', 'testfield3' => 'test3']]
196+
];
197+
}
108198
}

0 commit comments

Comments
 (0)