Skip to content

Commit 2389f00

Browse files
committed
Merge pull request #35 from Swader/issue-19
Fixes #19 and optimizes tests a bit
2 parents e68e6d6 + ce0e893 commit 2389f00

File tree

12 files changed

+214
-188
lines changed

12 files changed

+214
-188
lines changed

src/Entity/Article.php

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function getDate()
6666
*/
6767
public function getAuthor()
6868
{
69-
return (isset($this->data['author'])) ? $this->data['author'] : null;
69+
return $this->getOrDefault('author');
7070
}
7171

7272
/**
@@ -75,7 +75,7 @@ public function getAuthor()
7575
*/
7676
public function getAuthorUrl()
7777
{
78-
return (isset($this->data['authorUrl'])) ? $this->data['authorUrl'] : null;
78+
return $this->getOrDefault('authorUrl');
7979
}
8080

8181
/**
@@ -118,7 +118,7 @@ public function getTags()
118118
*/
119119
public function getNumPages()
120120
{
121-
return (isset($this->data['numPages'])) ? $this->data['numPages'] : 1;
121+
return $this->getOrDefault('numPages', 1);
122122
}
123123

124124
/**
@@ -129,7 +129,7 @@ public function getNumPages()
129129
*/
130130
public function getNextPages()
131131
{
132-
return (isset($this->data['nextPages'])) ? $this->data['nextPages'] : [];
132+
return $this->getOrDefault('nextPages', []);
133133
}
134134

135135
/**
@@ -139,7 +139,7 @@ public function getNextPages()
139139
*/
140140
public function getSentiment()
141141
{
142-
return (isset($this->data['sentiment'])) ? $this->data['sentiment'] : null;
142+
return $this->getOrDefault('sentiment');
143143
}
144144

145145
/**
@@ -172,7 +172,7 @@ public function getSentiment()
172172
*/
173173
public function getImages()
174174
{
175-
return (isset($this->data['images'])) ? $this->data['images'] : [];
175+
return $this->getOrDefault('images', []);
176176
}
177177

178178
/**
@@ -199,7 +199,7 @@ public function getImages()
199199
*/
200200
public function getVideos()
201201
{
202-
return (isset($this->data['videos'])) ? $this->data['videos'] : [];
202+
return $this->getOrDefault('videos', []);
203203
}
204204

205205
/**
@@ -210,4 +210,48 @@ public function getDiscussion()
210210
{
211211
return $this->discussion;
212212
}
213+
214+
/**
215+
* The plain-text name of the site (e.g. The New York Times or Diffbot).
216+
*
217+
* If no site name is automatically determined, the root domain (diffbot.com) will be returned.
218+
*
219+
* @return string | null
220+
*/
221+
public function getSiteName()
222+
{
223+
return $this->getOrDefault('siteName');
224+
}
225+
226+
/**
227+
* If known, the country of the article publication.
228+
*
229+
* @return string | null
230+
*/
231+
public function getPublisherCountry()
232+
{
233+
return $this->getOrDefault('publisherCountry', null);
234+
}
235+
236+
/**
237+
* If known, the region of the article publication.
238+
*
239+
* @return string | null
240+
*/
241+
public function getPublisherRegion()
242+
{
243+
return $this->getOrDefault('publisherRegion', null);
244+
}
245+
246+
/**
247+
* If an article's date is ambiguous, Diffbot will attempt to estimate a
248+
* more specific timestamp using various factors. This will not be
249+
* generated for articles older than two days, or articles without an identified date.
250+
*
251+
* @return string | null
252+
*/
253+
public function getEstimatedDate()
254+
{
255+
return $this->getOrDefault('estimatedDate', $this->getDate());
256+
}
213257
}

src/Traits/StandardEntity.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,10 @@ public function getDiffbotUri()
113113
return $this->data['diffbotUri'];
114114
}
115115

116+
protected function getOrDefault($key, $default = null, $data = null)
117+
{
118+
$data = ($data !== null) ?: $this->data;
119+
return (isset($data[$key]) ? $data[$key] : $default);
120+
}
121+
116122
}

tests/Entity/AbstractTest.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,10 @@
99
class AbstractTest extends ResponseProvider
1010
{
1111

12-
/** @var array */
13-
protected $responses = [];
14-
15-
protected $files = [
12+
protected static $staticFiles = [
1613
'Products/dogbrush.json'
1714
];
1815

19-
protected function ei($file)
20-
{
21-
$ef = new Entity();
22-
return $ef->createAppropriateIterator($this->prepareResponses()[$file]);
23-
}
24-
25-
public function returnFiles()
26-
{
27-
$files = [];
28-
foreach ($this->files as $file) {
29-
$files[] = [$file];
30-
}
31-
return $files;
32-
}
33-
3416
public function queryStringProvider()
3517
{
3618
return [

tests/Entity/ArticleTest.php

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,23 @@
33
namespace Swader\Diffbot\Test\Entity;
44

55
use Swader\Diffbot\Entity\Article;
6-
use Swader\Diffbot\Factory\Entity;
76
use Swader\Diffbot\Test\ResponseProvider;
87

98
class ArticleTest extends ResponseProvider
109
{
11-
/** @var array */
12-
protected $responses = [];
13-
14-
protected $files = [
10+
protected static $staticFiles = [
1511
'Articles/diffbot-sitepoint-basic.json',
1612
// http%3A%2F%2Fwww.sitepoint.com%2Fdiffbot-crawling-visual-machine-learning
1713
'Articles/diffbot-sitepoint-extended.json',
1814
'Articles/apple-watch-verge-basic.json',
1915
// http%3A%2F%2Fwww.theverge.com%2Fa%2Fapple-watch-review
20-
'Articles/apple-watch-verge-extended.json'
16+
'Articles/apple-watch-verge-extended.json',
17+
'Articles/15-11-07/diffbot-sitepoint-basic.json',
2118
];
2219

23-
protected function ei($file)
24-
{
25-
$ef = new Entity();
26-
27-
return $ef->createAppropriateIterator($this->prepareResponses()[$file]);
28-
}
29-
30-
public function returnFiles()
31-
{
32-
$files = [];
33-
foreach ($this->files as $file) {
34-
$files[] = [$file];
35-
}
36-
37-
return $files;
38-
}
39-
4020
/**
4121
* @dataProvider returnFiles
22+
* @param $file
4223
*/
4324
public function testType($file)
4425
{
@@ -303,4 +284,89 @@ public function testDiscussion($file, $articles)
303284
}
304285
}
305286
}
287+
288+
public function siteNameProvider()
289+
{
290+
return [
291+
['Articles/15-11-07/diffbot-sitepoint-basic.json', 'SitePoint'],
292+
];
293+
}
294+
295+
/**
296+
* @dataProvider siteNameProvider
297+
* @param $file
298+
* @param $value1
299+
*/
300+
public function testSiteName($file, $value1)
301+
{
302+
$value1 = (is_array($value1)) ? $value1 : [$value1];
303+
/** @var Article $entity */
304+
foreach ($this->ei($file) as $i => $entity) {
305+
$this->assertEquals($value1[$i], $entity->getSiteName());
306+
}
307+
}
308+
309+
public function publisherCountryProvider()
310+
{
311+
return [
312+
['Articles/15-11-07/diffbot-sitepoint-basic.json', 'Australia'],
313+
];
314+
}
315+
316+
/**
317+
* @dataProvider publisherCountryProvider
318+
* @param $file
319+
* @param $value1
320+
*/
321+
public function testPublisherCountry($file, $value1)
322+
{
323+
$value1 = (is_array($value1)) ? $value1 : [$value1];
324+
/** @var Article $entity */
325+
foreach ($this->ei($file) as $i => $entity) {
326+
$this->assertEquals($value1[$i], $entity->getPublisherCountry());
327+
}
328+
}
329+
330+
public function publisherRegionProvider()
331+
{
332+
return [
333+
['Articles/15-11-07/diffbot-sitepoint-basic.json', 'Australia and New Zealand'],
334+
];
335+
}
336+
337+
/**
338+
* @dataProvider publisherRegionProvider
339+
* @param $file
340+
* @param $value1
341+
*/
342+
public function testPublisherRegion($file, $value1)
343+
{
344+
$value1 = (is_array($value1)) ? $value1 : [$value1];
345+
/** @var Article $entity */
346+
foreach ($this->ei($file) as $i => $entity) {
347+
$this->assertEquals($value1[$i], $entity->getPublisherRegion());
348+
}
349+
}
350+
351+
public function estimatedDateProvider()
352+
{
353+
return [
354+
['Articles/15-11-07/diffbot-sitepoint-basic.json', 'Sun, 27 Jul 2014 00:00:00 GMT'],
355+
];
356+
}
357+
358+
/**
359+
* @dataProvider estimatedDateProvider
360+
* @param $file
361+
* @param $value1
362+
*/
363+
public function testEstimatedDate($file, $value1)
364+
{
365+
$value1 = (is_array($value1)) ? $value1 : [$value1];
366+
/** @var Article $entity */
367+
foreach ($this->ei($file) as $i => $entity) {
368+
$this->assertEquals($value1[$i], $entity->getEstimatedDate());
369+
}
370+
}
371+
306372
}

tests/Entity/CrawlJobTest.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@
1010

1111
class CrawlJobTest extends ResponseProvider
1212
{
13-
/** @var array */
14-
protected $responses = [];
15-
16-
protected $files = [
13+
protected static $staticFiles = [
1714
'Crawlbot/15-05-18/sitepoint_01_maxCrawled.json',
1815
'Crawlbot/15-05-20/multiplejobs01.json'
1916
];
2017

2118
protected function ei($file)
2219
{
23-
$this->prepareResponses();
20+
$responses = parent::prepareResponsesStatic();
2421
/** @var ResponseInterface $response */
25-
$response = $this->responses[$file];
22+
$response = $responses[$file];
2623
$jobs = [];
2724
foreach (json_decode($response->getBody(), true)['jobs'] as $data) {
2825
$jobs[] = new Job($data);
@@ -31,16 +28,6 @@ protected function ei($file)
3128
return new EntityIterator($jobs, $response);
3229
}
3330

34-
public function returnFiles()
35-
{
36-
$files = [];
37-
foreach ($this->files as $file) {
38-
$files[] = [$file];
39-
}
40-
41-
return $files;
42-
}
43-
4431
/**
4532
* @dataProvider returnFiles
4633
*/

tests/Entity/DiscussionTest.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,11 @@
88

99
class DiscussionTest extends ResponseProvider
1010
{
11-
/** @var array */
12-
protected $responses = [];
13-
14-
protected $files = [
11+
protected static $staticFiles = [
1512
'Discussions/15-05-01/sp_discourse_php7_recap.json',
1613
//http%3A%2F%2Fcommunity.sitepoint.com%2Ft%2Fphp7-resource-recap%2F174325%2F14
1714
];
1815

19-
protected function ei($file)
20-
{
21-
$ef = new Entity();
22-
23-
return $ef->createAppropriateIterator($this->prepareResponses()[$file]);
24-
}
25-
26-
public function returnFiles()
27-
{
28-
$files = [];
29-
foreach ($this->files as $file) {
30-
$files[] = [$file];
31-
}
32-
33-
return $files;
34-
}
35-
3616
/**
3717
* @dataProvider returnFiles
3818
* @param $file

0 commit comments

Comments
 (0)