Skip to content

Commit 0354bf9

Browse files
authored
Merge pull request #16 from GitHubHubus/tests
Created JsonQueriableTest class and corrected import method (added checking that file name is string)
2 parents 7d81ef5 + 176b0cb commit 0354bf9

File tree

2 files changed

+113
-5
lines changed

2 files changed

+113
-5
lines changed

src/JsonQueriable.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ trait JsonQueriable
7373
/**
7474
* import data from file
7575
*
76-
* @param $json_file string
76+
* @param string $file
7777
* @return bool
7878
* @throws FileNotFoundException
7979
*/
80-
public function import($json_file = null)
80+
public function import($file = null)
8181
{
82-
if (!is_null($json_file)) {
83-
if (file_exists($json_file)) {
84-
$this->_map = $this->getDataFromFile($json_file);
82+
if (!is_null($file)) {
83+
if (is_string($file) && file_exists($file)) {
84+
$this->_map = $this->getDataFromFile($file);
8585
$this->_baseContents = $this->_map;
8686
return true;
8787
}

tests/JsonQueriableTest.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Nahid\JsonQ\Tests;
4+
5+
use Nahid\JsonQ\Jsonq;
6+
use Nahid\JsonQ\Exceptions\FileNotFoundException;
7+
8+
class JsonQueriableTest extends \PHPUnit_Framework_TestCase
9+
{
10+
const FILE_NAME = 'data.json';
11+
12+
/**
13+
* @var Jsonq
14+
*/
15+
protected $jsonq;
16+
17+
/**
18+
* @var string
19+
*/
20+
protected $file;
21+
22+
protected function createFile()
23+
{
24+
$json = [
25+
'level1.1' => [
26+
'level2.1' => [
27+
'level3.1' => 'data31',
28+
'level3.2' => 32,
29+
'level3.3' => true,
30+
],
31+
'level2.2' => [
32+
'level3.4' => 'data34',
33+
'level3.5' => 35,
34+
'level3.6' => false,
35+
],
36+
'level2.3' => [
37+
'level3.7' => 'data37',
38+
'level3.8' => 38,
39+
'level3.9' => true,
40+
]
41+
],
42+
'level1.2' => [
43+
'level2.4' => [
44+
'level3.10' => 'data310',
45+
'level3.11' => 311,
46+
'level3.12' => true,
47+
],
48+
'level2.5' => [
49+
'level3.13' => 'data313',
50+
'level3.14' => 314,
51+
'level3.15' => false,
52+
],
53+
'level2.6' => [
54+
'level3.16' => 'data316',
55+
'level3.17' => 317,
56+
'level3.18' => true,
57+
]
58+
]
59+
];
60+
61+
file_put_contents(self::FILE_NAME, json_encode($json));
62+
$this->file = self::FILE_NAME;
63+
}
64+
65+
protected function removeFile()
66+
{
67+
unlink(self::FILE_NAME);
68+
$this->file = null;
69+
}
70+
71+
protected function setUp()
72+
{
73+
$this->createFile();
74+
$this->jsonq = new Jsonq(self::FILE_NAME);
75+
}
76+
77+
protected function tearDown()
78+
{
79+
$this->removeFile();
80+
}
81+
82+
/**
83+
* @param mixed $file
84+
* @param bool $result
85+
*
86+
* @dataProvider importProvider
87+
*/
88+
public function testImport($file, $result)
89+
{
90+
if ($result) {
91+
$this->assertEquals(true, $this->jsonq->import($file));
92+
} else {
93+
$this->expectException(FileNotFoundException::class);
94+
$this->jsonq->import($file);
95+
}
96+
}
97+
98+
public function importProvider()
99+
{
100+
return [
101+
[self::FILE_NAME, true],
102+
[null, false],
103+
[true, false],
104+
[1, false],
105+
['invalid_path.json', false]
106+
];
107+
}
108+
}

0 commit comments

Comments
 (0)