Passed
Push — master ( 324412...628ad0 )
by Yahya
05:56
created

UnitTest::testMissingCss()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests;
6
7
use DOMSelector\DOMSelector;
8
use DOMSelector\Formatters\Decimal;
9
use DOMSelector\Formatters\Integer;
10
use PHPUnit\Framework\TestCase;
11
12
class UnitTest extends TestCase
13
{
14
    public function testYamlLoaders()
15
    {
16
        $selector1 = DOMSelector::fromYamlString(file_get_contents('tests/data/files/basic.yaml'));
17
        $selector2 = DOMSelector::fromYamlFile('tests/data/files/basic.yaml');
18
19
        $this->assertInstanceOf(DOMSelector::class, $selector1);
20
        $this->assertInstanceOf(DOMSelector::class, $selector2);
21
        $this->assertSame($selector1->getConfig(), $selector2->getConfig());
22
    }
23
24
    public function testTypeAttribute()
25
    {
26
        $yaml_string = '
27
        img:
28
            css: "img"
29
            type: Attribute
30
            attribute: width';
31
32
        $selector = DOMSelector::fromYamlString($yaml_string)
33
            ->extract('<img src="photo.jpg" width="80" height="80" />');
34
35
        $this->assertEquals(80, $selector['img']);
36
    }
37
38
    public function testAttributeWithSingleFormatter()
39
    {
40
        $yaml_string = '
41
        width:
42
            css: "img"
43
            type: Attribute
44
            attribute: width
45
            format: 
46
                - Integer';
47
48
        $selector = DOMSelector::fromYamlString($yaml_string, [new Integer()])
49
            ->extract('<img src="photo.jpg" width="200" height="200" />');
50
51
        $this->assertSame('integer', gettype($selector['width']));
52
        $this->assertSame(1000, $selector['width'] * 5);
53
    }
54
55
    public function testAttributeWithMultipleFormat()
56
    {
57
        $yaml_string = '
58
        width:
59
            css: "img"
60
            type: Attribute
61
            attribute: width
62
            format: 
63
                - Integer
64
                - Decimal
65
        height:
66
            css: "img"
67
            type: Attribute
68
            attribute: height
69
            format: 
70
                - Decimal
71
                - Integer';
72
73
        $selector = DOMSelector::fromYamlString($yaml_string, [new Integer(), new Decimal()])
74
            ->extract('<img src="photo.jpg" width="200" height="200" />');
75
76
        $this->assertSame('double', gettype($selector['width']));
77
        $this->assertSame(200.00, $selector['width']);
78
        $this->assertSame('integer', gettype($selector['height']));
79
        $this->assertSame(200, $selector['height']);
80
        $this->assertEquals($selector['width'], $selector['height']);
81
        $this->assertNotSame($selector['width'], $selector['height']);
82
    }
83
84
    public function testTypeHtml()
85
    {
86
        $yaml_string = '
87
        content:
88
            css: "ul li"
89
            type: Html
90
        text:
91
            css: "ul"
92
            type: Text';
93
94
        $selector = DOMSelector::fromYamlString($yaml_string)
95
            ->extract('<ul><li><strong>STRONG!</strong></li></ul>');
96
97
        $this->assertEquals('<strong>STRONG!</strong>', $selector['content']);
98
        $this->assertEquals('STRONG!', $selector['text']);
99
    }
100
101
    public function testTypeImage()
102
    {
103
        $yaml_string = '
104
        img:
105
            css: "img"
106
            type: Image';
107
108
        $selector = DOMSelector::fromYamlString($yaml_string)
109
            ->extract('<img src="photo.jpg" width="80" height="80" />');
110
111
        $this->assertEquals('photo.jpg', $selector['img']);
112
    }
113
114
    public function testTypeLink()
115
    {
116
        $yaml_string = '
117
        link:
118
            css: "a"
119
            type: Link
120
        text:
121
            css: "a"
122
            type: Text';
123
124
        $selector = DOMSelector::fromYamlString($yaml_string)
125
            ->extract('<div><a href="https://example.com/">Click Here!</a> </div>');
126
127
        $this->assertEquals('https://example.com/', $selector['link']);
128
        $this->assertEquals('Click Here!', $selector['text']);
129
    }
130
131
    public function testTypeText()
132
    {
133
        $yaml_string = '
134
        content:
135
            css: "h1"
136
            type: Text';
137
138
        $selector = DOMSelector::fromYamlString($yaml_string)->extract('<div><h1>Hey Bro :)</h1></div>');
139
140
        $this->assertEquals('Hey Bro :)', $selector['content']);
141
    }
142
143
    public function testMissingCss()
144
    {
145
        $yaml_string = '
146
        content:
147
            type: Text';
148
149
        $selector = DOMSelector::fromYamlString($yaml_string)->extract('<div></div>');
150
151
        $this->assertEquals(false, $selector['content']);
152
    }
153
154
    public function testDefaultItemType()
155
    {
156
        $yaml_string = '
157
        content:
158
            css: "div"';
159
160
        $selector = DOMSelector::fromYamlString($yaml_string)
161
            ->extract('<div><h1><a href="https://example.com/">Click Here!</a></h1></div>');
162
163
        $this->assertEquals('Click Here!', $selector['content']);
164
    }
165
166
    public function testGetChildItem()
167
    {
168
        $yaml_string = '
169
        items:
170
            css: "div.items"
171
            type: Text
172
            children:
173
                name:
174
                    css: "p"
175
                    type: Text
176
                value:
177
                    css: "span"
178
                    type: Text';
179
180
        $selector = DOMSelector::fromYamlString($yaml_string)
181
            ->extract('<div class="items"><p>key</p><span>value</span></div>');
182
183
        $this->assertEquals([
184
            'items' => [
185
                'name'  => 'key',
186
                'value' => 'value',
187
            ],
188
        ], $selector);
189
    }
190
191
    public function testMultiple()
192
    {
193
        $yaml_string = '
194
        items:
195
            css: "ul.items li"
196
            multiple: True';
197
198
        $selector = DOMSelector::fromYamlString($yaml_string)
199
            ->extract('<ul class="items"><li>One</li><li>Two</li><li>Three</li></ul>');
200
201
        $this->assertEquals([
202
            'items' => [
203
                'One', 'Two', 'Three',
204
            ],
205
        ], $selector);
206
    }
207
208
    public function testMultipleWithChildren()
209
    {
210
        $yaml_string = '
211
        items:
212
            css: "ul li"
213
            multiple: True
214
            children:
215
                firstname:
216
                    css: ".key"
217
                    type: Text
218
                lastname:
219
                    css: ".value"
220
                    type: Text';
221
222
        $html = '
223
        <ul>
224
            <li><p class="key">John</p><p class="value">Doe</p></li>
225
            <li><p class="key">Jane</p><p class="value">Doe</p></li>
226
        </ul>';
227
228
        $selector = DOMSelector::fromYamlString($yaml_string)->extract($html);
229
230
        $this->assertEquals([
231
            'items' => [
232
                0 => [
233
                    'firstname' => 'John',
234
                    'lastname'  => 'Doe',
235
                ],
236
                1 => [
237
                    'firstname' => 'Jane',
238
                    'lastname'  => 'Doe',
239
                ],
240
            ],
241
        ], $selector);
242
    }
243
244
    public function testInitializingFormatter()
245
    {
246
        $yaml_string = '
247
        test:
248
            css: "h1"
249
            type: Text';
250
251
        $selector = DOMSelector::fromYamlString($yaml_string, [new Integer()]);
252
253
        $this->assertEquals(['Integer' => new Integer()], $selector->getFormatters());
254
        $this->assertEquals('Integer', $selector->getFormatter('Integer')->getName());
255
    }
256
257
    public function testFormatterSingle()
258
    {
259
        $yaml_string = '
260
        string:
261
            css: "p"
262
            type: Text
263
        integer:
264
            css: "p"
265
            type: Text
266
            format: Integer';
267
268
        $selector = DOMSelector::fromYamlString($yaml_string, [new Integer()])->extract('<p>1</p>');
269
270
        $this->assertEquals('string', gettype($selector['string']));
271
        $this->assertEquals('integer', gettype($selector['integer']));
272
    }
273
274
    public function testFormatterMultiple()
275
    {
276
        $yaml_string = '
277
        decimal:
278
            css: "p"
279
            type: Text
280
            format: 
281
                - Integer
282
                - Decimal';
283
284
        $selector = DOMSelector::fromYamlString($yaml_string, [new Integer(), new Decimal()])->extract('<p>1</p>');
285
286
        $this->assertSame(1.00, $selector['decimal']);
287
        $this->assertSame('double', gettype($selector['decimal']));
288
    }
289
}
290