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\Row; |
7
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class JsonExtractorTest |
12
|
|
|
* @package Coco\SourceWatcher\Tests\Core\Extractors |
13
|
|
|
*/ |
14
|
|
|
class JsonExtractorTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
public function testSetterGetterAttributeInput () : void |
20
|
|
|
{ |
21
|
|
|
$jsonExtractor = new JsonExtractor(); |
22
|
|
|
|
23
|
|
|
$givenInput = "/some/file/path/file.json"; |
24
|
|
|
$expectedInput = "/some/file/path/file.json"; |
25
|
|
|
|
26
|
|
|
$jsonExtractor->setInput( $givenInput ); |
27
|
|
|
|
28
|
|
|
$this->assertEquals( $expectedInput, $jsonExtractor->getInput() ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
public function testSetGetColumns () : void |
35
|
|
|
{ |
36
|
|
|
$jsonExtractor = new JsonExtractor(); |
37
|
|
|
|
38
|
|
|
$givenColumns = array( "color" => "colors.*.color" ); |
39
|
|
|
$expectedColumns = array( "color" => "colors.*.color" ); |
40
|
|
|
|
41
|
|
|
$jsonExtractor->setColumns( $givenColumns ); |
42
|
|
|
|
43
|
|
|
$this->assertEquals( $expectedColumns, $jsonExtractor->getColumns() ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @throws SourceWatcherException |
48
|
|
|
*/ |
49
|
|
|
public function testExtractColors () : void |
50
|
|
|
{ |
51
|
|
|
$jsonExtractor = new JsonExtractor(); |
52
|
|
|
$jsonExtractor->setColumns( array( "color" => "colors.*.color" ) ); |
53
|
|
|
$jsonExtractor->setInput( __DIR__ . "/../../../samples/data/json/colors.json" ); |
54
|
|
|
|
55
|
|
|
$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" ] ) ]; |
56
|
|
|
|
57
|
|
|
$this->assertEquals( $expected, $jsonExtractor->extract() ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @throws SourceWatcherException |
62
|
|
|
*/ |
63
|
|
|
public function testNoInputException () : void |
64
|
|
|
{ |
65
|
|
|
$this->expectException( SourceWatcherException::class ); |
66
|
|
|
|
67
|
|
|
$jsonExtractor = new JsonExtractor(); |
68
|
|
|
$jsonExtractor->setInput( null ); |
69
|
|
|
$jsonExtractor->extract(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @throws SourceWatcherException |
74
|
|
|
*/ |
75
|
|
|
public function testNonexistentPath () : void |
76
|
|
|
{ |
77
|
|
|
$this->expectException( SourceWatcherException::class ); |
78
|
|
|
|
79
|
|
|
$jsonExtractor = new JsonExtractor(); |
80
|
|
|
$jsonExtractor->setInput( "/file/path/this/doest/not/exist/file.json" ); |
81
|
|
|
$jsonExtractor->extract(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws SourceWatcherException |
86
|
|
|
*/ |
87
|
|
|
public function testWrongColumnSelectorException () : void |
88
|
|
|
{ |
89
|
|
|
$this->expectException( SourceWatcherException::class ); |
90
|
|
|
|
91
|
|
|
$jsonExtractor = new JsonExtractor(); |
92
|
|
|
$jsonExtractor->setColumns( array( "color" => "$.bad-!-selector" ) ); |
93
|
|
|
$jsonExtractor->setInput( __DIR__ . "/../../../samples/data/json/colors.json" ); |
94
|
|
|
$jsonExtractor->extract(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|