Skip to content

Commit e6d450f

Browse files
committed
test: add new tests
Add new tests. #15
1 parent 1e8a47f commit e6d450f

File tree

1 file changed

+258
-61
lines changed

1 file changed

+258
-61
lines changed

tests/JsonTest.php

Lines changed: 258 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* PHP simple library for managing JSON files.
57
*
@@ -9,130 +11,325 @@
911
* @link https://github.com/josantonius/php-json
1012
* @since 1.1.3
1113
*/
12-
namespace Josantonius\Json;
14+
namespace Josantonius\Json\Tests;
1315

14-
use Josantonius\Json\Exception\JsonException;
16+
use Josantonius\Json\Exception\CreateDirectoryException;
17+
use Josantonius\Json\Exception\CreateFileException;
18+
use Josantonius\Json\Exception\GetFileException;
19+
use Josantonius\Json\Exception\JsonErrorException;
20+
use Josantonius\Json\Exception\UnavailableMethodException;
21+
use Josantonius\Json\Json;
1522
use PHPUnit\Framework\TestCase;
1623

17-
/**
18-
* Tests class for JSON library.
19-
*/
2024
class JsonTest extends TestCase
2125
{
2226
/**
23-
* JSON instance.
24-
*
25-
* @since 1.1.6
26-
*
27-
* @var object
27+
* @since 2.0.0
28+
*/
29+
private string $filepath = __DIR__ . '/filename.json';
30+
31+
/**
32+
* @since 2.0.0
2833
*/
29-
protected $json;
34+
private string $url = 'https://raw.githubusercontent.com/josantonius/php-json/main/composer.json';
3035

3136
/**
32-
* Set up.
33-
*
3437
* @since 1.1.6
3538
*/
3639
public function setUp(): void
3740
{
38-
parent::setUp();
41+
parent::setup();
42+
}
3943

40-
$this->json = new Json;
44+
/**
45+
* @since 2.0.0
46+
*/
47+
public function tearDown(): void
48+
{
49+
if (file_exists($this->filepath)) {
50+
unlink($this->filepath);
51+
}
4152
}
4253

4354
/**
44-
* Check if it is an instance of JSON.
45-
*
46-
* @since 1.1.6
55+
* @test
56+
* @since 2.0.0
4757
*/
48-
public function testGetCollection()
58+
public function itShouldReturnValidInstance(): void
4959
{
50-
$this->assertInstanceOf('Josantonius\Json\Json', $this->json);
60+
$this->assertInstanceOf(Json::class, new Json($this->filepath));
5161
}
5262

5363
/**
54-
* Creating JSON file from array.
64+
* @test
65+
* @since 2.0.0
5566
*/
56-
public function testArrayToFile()
67+
public function constructorShouldCreateTheFileIfNotExist(): void
5768
{
58-
$json = $this->json;
69+
$this->assertFalse(file_exists($this->filepath));
70+
71+
new Json($this->filepath);
72+
73+
$this->assertTrue(file_exists($this->filepath));
5974

60-
$array = [
61-
'name' => 'Josantonius',
62-
'email' => 'hello@josantonius.dev',
63-
'url' => 'https://github.com/josantonius/php-json',
64-
];
75+
$this->assertEquals('[]', file_get_contents($this->filepath));
76+
}
6577

66-
$file = __DIR__.'/filename.json';
67-
$result = $json::arrayToFile($array, $file);
78+
/**
79+
* @test
80+
* @since 2.0.0
81+
*/
82+
public function constructorShouldThrowExceptionIfPathIsWrong(): void
83+
{
84+
$this->expectException(CreateDirectoryException::class);
6885

69-
$this->assertFileIsReadable($file);
86+
new Json(__DIR__ . '/foo|/filename.json');
87+
}
7088

71-
$this->assertTrue($result);
89+
/**
90+
* @test
91+
* @since 2.0.0
92+
*/
93+
public function constructorShouldThrowExceptionIfFilenameIsWrong(): void
94+
{
95+
$this->expectException(CreateFileException::class);
96+
97+
new Json(__DIR__ . '/file|name.json');
7298
}
7399

74100
/**
75-
* Exception to creating JSON file from array.
101+
* @test
102+
* @since 2.0.0
76103
*/
77-
public function testArrayToFileCreateFileException()
104+
public function itShouldGetFileContents(): void
78105
{
79-
$json = $this->json;
106+
$jsonFile = new Json($this->filepath);
80107

81-
$this->expectException(JsonException::class);
108+
$this->assertEquals([], $jsonFile->get());
109+
}
110+
111+
/**
112+
* @test
113+
* @since 2.0.0
114+
*/
115+
public function itShouldGetRemoteFileContents(): void
116+
{
117+
$jsonFile = new Json($this->url);
82118

83-
$json::arrayToFile([], '..');
119+
$this->assertArrayHasKey('name', $jsonFile->get());
84120
}
85121

86122
/**
87-
* Get to array the JSON file content.
123+
* @test
124+
* @since 2.0.0
88125
*/
89-
public function testFileToArray()
126+
public function itShouldSetArrayOnJsonFile(): void
90127
{
91-
$json = $this->json;
128+
$jsonFile = new Json($this->filepath);
92129

93-
$file = __DIR__.'/filename.json';
130+
$array = ['foo' => 'bar'];
94131

95-
$this->assertArrayHasKey('name', $json::fileToArray($file));
96-
$this->assertArrayHasKey('email', $json::fileToArray($file));
97-
$this->assertArrayHasKey('url', $json::fileToArray($file));
132+
$jsonFile->set($array);
98133

99-
unlink($file);
134+
$this->assertEquals($array, json_decode(file_get_contents($this->filepath), true));
100135
}
101136

102137
/**
103-
* Error when file doesn't exist and cannot create file.
138+
* @test
139+
* @since 2.0.0
104140
*/
105-
public function testFileToArrayCreateFileException()
141+
public function itShouldSetObjectOnJsonFile(): void
106142
{
107-
$json = $this->json;
143+
$jsonFile = new Json($this->filepath);
144+
145+
$object = (object) ['foo' => 'bar'];
108146

109-
$this->expectException(JsonException::class);
147+
$jsonFile->set($object);
110148

111-
$json::fileToArray(__DIR__.'');
149+
$this->assertEquals($object, json_decode(file_get_contents($this->filepath)));
112150
}
113151

114152
/**
115-
* Get external JSON file and save as array.
153+
* @test
154+
* @since 2.0.0
116155
*/
117-
public function testExternalFileToArray()
156+
public function itShouldThrowExceptionIfSetMethodIsUsedWithRemoteFile(): void
118157
{
119-
$json = $this->json;
158+
$jsonFile = new Json($this->url);
120159

121-
$file = 'https://raw.githubusercontent.com/josantonius/php-json/master/composer.json';
160+
$this->expectException(UnavailableMethodException::class);
122161

123-
$this->assertArrayHasKey('name', $json::fileToArray($file));
124-
$this->assertArrayHasKey('type', $json::fileToArray($file));
162+
$jsonFile->set(['foo' => 'bar']);
125163
}
126164

127165
/**
128-
* Get external JSON file and save as array.
166+
* @test
167+
* @since 2.0.0
129168
*/
130-
public function testExternalFileNonExistentToArray()
169+
public function itShouldMergeArrayOnJsonFile(): void
131170
{
132-
$json = $this->json;
171+
$jsonFile = new Json($this->filepath);
172+
173+
$jsonFile->set(['foo' => 'bar']);
174+
175+
$jsonFile->merge(['bar' => 'foo']);
176+
177+
$this->assertEquals([
178+
'foo' => 'bar',
179+
'bar' => 'foo'
180+
], json_decode(file_get_contents($this->filepath), true));
181+
}
182+
183+
/**
184+
* @test
185+
* @since 2.0.0
186+
*/
187+
public function itShouldMergeObjectOnJsonFile(): void
188+
{
189+
$jsonFile = new Json($this->filepath);
190+
191+
$jsonFile->set(['foo' => 'bar']);
192+
193+
$object = (object) ['bar' => 'foo'];
194+
195+
$jsonFile->merge($object);
196+
197+
$this->assertEquals([
198+
'foo' => 'bar',
199+
'bar' => 'foo'
200+
], json_decode(file_get_contents($this->filepath), true));
201+
}
202+
203+
/**
204+
* @test
205+
* @since 2.0.0
206+
*/
207+
public function itShouldThrowExceptionIfMergeMethodIsUsedWithRemoteFile(): void
208+
{
209+
$jsonFile = new Json($this->url);
210+
211+
$this->expectException(UnavailableMethodException::class);
212+
213+
$jsonFile->merge(['bar' => 'foo']);
214+
}
215+
216+
/**
217+
* @test
218+
* @since 2.0.0
219+
*/
220+
public function itShouldPushArrayOnJsonFile(): void
221+
{
222+
$jsonFile = new Json($this->filepath);
223+
224+
$jsonFile->set([['foo' => 'bar']]);
133225

134-
$file = 'https://raw.githubusercontent.com/composer.json';
226+
$jsonFile->push(['bar' => 'foo']);
227+
228+
$this->assertEquals(
229+
[['foo' => 'bar'], ['bar' => 'foo']],
230+
json_decode(file_get_contents($this->filepath), true)
231+
);
232+
}
233+
234+
/**
235+
* @test
236+
* @since 2.0.0
237+
*/
238+
public function itShouldPushObjectOnJsonFile(): void
239+
{
240+
$jsonFile = new Json($this->filepath);
241+
242+
$jsonFile->set([['foo' => 'bar']]);
243+
244+
$jsonFile->push((object) ['bar' => 'foo']);
245+
246+
$this->assertEquals(
247+
[['foo' => 'bar'], ['bar' => 'foo']],
248+
json_decode(file_get_contents($this->filepath), true)
249+
);
250+
}
251+
252+
/**
253+
* @test
254+
* @since 2.0.0
255+
*/
256+
public function itShouldThrowExceptionIfPushMethodIsUsedWithRemoteFile(): void
257+
{
258+
$jsonFile = new Json($this->url);
259+
260+
$this->expectException(UnavailableMethodException::class);
261+
262+
$jsonFile->push(['bar' => 'foo']);
263+
}
264+
265+
/**
266+
* @test
267+
* @since 2.0.0
268+
*/
269+
public function itShouldThrowExceptionIfFileCannotBeObtained(): void
270+
{
271+
$jsonFile = new Json($this->filepath);
272+
273+
unlink($this->filepath);
274+
275+
$this->expectException(GetFileException::class);
276+
277+
$jsonFile->push((object) ['bar' => 'foo']);
278+
}
279+
280+
281+
/**
282+
* @test
283+
* @since 2.0.0
284+
*/
285+
public function itShouldThrowExceptionIfRemoteFileCannotBeObtained(): void
286+
{
287+
$jsonFile = new Json($this->url . 'wrong');
288+
289+
$this->expectException(GetFileException::class);
290+
291+
$jsonFile->push((object) ['bar' => 'foo']);
292+
}
293+
294+
/**
295+
* @test
296+
* @since 2.0.0
297+
*/
298+
public function itShouldThrowExceptionWhenThereAreJsonErrorsInTheFile(): void
299+
{
300+
$jsonFile = new Json($this->filepath);
301+
302+
file_put_contents($this->filepath, '{');
303+
304+
$this->expectException(JsonErrorException::class);
305+
306+
$jsonFile->get();
307+
}
308+
309+
/**
310+
* @test
311+
* @since 2.0.0
312+
*/
313+
public function arrayToFileStaticMethodShouldBehaveLikeTheSetMethod()
314+
{
315+
$json = new Json($this->filepath);
316+
317+
$json->arrayToFile(['foo' => 'bar'], $this->filepath);
318+
319+
$this->assertEquals(['foo' => 'bar'], json_decode(
320+
file_get_contents($this->filepath),
321+
true
322+
));
323+
}
324+
325+
/**
326+
* @test
327+
* @since 2.0.0
328+
*/
329+
public function fileToArrayStaticMethodShouldBehaveLikeTheGetMethod()
330+
{
331+
$json = new Json($this->filepath);
135332

136-
$this->assertFalse($json::fileToArray($file));
333+
$this->assertEquals($json->fileToArray($this->filepath), $json->get());
137334
}
138335
}

0 commit comments

Comments
 (0)