NewsTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 103
c 1
b 0
f 0
dl 0
loc 164
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createJsonWithItems() 0 17 1
B providerExtractFromJson() 0 118 1
A test_extract_from_json() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chemaclass\StockTickerTests\Unit\Domain\Crawler\Site\FinanceYahoo\JsonExtractor\StreamStore;
6
7
use Chemaclass\StockTicker\Domain\Crawler\Site\FinanceYahoo\JsonExtractor\StreamStore\News;
8
use Chemaclass\StockTicker\Domain\Crawler\Site\Shared\NewsNormalizer;
9
use DateTimeZone;
10
use Generator;
11
use PHPUnit\Framework\TestCase;
12
13
final class NewsTest extends TestCase
14
{
15
    private const EXAMPLE_UNIX_PUBTIME = 1607651748000;
16
17
    private const EXAMPLE_FORMATTED_DATETIME = '2020-12-11 01:55:48';
18
19
    private const EXAMPLE_TIMEZONE = 'Europe/Berlin';
20
21
    /**
22
     * @dataProvider providerExtractFromJson
23
     */
24
    public function test_extract_from_json(array $allItems, array $expected): void
25
    {
26
        $json = $this->createJsonWithItems($allItems);
27
        $news = new News(
28
            new NewsNormalizer(new DateTimeZone(self::EXAMPLE_TIMEZONE)),
29
        );
30
31
        self::assertEquals(
32
            $expected,
33
            $news->extractFromJson($json),
34
        );
35
    }
36
37
    public function providerExtractFromJson(): Generator
38
    {
39
        yield 'One add' => [
40
            'allItems' => [
41
                [
42
                    'type' => 'ad',
43
                    'title' => 'This is an add',
44
                    'pubtime' => self::EXAMPLE_UNIX_PUBTIME,
45
                    'url' => 'url.com',
46
                    'summary' => 'A summary',
47
                ],
48
            ],
49
            'expected' => [],
50
        ];
51
52
        yield 'One video' => [
53
            'allItems' => [
54
                [
55
                    'type' => 'video',
56
                    'title' => 'This is a video',
57
                    'pubtime' => self::EXAMPLE_UNIX_PUBTIME,
58
                    'url' => 'url.com',
59
                    'summary' => 'A summary',
60
                ],
61
            ],
62
            'expected' => [],
63
        ];
64
65
        yield 'One article' => [
66
            'allItems' => [
67
                [
68
                    'type' => 'article',
69
                    'title' => 'The title',
70
                    'pubtime' => self::EXAMPLE_UNIX_PUBTIME,
71
                    'url' => 'url.com',
72
                    'summary' => 'A summary',
73
                    'publisher' => null,
74
                    'images' => [],
75
                ],
76
            ],
77
            'expected' => [
78
                [
79
                    'title' => 'The title',
80
                    'datetime' => self::EXAMPLE_FORMATTED_DATETIME,
81
                    'url' => 'url.com',
82
                    'summary' => 'A summary',
83
                    'timezone' => self::EXAMPLE_TIMEZONE,
84
                    'source' => 'FinanceYahoo',
85
                    'publisher' => null,
86
                    'images' => [],
87
                ],
88
            ],
89
        ];
90
91
        yield 'A mix with add, video and articles' => [
92
            'allItems' => [
93
                [
94
                    'type' => '-',
95
                    'title' => 'Unknown type title',
96
                    'pubtime' => self::EXAMPLE_UNIX_PUBTIME,
97
                    'url' => 'url.~0~.com',
98
                    'summary' => 'summary',
99
                ],
100
                [
101
                    'type' => 'ad',
102
                    'title' => 'This is an Add!',
103
                    'pubtime' => self::EXAMPLE_UNIX_PUBTIME,
104
                    'url' => 'url.~1~.com',
105
                    'summary' => 'summary',
106
                ],
107
                [
108
                    'type' => 'article',
109
                    'title' => 'The first title',
110
                    'pubtime' => self::EXAMPLE_UNIX_PUBTIME,
111
                    'url' => 'url.1.com',
112
                    'summary' => 'First summary',
113
                    'source' => 'FinanceYahoo',
114
                    'publisher' => null,
115
                    'images' => [],
116
                ],
117
                [
118
                    'type' => 'video',
119
                    'title' => 'This is another Add!',
120
                    'pubtime' => self::EXAMPLE_UNIX_PUBTIME,
121
                    'url' => 'url.~2~.com',
122
                    'summary' => 'summary',
123
                ],
124
                [
125
                    'type' => 'article',
126
                    'title' => 'The second title',
127
                    'pubtime' => self::EXAMPLE_UNIX_PUBTIME,
128
                    'url' => 'url.2.com',
129
                    'summary' => 'Second summary',
130
                    'source' => 'FinanceYahoo',
131
                    'publisher' => null,
132
                    'images' => [],
133
                ],
134
            ],
135
            'expected' => [
136
                [
137
                    'title' => 'The first title',
138
                    'datetime' => self::EXAMPLE_FORMATTED_DATETIME,
139
                    'url' => 'url.1.com',
140
                    'summary' => 'First summary',
141
                    'timezone' => self::EXAMPLE_TIMEZONE,
142
                    'source' => 'FinanceYahoo',
143
                    'publisher' => null,
144
                    'images' => [],
145
                ],
146
                [
147
                    'title' => 'The second title',
148
                    'datetime' => self::EXAMPLE_FORMATTED_DATETIME,
149
                    'url' => 'url.2.com',
150
                    'summary' => 'Second summary',
151
                    'timezone' => self::EXAMPLE_TIMEZONE,
152
                    'source' => 'FinanceYahoo',
153
                    'publisher' => null,
154
                    'images' => [],
155
                ],
156
            ],
157
        ];
158
    }
159
160
    private function createJsonWithItems(array $allItems): array
161
    {
162
        $streamStore = [
163
            'streams' => [
164
                [
165
                    'data' => [
166
                        'stream_items' => $allItems,
167
                    ],
168
                ],
169
            ],
170
        ];
171
172
        return [
173
            'context' => [
174
                'dispatcher' => [
175
                    'stores' => [
176
                        'StreamStore' => $streamStore,
177
                    ],
178
                ],
179
            ],
180
        ];
181
    }
182
}
183