Skip to content

Fixes #19 and optimizes tests a bit #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions src/Entity/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getDate()
*/
public function getAuthor()
{
return (isset($this->data['author'])) ? $this->data['author'] : null;
return $this->getOrDefault('author');
}

/**
Expand All @@ -75,7 +75,7 @@ public function getAuthor()
*/
public function getAuthorUrl()
{
return (isset($this->data['authorUrl'])) ? $this->data['authorUrl'] : null;
return $this->getOrDefault('authorUrl');
}

/**
Expand Down Expand Up @@ -118,7 +118,7 @@ public function getTags()
*/
public function getNumPages()
{
return (isset($this->data['numPages'])) ? $this->data['numPages'] : 1;
return $this->getOrDefault('numPages', 1);
}

/**
Expand All @@ -129,7 +129,7 @@ public function getNumPages()
*/
public function getNextPages()
{
return (isset($this->data['nextPages'])) ? $this->data['nextPages'] : [];
return $this->getOrDefault('nextPages', []);
}

/**
Expand All @@ -139,7 +139,7 @@ public function getNextPages()
*/
public function getSentiment()
{
return (isset($this->data['sentiment'])) ? $this->data['sentiment'] : null;
return $this->getOrDefault('sentiment');
}

/**
Expand Down Expand Up @@ -172,7 +172,7 @@ public function getSentiment()
*/
public function getImages()
{
return (isset($this->data['images'])) ? $this->data['images'] : [];
return $this->getOrDefault('images', []);
}

/**
Expand All @@ -199,7 +199,7 @@ public function getImages()
*/
public function getVideos()
{
return (isset($this->data['videos'])) ? $this->data['videos'] : [];
return $this->getOrDefault('videos', []);
}

/**
Expand All @@ -210,4 +210,48 @@ public function getDiscussion()
{
return $this->discussion;
}

/**
* The plain-text name of the site (e.g. The New York Times or Diffbot).
*
* If no site name is automatically determined, the root domain (diffbot.com) will be returned.
*
* @return string | null
*/
public function getSiteName()
{
return $this->getOrDefault('siteName');
}

/**
* If known, the country of the article publication.
*
* @return string | null
*/
public function getPublisherCountry()
{
return $this->getOrDefault('publisherCountry', null);
}

/**
* If known, the region of the article publication.
*
* @return string | null
*/
public function getPublisherRegion()
{
return $this->getOrDefault('publisherRegion', null);
}

/**
* If an article's date is ambiguous, Diffbot will attempt to estimate a
* more specific timestamp using various factors. This will not be
* generated for articles older than two days, or articles without an identified date.
*
* @return string | null
*/
public function getEstimatedDate()
{
return $this->getOrDefault('estimatedDate', $this->getDate());
}
}
6 changes: 6 additions & 0 deletions src/Traits/StandardEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,10 @@ public function getDiffbotUri()
return $this->data['diffbotUri'];
}

protected function getOrDefault($key, $default = null, $data = null)
{
$data = ($data !== null) ?: $this->data;
return (isset($data[$key]) ? $data[$key] : $default);
}

}
20 changes: 1 addition & 19 deletions tests/Entity/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,10 @@
class AbstractTest extends ResponseProvider
{

/** @var array */
protected $responses = [];

protected $files = [
protected static $staticFiles = [
'Products/dogbrush.json'
];

protected function ei($file)
{
$ef = new Entity();
return $ef->createAppropriateIterator($this->prepareResponses()[$file]);
}

public function returnFiles()
{
$files = [];
foreach ($this->files as $file) {
$files[] = [$file];
}
return $files;
}

public function queryStringProvider()
{
return [
Expand Down
112 changes: 89 additions & 23 deletions tests/Entity/ArticleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,23 @@
namespace Swader\Diffbot\Test\Entity;

use Swader\Diffbot\Entity\Article;
use Swader\Diffbot\Factory\Entity;
use Swader\Diffbot\Test\ResponseProvider;

class ArticleTest extends ResponseProvider
{
/** @var array */
protected $responses = [];

protected $files = [
protected static $staticFiles = [
'Articles/diffbot-sitepoint-basic.json',
// http%3A%2F%2Fwww.sitepoint.com%2Fdiffbot-crawling-visual-machine-learning
'Articles/diffbot-sitepoint-extended.json',
'Articles/apple-watch-verge-basic.json',
// http%3A%2F%2Fwww.theverge.com%2Fa%2Fapple-watch-review
'Articles/apple-watch-verge-extended.json'
'Articles/apple-watch-verge-extended.json',
'Articles/15-11-07/diffbot-sitepoint-basic.json',
];

protected function ei($file)
{
$ef = new Entity();

return $ef->createAppropriateIterator($this->prepareResponses()[$file]);
}

public function returnFiles()
{
$files = [];
foreach ($this->files as $file) {
$files[] = [$file];
}

return $files;
}

/**
* @dataProvider returnFiles
* @param $file
*/
public function testType($file)
{
Expand Down Expand Up @@ -303,4 +284,89 @@ public function testDiscussion($file, $articles)
}
}
}

public function siteNameProvider()
{
return [
['Articles/15-11-07/diffbot-sitepoint-basic.json', 'SitePoint'],
];
}

/**
* @dataProvider siteNameProvider
* @param $file
* @param $value1
*/
public function testSiteName($file, $value1)
{
$value1 = (is_array($value1)) ? $value1 : [$value1];
/** @var Article $entity */
foreach ($this->ei($file) as $i => $entity) {
$this->assertEquals($value1[$i], $entity->getSiteName());
}
}

public function publisherCountryProvider()
{
return [
['Articles/15-11-07/diffbot-sitepoint-basic.json', 'Australia'],
];
}

/**
* @dataProvider publisherCountryProvider
* @param $file
* @param $value1
*/
public function testPublisherCountry($file, $value1)
{
$value1 = (is_array($value1)) ? $value1 : [$value1];
/** @var Article $entity */
foreach ($this->ei($file) as $i => $entity) {
$this->assertEquals($value1[$i], $entity->getPublisherCountry());
}
}

public function publisherRegionProvider()
{
return [
['Articles/15-11-07/diffbot-sitepoint-basic.json', 'Australia and New Zealand'],
];
}

/**
* @dataProvider publisherRegionProvider
* @param $file
* @param $value1
*/
public function testPublisherRegion($file, $value1)
{
$value1 = (is_array($value1)) ? $value1 : [$value1];
/** @var Article $entity */
foreach ($this->ei($file) as $i => $entity) {
$this->assertEquals($value1[$i], $entity->getPublisherRegion());
}
}

public function estimatedDateProvider()
{
return [
['Articles/15-11-07/diffbot-sitepoint-basic.json', 'Sun, 27 Jul 2014 00:00:00 GMT'],
];
}

/**
* @dataProvider estimatedDateProvider
* @param $file
* @param $value1
*/
public function testEstimatedDate($file, $value1)
{
$value1 = (is_array($value1)) ? $value1 : [$value1];
/** @var Article $entity */
foreach ($this->ei($file) as $i => $entity) {
$this->assertEquals($value1[$i], $entity->getEstimatedDate());
}
}

}
19 changes: 3 additions & 16 deletions tests/Entity/CrawlJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@

class CrawlJobTest extends ResponseProvider
{
/** @var array */
protected $responses = [];

protected $files = [
protected static $staticFiles = [
'Crawlbot/15-05-18/sitepoint_01_maxCrawled.json',
'Crawlbot/15-05-20/multiplejobs01.json'
];

protected function ei($file)
{
$this->prepareResponses();
$responses = parent::prepareResponsesStatic();
/** @var ResponseInterface $response */
$response = $this->responses[$file];
$response = $responses[$file];
$jobs = [];
foreach (json_decode($response->getBody(), true)['jobs'] as $data) {
$jobs[] = new Job($data);
Expand All @@ -31,16 +28,6 @@ protected function ei($file)
return new EntityIterator($jobs, $response);
}

public function returnFiles()
{
$files = [];
foreach ($this->files as $file) {
$files[] = [$file];
}

return $files;
}

/**
* @dataProvider returnFiles
*/
Expand Down
22 changes: 1 addition & 21 deletions tests/Entity/DiscussionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,11 @@

class DiscussionTest extends ResponseProvider
{
/** @var array */
protected $responses = [];

protected $files = [
protected static $staticFiles = [
'Discussions/15-05-01/sp_discourse_php7_recap.json',
//http%3A%2F%2Fcommunity.sitepoint.com%2Ft%2Fphp7-resource-recap%2F174325%2F14
];

protected function ei($file)
{
$ef = new Entity();

return $ef->createAppropriateIterator($this->prepareResponses()[$file]);
}

public function returnFiles()
{
$files = [];
foreach ($this->files as $file) {
$files[] = [$file];
}

return $files;
}

/**
* @dataProvider returnFiles
* @param $file
Expand Down
Loading