|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | /** |
4 | 6 | * PHP simple library for managing JSON files. |
5 | 7 | * |
|
9 | 11 | * @link https://github.com/josantonius/php-json |
10 | 12 | * @since 1.1.3 |
11 | 13 | */ |
12 | | -namespace Josantonius\Json; |
| 14 | +namespace Josantonius\Json\Tests; |
13 | 15 |
|
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; |
15 | 22 | use PHPUnit\Framework\TestCase; |
16 | 23 |
|
17 | | -/** |
18 | | - * Tests class for JSON library. |
19 | | - */ |
20 | 24 | class JsonTest extends TestCase |
21 | 25 | { |
22 | 26 | /** |
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 |
28 | 33 | */ |
29 | | - protected $json; |
| 34 | + private string $url = 'https://raw.githubusercontent.com/josantonius/php-json/main/composer.json'; |
30 | 35 |
|
31 | 36 | /** |
32 | | - * Set up. |
33 | | - * |
34 | 37 | * @since 1.1.6 |
35 | 38 | */ |
36 | 39 | public function setUp(): void |
37 | 40 | { |
38 | | - parent::setUp(); |
| 41 | + parent::setup(); |
| 42 | + } |
39 | 43 |
|
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 | + } |
41 | 52 | } |
42 | 53 |
|
43 | 54 | /** |
44 | | - * Check if it is an instance of JSON. |
45 | | - * |
46 | | - * @since 1.1.6 |
| 55 | + * @test |
| 56 | + * @since 2.0.0 |
47 | 57 | */ |
48 | | - public function testGetCollection() |
| 58 | + public function itShouldReturnValidInstance(): void |
49 | 59 | { |
50 | | - $this->assertInstanceOf('Josantonius\Json\Json', $this->json); |
| 60 | + $this->assertInstanceOf(Json::class, new Json($this->filepath)); |
51 | 61 | } |
52 | 62 |
|
53 | 63 | /** |
54 | | - * Creating JSON file from array. |
| 64 | + * @test |
| 65 | + * @since 2.0.0 |
55 | 66 | */ |
56 | | - public function testArrayToFile() |
| 67 | + public function constructorShouldCreateTheFileIfNotExist(): void |
57 | 68 | { |
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)); |
59 | 74 |
|
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 | + } |
65 | 77 |
|
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); |
68 | 85 |
|
69 | | - $this->assertFileIsReadable($file); |
| 86 | + new Json(__DIR__ . '/foo|/filename.json'); |
| 87 | + } |
70 | 88 |
|
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'); |
72 | 98 | } |
73 | 99 |
|
74 | 100 | /** |
75 | | - * Exception to creating JSON file from array. |
| 101 | + * @test |
| 102 | + * @since 2.0.0 |
76 | 103 | */ |
77 | | - public function testArrayToFileCreateFileException() |
| 104 | + public function itShouldGetFileContents(): void |
78 | 105 | { |
79 | | - $json = $this->json; |
| 106 | + $jsonFile = new Json($this->filepath); |
80 | 107 |
|
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); |
82 | 118 |
|
83 | | - $json::arrayToFile([], '..'); |
| 119 | + $this->assertArrayHasKey('name', $jsonFile->get()); |
84 | 120 | } |
85 | 121 |
|
86 | 122 | /** |
87 | | - * Get to array the JSON file content. |
| 123 | + * @test |
| 124 | + * @since 2.0.0 |
88 | 125 | */ |
89 | | - public function testFileToArray() |
| 126 | + public function itShouldSetArrayOnJsonFile(): void |
90 | 127 | { |
91 | | - $json = $this->json; |
| 128 | + $jsonFile = new Json($this->filepath); |
92 | 129 |
|
93 | | - $file = __DIR__.'/filename.json'; |
| 130 | + $array = ['foo' => 'bar']; |
94 | 131 |
|
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); |
98 | 133 |
|
99 | | - unlink($file); |
| 134 | + $this->assertEquals($array, json_decode(file_get_contents($this->filepath), true)); |
100 | 135 | } |
101 | 136 |
|
102 | 137 | /** |
103 | | - * Error when file doesn't exist and cannot create file. |
| 138 | + * @test |
| 139 | + * @since 2.0.0 |
104 | 140 | */ |
105 | | - public function testFileToArrayCreateFileException() |
| 141 | + public function itShouldSetObjectOnJsonFile(): void |
106 | 142 | { |
107 | | - $json = $this->json; |
| 143 | + $jsonFile = new Json($this->filepath); |
| 144 | + |
| 145 | + $object = (object) ['foo' => 'bar']; |
108 | 146 |
|
109 | | - $this->expectException(JsonException::class); |
| 147 | + $jsonFile->set($object); |
110 | 148 |
|
111 | | - $json::fileToArray(__DIR__.''); |
| 149 | + $this->assertEquals($object, json_decode(file_get_contents($this->filepath))); |
112 | 150 | } |
113 | 151 |
|
114 | 152 | /** |
115 | | - * Get external JSON file and save as array. |
| 153 | + * @test |
| 154 | + * @since 2.0.0 |
116 | 155 | */ |
117 | | - public function testExternalFileToArray() |
| 156 | + public function itShouldThrowExceptionIfSetMethodIsUsedWithRemoteFile(): void |
118 | 157 | { |
119 | | - $json = $this->json; |
| 158 | + $jsonFile = new Json($this->url); |
120 | 159 |
|
121 | | - $file = 'https://raw.githubusercontent.com/josantonius/php-json/master/composer.json'; |
| 160 | + $this->expectException(UnavailableMethodException::class); |
122 | 161 |
|
123 | | - $this->assertArrayHasKey('name', $json::fileToArray($file)); |
124 | | - $this->assertArrayHasKey('type', $json::fileToArray($file)); |
| 162 | + $jsonFile->set(['foo' => 'bar']); |
125 | 163 | } |
126 | 164 |
|
127 | 165 | /** |
128 | | - * Get external JSON file and save as array. |
| 166 | + * @test |
| 167 | + * @since 2.0.0 |
129 | 168 | */ |
130 | | - public function testExternalFileNonExistentToArray() |
| 169 | + public function itShouldMergeArrayOnJsonFile(): void |
131 | 170 | { |
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']]); |
133 | 225 |
|
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); |
135 | 332 |
|
136 | | - $this->assertFalse($json::fileToArray($file)); |
| 333 | + $this->assertEquals($json->fileToArray($this->filepath), $json->get()); |
137 | 334 | } |
138 | 335 | } |
0 commit comments