Passed
Push — master ( 7bee4d...457c92 )
by Sebastian
01:54
created

JsonTest::testWrite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace CaptainHook\App\Storage\File;
11
12
use Exception;
13
use PHPUnit\Framework\TestCase;
14
15
class JsonTest extends TestCase
16
{
17
    /**
18
     * Tests Json::readAssoc
19
     */
20
    public function testReadInvalid(): void
21
    {
22
        $this->expectException(Exception::class);
23
24
        $path = realpath(CH_PATH_FILES . '/config/invalid.json');
25
        $json = new Json($path);
26
        $data = $json->readAssoc();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
27
    }
28
29
    /**
30
     * Tests Json::readAssoc
31
     */
32
    public function testReadAssoc(): void
33
    {
34
        $path = realpath(CH_PATH_FILES . '/storage/test.json');
35
        $json = new Json($path);
36
        $data = $json->readAssoc();
37
38
        $this->assertEquals('bar', $data['foo']);
39
    }
40
41
    /**
42
     * Test Json::write
43
     */
44
    public function testWrite(): void
45
    {
46
        $path = tempnam(sys_get_temp_dir(), 'json');
47
        $json = new Json($path);
48
        $data = ['foo' => 'bar'];
49
        $json->write($data);
50
51
        $json = file_get_contents($path);
52
        $load = json_decode($json, true);
53
54
        unlink($path);
55
        $this->assertEquals('bar', $load['foo']);
56
    }
57
}
58