Passed
Push — master ( 2be253...46260f )
by Jean Paul
01:26
created

JsonExtractorTest::testNoInputException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
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\Inputs\FileInput;
7
use Coco\SourceWatcher\Core\Row;
8
use Coco\SourceWatcher\Core\SourceWatcherException;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * Class JsonExtractorTest
13
 * @package Coco\SourceWatcher\Tests\Core\Extractors
14
 */
15
class JsonExtractorTest extends TestCase
16
{
17
    public function testSetGetColumns () : void
18
    {
19
        $jsonExtractor = new JsonExtractor();
20
21
        $givenColumns = array( "color" => "colors.*.color" );
22
        $expectedColumns = array( "color" => "colors.*.color" );
23
24
        $jsonExtractor->setColumns( $givenColumns );
25
26
        $this->assertEquals( $expectedColumns, $jsonExtractor->getColumns() );
27
    }
28
29
    public function testSetGetInput () : void
30
    {
31
        $jsonExtractor = new JsonExtractor();
32
33
        $givenInput = new FileInput( "/some/file/path/file.json" );
34
        $expectedInput = new FileInput( "/some/file/path/file.json" );
35
36
        $jsonExtractor->setInput( $givenInput );
37
38
        $this->assertEquals( $expectedInput, $jsonExtractor->getInput() );
39
    }
40
41
    public function testExceptionNoInput () : void
42
    {
43
        $this->expectException( SourceWatcherException::class );
44
45
        $jsonExtractor = new JsonExtractor();
46
47
        $jsonExtractor->extract();
48
    }
49
50
    public function testExtractColors () : void
51
    {
52
        try {
53
            $jsonExtractor = new JsonExtractor();
54
            $jsonExtractor->setColumns( array( "color" => "colors.*.color" ) );
55
            $jsonExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/json/colors.json" ) );
56
57
            $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" ] ) ];
58
59
            $this->assertEquals( $expected, $jsonExtractor->extract() );
60
        } catch ( SourceWatcherException $sourceWatcherException ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
61
62
        }
63
    }
64
65
    public function testNonexistentPath () : void
66
    {
67
        $this->expectException( SourceWatcherException::class );
68
69
        $jsonExtractor = new JsonExtractor();
70
        $jsonExtractor->setInput( new FileInput( "/file/path/this/doest/not/exist/file.json" ) );
71
        $jsonExtractor->extract();
72
    }
73
74
    public function testWrongColumnSelectorException () : void
75
    {
76
        $this->expectException( SourceWatcherException::class );
77
78
        $jsonExtractor = new JsonExtractor();
79
        $jsonExtractor->setColumns( array( "color" => "$.bad-!-selector" ) );
80
        $jsonExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/json/colors.json" ) );
81
        $jsonExtractor->extract();
82
    }
83
}
84