Skip to content

Commit 1878b45

Browse files
committed
test: upgrade to the new version
Closes #24
1 parent 81b81b4 commit 1878b45

File tree

9 files changed

+796
-82
lines changed

9 files changed

+796
-82
lines changed

tests/ExistsMethodTest.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818

1919
class ExistsMethodTest extends TestCase
2020
{
21-
private string $filepath = __DIR__ . '/filename.json';
21+
protected string $filepath = __DIR__ . '/filename.json';
2222

23-
private string $url = 'https://raw.githubusercontent.com/josantonius/php-json/main/composer.json';
24-
25-
public function setUp(): void
23+
protected function setUp(): void
2624
{
2725
parent::setup();
2826
}
2927

30-
public function tearDown(): void
28+
protected function tearDown(): void
3129
{
3230
if (file_exists($this->filepath)) {
3331
unlink($this->filepath);
@@ -46,15 +44,4 @@ public function test_should_check_if_a_file_exists(): void
4644

4745
$this->assertTrue($jsonFile->exists());
4846
}
49-
50-
public function test_should_check_if_a_remote_file_exists(): void
51-
{
52-
$jsonFile = new Json($this->url . '.foo');
53-
54-
$this->assertFalse($jsonFile->exists());
55-
56-
$jsonFile = new Json($this->url);
57-
58-
$this->assertTrue($jsonFile->exists());
59-
}
6047
}

tests/GetMethodTest.php

Lines changed: 88 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,121 @@
2020

2121
class GetMethodTest extends TestCase
2222
{
23-
private string $filepath = __DIR__ . '/filename.json';
23+
protected string $filepath = __DIR__ . '/filename.json';
2424

25-
private string $url = 'https://raw.githubusercontent.com/josantonius/php-json/main/composer.json';
26-
27-
public function setUp(): void
25+
protected function setUp(): void
2826
{
2927
parent::setup();
3028
}
3129

32-
public function tearDown(): void
30+
protected function tearDown(): void
3331
{
3432
if (file_exists($this->filepath)) {
3533
unlink($this->filepath);
3634
}
3735
}
3836

39-
public function test_should_get_file_contents_if_the_file_exists(): void
37+
public function test_should_get_file_contents_as_associative_array(): void
4038
{
4139
$jsonFile = new Json($this->filepath);
4240

43-
$jsonFile->set();
41+
$value = ['foo' => 'bar'];
42+
43+
file_put_contents($this->filepath, json_encode($value));
4444

45-
$this->assertEquals([], $jsonFile->get());
45+
$content = $jsonFile->get();
46+
47+
$this->assertEquals($value, $content);
4648
}
4749

48-
public function test_should_fail_if_the_file_does_not_exists(): void
50+
public function test_should_get_file_contents_as_numeric_array(): void
4951
{
50-
$this->expectException(GetFileException::class);
52+
$jsonFile = new Json($this->filepath);
53+
54+
$value = ['foo', 'bar'];
55+
56+
file_put_contents($this->filepath, json_encode($value));
57+
58+
$content = $jsonFile->get();
59+
60+
$this->assertEquals($value, $content);
61+
}
62+
63+
public function test_should_get_file_contents_as_object(): void
64+
{
65+
$jsonFile = new Json($this->filepath);
66+
67+
$value = ['foo' => 'bar'];
68+
69+
file_put_contents($this->filepath, json_encode($value));
70+
71+
$content = $jsonFile->get(false);
72+
73+
$this->assertEquals((object) $value, $content);
74+
}
75+
76+
public function test_should_get_file_contents_as_boolean(): void
77+
{
78+
$jsonFile = new Json($this->filepath);
79+
80+
$value = false;
81+
82+
file_put_contents($this->filepath, json_encode($value));
83+
84+
$content = $jsonFile->get();
85+
86+
$this->assertEquals($value, $content);
87+
}
88+
89+
public function test_should_get_file_contents_as_integer(): void
90+
{
91+
$jsonFile = new Json($this->filepath);
92+
93+
$value = 8;
94+
95+
file_put_contents($this->filepath, json_encode($value));
96+
97+
$content = $jsonFile->get();
98+
99+
$this->assertEquals($value, $content);
100+
}
101+
102+
public function test_should_get_file_contents_as_string(): void
103+
{
104+
$jsonFile = new Json($this->filepath);
105+
106+
$value = 'foo';
51107

108+
file_put_contents($this->filepath, json_encode($value));
109+
110+
$content = $jsonFile->get();
111+
112+
$this->assertEquals($value, $content);
113+
}
114+
115+
public function test_should_get_file_contents_as_null(): void
116+
{
52117
$jsonFile = new Json($this->filepath);
53118

54-
$this->assertEquals([], $jsonFile->get());
119+
$value = null;
120+
121+
file_put_contents($this->filepath, json_encode($value));
122+
123+
$content = $jsonFile->get();
124+
125+
$this->assertNull($content);
55126
}
56127

57-
public function test_should_get_remote_file_contents(): void
128+
public function test_should_fail_if_the_file_does_not_exists(): void
58129
{
59-
$jsonFile = new Json($this->url);
130+
$jsonFile = new Json($this->filepath);
60131

61-
$this->assertArrayHasKey('name', $jsonFile->get());
132+
$this->expectException(GetFileException::class);
133+
134+
$jsonFile->get();
62135
}
63136

64-
public function test_should_throw_exception_when_there_are_json_errors_in_the_file(): void
137+
public function test_should_fail_when_there_are_json_errors_in_the_file(): void
65138
{
66139
$jsonFile = new Json($this->filepath);
67140

tests/MergeMethodTest.php

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
use PHPUnit\Framework\TestCase;
1818
use Josantonius\Json\Exceptions\GetFileException;
1919
use Josantonius\Json\Exceptions\JsonErrorException;
20-
use Josantonius\Json\Exceptions\UnavailableMethodException;
20+
use Josantonius\Json\Exceptions\NoIterableFileException;
21+
use Josantonius\Json\Exceptions\NoIterableElementException;
2122

2223
class MergeMethodTest extends TestCase
2324
{
2425
private string $filepath = __DIR__ . '/filename.json';
2526

26-
private string $url = 'https://raw.githubusercontent.com/josantonius/php-json/main/composer.json';
27-
2827
public function setUp(): void
2928
{
3029
parent::setup();
@@ -43,12 +42,12 @@ public function test_should_merge_array_on_json_file(): void
4342

4443
$jsonFile->set(['foo' => 'bar']);
4544

46-
$jsonFile->merge(['bar' => 'foo']);
45+
$result = $jsonFile->merge(['bar' => 'foo']);
4746

4847
$this->assertEquals([
4948
'foo' => 'bar',
5049
'bar' => 'foo'
51-
], json_decode(file_get_contents($this->filepath), true));
50+
], $result);
5251
}
5352

5453
public function test_should_merge_object_on_json_file(): void
@@ -57,14 +56,43 @@ public function test_should_merge_object_on_json_file(): void
5756

5857
$jsonFile->set(['foo' => 'bar']);
5958

60-
$object = (object) ['bar' => 'foo'];
61-
62-
$jsonFile->merge($object);
59+
$result = $jsonFile->merge((object) ['bar' => 'foo']);
6360

6461
$this->assertEquals([
6562
'foo' => 'bar',
6663
'bar' => 'foo'
67-
], json_decode(file_get_contents($this->filepath), true));
64+
], $result);
65+
}
66+
67+
public function test_should_merge_the_content_for_a_specific_level_from_dot_notation(): void
68+
{
69+
$jsonFile = new Json($this->filepath);
70+
71+
$jsonFile->set(['foo' => ['bar' => 'baz']]);
72+
73+
$result = $jsonFile->merge((object) ['baz' => 'bar'], 'foo');
74+
75+
$this->assertEquals(['foo' => [
76+
'bar' => 'baz',
77+
'baz' => 'bar'
78+
]], $result);
79+
80+
$jsonFile->set(['foo' => [['bar']]]);
81+
82+
$result = $jsonFile->merge(['baz'], 'foo.0');
83+
84+
$this->assertEquals(['foo' => [
85+
['bar', 'baz']
86+
]], $result);
87+
88+
$jsonFile->set([['foo'], ['bar']]);
89+
90+
$result = $jsonFile->merge(['baz'], 1);
91+
92+
$this->assertEquals([
93+
['foo'],
94+
['bar', 'baz'],
95+
], $result);
6896
}
6997

7098
public function test_should_fail_if_the_file_does_not_exists(): void
@@ -76,23 +104,36 @@ public function test_should_fail_if_the_file_does_not_exists(): void
76104
$jsonFile->merge((object) ['bar' => 'foo']);
77105
}
78106

79-
public function test_should_throw_exception_if_merge_method_is_used_with_remote_file(): void
107+
public function test_should_fail_when_there_are_json_errors_in_the_file(): void
80108
{
81-
$jsonFile = new Json($this->url);
109+
$jsonFile = new Json($this->filepath);
110+
111+
file_put_contents($this->filepath, '{');
82112

83-
$this->expectException(UnavailableMethodException::class);
113+
$this->expectException(JsonErrorException::class);
84114

85115
$jsonFile->merge(['bar' => 'foo']);
86116
}
87117

88-
public function test_should_throw_exception_when_there_are_json_errors_in_the_file(): void
118+
public function test_should_fail_if_the_dot_path_does_not_contain_an_array(): void
89119
{
90120
$jsonFile = new Json($this->filepath);
91121

92-
file_put_contents($this->filepath, '{');
122+
$jsonFile->set(['foo' => null]);
93123

94-
$this->expectException(JsonErrorException::class);
124+
$this->expectException(NoIterableElementException::class);
95125

96-
$jsonFile->merge(['bar' => 'foo']);
126+
$jsonFile->merge(['bar'], 'foo');
127+
}
128+
129+
public function test_should_fail_if_the_file_does_not_contain_an_array(): void
130+
{
131+
$jsonFile = new Json($this->filepath);
132+
133+
$jsonFile->set('foo');
134+
135+
$this->expectException(NoIterableFileException::class);
136+
137+
$jsonFile->merge(['bar'], 'foo');
97138
}
98139
}

0 commit comments

Comments
 (0)