Skip to content

Commit cbaa23c

Browse files
committed
test: add new tests
#17
1 parent e3d7128 commit cbaa23c

File tree

5 files changed

+363
-0
lines changed

5 files changed

+363
-0
lines changed

tests/ConstructMethodTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of https://github.com/josantonius/php-json repository.
7+
*
8+
* (c) Josantonius <hello@josantonius.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Josantonius\Json\Tests;
15+
16+
use Josantonius\Json\Exception\CreateDirectoryException;
17+
use Josantonius\Json\Exception\CreateFileException;
18+
use Josantonius\Json\Json;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class ConstructMethodTest extends TestCase
22+
{
23+
private string $filepath = __DIR__ . '/filename.json';
24+
25+
public function setUp(): void
26+
{
27+
parent::setup();
28+
}
29+
30+
public function tearDown(): void
31+
{
32+
if (file_exists($this->filepath)) {
33+
unlink($this->filepath);
34+
}
35+
}
36+
37+
public function testShouldCreateTheFileIfNotExist(): void
38+
{
39+
$this->assertFalse(file_exists($this->filepath));
40+
41+
new Json($this->filepath);
42+
43+
$this->assertTrue(file_exists($this->filepath));
44+
45+
$this->assertEquals('[]', file_get_contents($this->filepath));
46+
}
47+
48+
public function testShouldThrowExceptionIfPathIsWrong(): void
49+
{
50+
$this->expectException(CreateDirectoryException::class);
51+
52+
new Json('/foo:/filename.json');
53+
}
54+
55+
public function testShouldThrowExceptionIfFilenameIsWrong(): void
56+
{
57+
$this->expectException(CreateFileException::class);
58+
59+
new Json('/file:name.json');
60+
}
61+
}

tests/GetMethodTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of https://github.com/josantonius/php-json repository.
7+
*
8+
* (c) Josantonius <hello@josantonius.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Josantonius\Json\Tests;
15+
16+
use Josantonius\Json\Exception\JsonErrorException;
17+
use Josantonius\Json\Json;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class GetMethodTest extends TestCase
21+
{
22+
private string $filepath = __DIR__ . '/filename.json';
23+
24+
private string $url = 'https://raw.githubusercontent.com/josantonius/php-json/main/composer.json';
25+
26+
public function setUp(): void
27+
{
28+
parent::setup();
29+
}
30+
31+
public function tearDown(): void
32+
{
33+
if (file_exists($this->filepath)) {
34+
unlink($this->filepath);
35+
}
36+
}
37+
38+
public function testShouldGetFileContents(): void
39+
{
40+
$jsonFile = new Json($this->filepath);
41+
42+
$this->assertEquals([], $jsonFile->get());
43+
}
44+
45+
public function testShouldGetRemoteFileContents(): void
46+
{
47+
$jsonFile = new Json($this->url);
48+
49+
$this->assertArrayHasKey('name', $jsonFile->get());
50+
}
51+
52+
public function testShouldThrowExceptionWhenThereAreJsonErrorsInTheFile(): void
53+
{
54+
$jsonFile = new Json($this->filepath);
55+
56+
file_put_contents($this->filepath, '{');
57+
58+
$this->expectException(JsonErrorException::class);
59+
60+
$jsonFile->get();
61+
}
62+
}

tests/MergeMethodTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of https://github.com/josantonius/php-json repository.
7+
*
8+
* (c) Josantonius <hello@josantonius.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Josantonius\Json\Tests;
15+
16+
use Josantonius\Json\Exception\UnavailableMethodException;
17+
use Josantonius\Json\Json;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class MergeMethodTest extends TestCase
21+
{
22+
private string $filepath = __DIR__ . '/filename.json';
23+
24+
private string $url = 'https://raw.githubusercontent.com/josantonius/php-json/main/composer.json';
25+
26+
public function setUp(): void
27+
{
28+
parent::setup();
29+
}
30+
31+
public function tearDown(): void
32+
{
33+
if (file_exists($this->filepath)) {
34+
unlink($this->filepath);
35+
}
36+
}
37+
38+
public function testShouldMergeArrayOnJsonFile(): void
39+
{
40+
$jsonFile = new Json($this->filepath);
41+
42+
$jsonFile->set(['foo' => 'bar']);
43+
44+
$jsonFile->merge(['bar' => 'foo']);
45+
46+
$this->assertEquals([
47+
'foo' => 'bar',
48+
'bar' => 'foo'
49+
], json_decode(file_get_contents($this->filepath), true));
50+
}
51+
52+
public function testShouldMergeObjectOnJsonFile(): void
53+
{
54+
$jsonFile = new Json($this->filepath);
55+
56+
$jsonFile->set(['foo' => 'bar']);
57+
58+
$object = (object) ['bar' => 'foo'];
59+
60+
$jsonFile->merge($object);
61+
62+
$this->assertEquals([
63+
'foo' => 'bar',
64+
'bar' => 'foo'
65+
], json_decode(file_get_contents($this->filepath), true));
66+
}
67+
68+
public function testShouldThrowExceptionIfMergeMethodIsUsedWithRemoteFile(): void
69+
{
70+
$jsonFile = new Json($this->url);
71+
72+
$this->expectException(UnavailableMethodException::class);
73+
74+
$jsonFile->merge(['bar' => 'foo']);
75+
}
76+
}

tests/PushMethodTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of https://github.com/josantonius/php-json repository.
7+
*
8+
* (c) Josantonius <hello@josantonius.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Josantonius\Json\Tests;
15+
16+
use Josantonius\Json\Exception\GetFileException;
17+
use Josantonius\Json\Exception\UnavailableMethodException;
18+
use Josantonius\Json\Json;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class PushMethodTest extends TestCase
22+
{
23+
private string $filepath = __DIR__ . '/filename.json';
24+
25+
private string $url = 'https://raw.githubusercontent.com/josantonius/php-json/main/composer.json';
26+
27+
public function setUp(): void
28+
{
29+
parent::setup();
30+
}
31+
32+
public function tearDown(): void
33+
{
34+
if (file_exists($this->filepath)) {
35+
unlink($this->filepath);
36+
}
37+
}
38+
39+
public function testShouldPushArrayOnJsonFile(): void
40+
{
41+
$jsonFile = new Json($this->filepath);
42+
43+
$jsonFile->set([['foo' => 'bar']]);
44+
45+
$jsonFile->push(['bar' => 'foo']);
46+
47+
$this->assertEquals(
48+
[['foo' => 'bar'], ['bar' => 'foo']],
49+
json_decode(file_get_contents($this->filepath), true)
50+
);
51+
}
52+
53+
public function testShouldPushObjectOnJsonFile(): void
54+
{
55+
$jsonFile = new Json($this->filepath);
56+
57+
$jsonFile->set([['foo' => 'bar']]);
58+
59+
$jsonFile->push((object) ['bar' => 'foo']);
60+
61+
$this->assertEquals(
62+
[['foo' => 'bar'], ['bar' => 'foo']],
63+
json_decode(file_get_contents($this->filepath), true)
64+
);
65+
}
66+
67+
public function testShouldThrowExceptionIfPushMethodIsUsedWithRemoteFile(): void
68+
{
69+
$jsonFile = new Json($this->url);
70+
71+
$this->expectException(UnavailableMethodException::class);
72+
73+
$jsonFile->push(['bar' => 'foo']);
74+
}
75+
76+
public function testShouldThrowExceptionIfFileCannotBeObtained(): void
77+
{
78+
$jsonFile = new Json($this->filepath);
79+
80+
unlink($this->filepath);
81+
82+
$this->expectException(GetFileException::class);
83+
84+
$jsonFile->push((object) ['bar' => 'foo']);
85+
}
86+
87+
public function testShouldThrowExceptionIfRemoteFileCannotBeObtained(): void
88+
{
89+
$jsonFile = new Json($this->url . 'wrong');
90+
91+
$this->expectException(GetFileException::class);
92+
93+
$jsonFile->push((object) ['bar' => 'foo']);
94+
}
95+
}

tests/SetMethodTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of https://github.com/josantonius/php-json repository.
7+
*
8+
* (c) Josantonius <hello@josantonius.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Josantonius\Json\Tests;
15+
16+
use Josantonius\Json\Exception\UnavailableMethodException;
17+
use Josantonius\Json\Json;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class SetMethodTest extends TestCase
21+
{
22+
private string $filepath = __DIR__ . '/filename.json';
23+
24+
private string $url = 'https://raw.githubusercontent.com/josantonius/php-json/main/composer.json';
25+
26+
public function setUp(): void
27+
{
28+
parent::setup();
29+
}
30+
31+
public function tearDown(): void
32+
{
33+
if (file_exists($this->filepath)) {
34+
unlink($this->filepath);
35+
}
36+
}
37+
38+
public function testShouldSetArrayOnJsonFile(): void
39+
{
40+
$jsonFile = new Json($this->filepath);
41+
42+
$array = ['foo' => 'bar'];
43+
44+
$jsonFile->set($array);
45+
46+
$this->assertEquals($array, json_decode(file_get_contents($this->filepath), true));
47+
}
48+
49+
public function testShouldSetObjectOnJsonFile(): void
50+
{
51+
$jsonFile = new Json($this->filepath);
52+
53+
$object = (object) ['foo' => 'bar'];
54+
55+
$jsonFile->set($object);
56+
57+
$this->assertEquals($object, json_decode(file_get_contents($this->filepath)));
58+
}
59+
60+
61+
public function testShouldThrowExceptionIfSetMethodIsUsedWithRemoteFile(): void
62+
{
63+
$jsonFile = new Json($this->url);
64+
65+
$this->expectException(UnavailableMethodException::class);
66+
67+
$jsonFile->set(['foo' => 'bar']);
68+
}
69+
}

0 commit comments

Comments
 (0)