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
|
|
|
* |
15
|
|
|
* @package Coco\SourceWatcher\Tests\Core\Extractors |
16
|
|
|
*/ |
17
|
|
|
class JsonExtractorTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
private string $colorIndex; |
20
|
|
|
private string $allColorsSelector; |
21
|
|
|
|
22
|
|
|
public function setUp () : void |
23
|
|
|
{ |
24
|
|
|
$this->colorIndex = "color"; |
25
|
|
|
$this->allColorsSelector = "colors.*.color"; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testSetGetColumns () : void |
29
|
|
|
{ |
30
|
|
|
$jsonExtractor = new JsonExtractor(); |
31
|
|
|
|
32
|
|
|
$givenColumns = [ $this->colorIndex => $this->allColorsSelector ]; |
33
|
|
|
$expectedColumns = [ $this->colorIndex => $this->allColorsSelector ]; |
34
|
|
|
|
35
|
|
|
$jsonExtractor->setColumns( $givenColumns ); |
36
|
|
|
|
37
|
|
|
$this->assertEquals( $expectedColumns, $jsonExtractor->getColumns() ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testSetGetInput () : void |
41
|
|
|
{ |
42
|
|
|
$jsonExtractor = new JsonExtractor(); |
43
|
|
|
|
44
|
|
|
$givenInput = new FileInput( "/some/file/path/file.json" ); |
45
|
|
|
$expectedInput = new FileInput( "/some/file/path/file.json" ); |
46
|
|
|
|
47
|
|
|
$jsonExtractor->setInput( $givenInput ); |
48
|
|
|
|
49
|
|
|
$this->assertEquals( $expectedInput, $jsonExtractor->getInput() ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @throws SourceWatcherException |
54
|
|
|
*/ |
55
|
|
|
public function testExceptionNoInput () : void |
56
|
|
|
{ |
57
|
|
|
$this->expectException( SourceWatcherException::class ); |
58
|
|
|
|
59
|
|
|
$jsonExtractor = new JsonExtractor(); |
60
|
|
|
|
61
|
|
|
$jsonExtractor->extract(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @throws SourceWatcherException |
66
|
|
|
*/ |
67
|
|
|
public function testExtractColors () : void |
68
|
|
|
{ |
69
|
|
|
$jsonExtractor = new JsonExtractor(); |
70
|
|
|
$jsonExtractor->setColumns( [ $this->colorIndex => $this->allColorsSelector ] ); |
71
|
|
|
$jsonExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/json/colors.json" ) ); |
72
|
|
|
|
73
|
|
|
$expected = [ |
74
|
|
|
new Row( [ $this->colorIndex => "black" ] ), |
75
|
|
|
new Row( [ $this->colorIndex => "white" ] ), |
76
|
|
|
new Row( [ $this->colorIndex => "red" ] ), |
77
|
|
|
new Row( [ $this->colorIndex => "blue" ] ), |
78
|
|
|
new Row( [ $this->colorIndex => "yellow" ] ), |
79
|
|
|
new Row( [ $this->colorIndex => "green" ] ) |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$this->assertEquals( $expected, $jsonExtractor->extract() ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @throws SourceWatcherException |
87
|
|
|
*/ |
88
|
|
|
public function testNonexistentPath () : void |
89
|
|
|
{ |
90
|
|
|
$this->expectException( SourceWatcherException::class ); |
91
|
|
|
|
92
|
|
|
$jsonExtractor = new JsonExtractor(); |
93
|
|
|
$jsonExtractor->setInput( new FileInput( "/file/path/this/doest/not/exist/file.json" ) ); |
94
|
|
|
$jsonExtractor->extract(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @throws SourceWatcherException |
99
|
|
|
*/ |
100
|
|
|
public function testWrongColumnSelectorException () : void |
101
|
|
|
{ |
102
|
|
|
$this->expectException( SourceWatcherException::class ); |
103
|
|
|
|
104
|
|
|
$jsonExtractor = new JsonExtractor(); |
105
|
|
|
$jsonExtractor->setColumns( [ $this->colorIndex => "$.bad-!-selector" ] ); |
106
|
|
|
$jsonExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/json/colors.json" ) ); |
107
|
|
|
$jsonExtractor->extract(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @throws SourceWatcherException |
112
|
|
|
*/ |
113
|
|
|
public function testNoFileInputProvided () : void |
114
|
|
|
{ |
115
|
|
|
$this->expectException( SourceWatcherException::class ); |
116
|
|
|
|
117
|
|
|
$jsonExtractor = new JsonExtractor(); |
118
|
|
|
$jsonExtractor->setColumns( [ $this->colorIndex => "some.selector" ] ); |
119
|
|
|
$jsonExtractor->setInput( $this->createMock( Input::class ) ); |
120
|
|
|
$jsonExtractor->extract(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|