Passed
Push — master ( fc7f46...e20581 )
by Jean Paul
01:37
created

JsonExtractorTest::testSetGetInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php declare( strict_types = 1 );
2
3
namespace Coco\SourceWatcher\Tests\Core\Extractors;
4
5
use Coco\SourceWatcher\Core\Extractors\JsonExtractor;
6
use Coco\SourceWatcher\Core\IO\Inputs\FileInput;
7
use Coco\SourceWatcher\Core\IO\Inputs\Input;
8
use Coco\SourceWatcher\Core\Row;
9
use Coco\SourceWatcher\Core\SourceWatcherException;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * Class JsonExtractorTest
14
 * @package Coco\SourceWatcher\Tests\Core\Extractors
15
 */
16
class JsonExtractorTest extends TestCase
17
{
18
    /**
19
     *
20
     */
21
    public function testSetGetColumns () : void
22
    {
23
        $jsonExtractor = new JsonExtractor();
24
25
        $givenColumns = array( "color" => "colors.*.color" );
26
        $expectedColumns = array( "color" => "colors.*.color" );
27
28
        $jsonExtractor->setColumns( $givenColumns );
29
30
        $this->assertEquals( $expectedColumns, $jsonExtractor->getColumns() );
31
    }
32
33
    /**
34
     *
35
     */
36
    public function testSetGetInput () : void
37
    {
38
        $jsonExtractor = new JsonExtractor();
39
40
        $givenInput = new FileInput( "/some/file/path/file.json" );
41
        $expectedInput = new FileInput( "/some/file/path/file.json" );
42
43
        $jsonExtractor->setInput( $givenInput );
44
45
        $this->assertEquals( $expectedInput, $jsonExtractor->getInput() );
46
    }
47
48
    /**
49
     * @throws SourceWatcherException
50
     */
51
    public function testExceptionNoInput () : void
52
    {
53
        $this->expectException( SourceWatcherException::class );
54
55
        $jsonExtractor = new JsonExtractor();
56
57
        $jsonExtractor->extract();
58
    }
59
60
    /**
61
     * @throws SourceWatcherException
62
     */
63
    public function testExtractColors () : void
64
    {
65
        $jsonExtractor = new JsonExtractor();
66
        $jsonExtractor->setColumns( array( "color" => "colors.*.color" ) );
67
        $jsonExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/json/colors.json" ) );
68
69
        $expected = [ new Row( [ "color" => "black" ] ), new Row( [ "color" => "white" ] ), new Row( [ "color" => "red" ] ), new Row( [ "color" => "blue" ] ), new Row( [ "color" => "yellow" ] ), new Row( [ "color" => "green" ] ) ];
70
71
        $this->assertEquals( $expected, $jsonExtractor->extract() );
72
    }
73
74
    /**
75
     * @throws SourceWatcherException
76
     */
77
    public function testNonexistentPath () : void
78
    {
79
        $this->expectException( SourceWatcherException::class );
80
81
        $jsonExtractor = new JsonExtractor();
82
        $jsonExtractor->setInput( new FileInput( "/file/path/this/doest/not/exist/file.json" ) );
83
        $jsonExtractor->extract();
84
    }
85
86
    /**
87
     * @throws SourceWatcherException
88
     */
89
    public function testWrongColumnSelectorException () : void
90
    {
91
        $this->expectException( SourceWatcherException::class );
92
93
        $jsonExtractor = new JsonExtractor();
94
        $jsonExtractor->setColumns( array( "color" => "$.bad-!-selector" ) );
95
        $jsonExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/json/colors.json" ) );
96
        $jsonExtractor->extract();
97
    }
98
99
    /**
100
     * @throws SourceWatcherException
101
     */
102
    public function testNoFileInputProvided () : void
103
    {
104
        $this->expectException( SourceWatcherException::class );
105
106
        $jsonExtractor = new JsonExtractor();
107
        $jsonExtractor->setColumns( array( "color" => "some.selector" ) );
108
        $jsonExtractor->setInput( $this->createMock( Input::class ) );
109
        $jsonExtractor->extract();
110
    }
111
}
112