Skip to content
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
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ php:
- 7.0
- hhvm

# This triggers builds to run on the new TravisCI infrastructure.
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
sudo: false

matrix:
allow_failures:
- php: 7.0
Expand All @@ -17,5 +21,4 @@ script:
- phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' && $TRAVIS_PHP_VERSION != '7.0' ]]; then php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover; fi
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
],
"require": {
"php" : ">=5.4.0",
"guzzlehttp/guzzle": "~5.0"
"php-http/client-implementation": "^1.0",
"php-http/utils": "^0.1.0@dev",
"php-http/discovery": "^0.2.0@dev"
},
"require-dev": {
"symfony/var-dumper": "~2",
"phpunit/phpunit": "^5.0"
"phpunit/phpunit": "^5.0",
"php-http/guzzle6-adapter": "~0.2@dev",
"scrutinizer/ocular": "^1.1"
},
"autoload": {
"psr-4": {
Expand All @@ -36,5 +40,7 @@
"branch-alias": {
"dev-master": "0.5-dev"
}
}
},
"prefer-stable": true,
"minimum-stability": "dev"
}
2 changes: 1 addition & 1 deletion src/Api/Crawl.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public function call()
{
$response = $this->diffbot->getHttpClient()->get($this->buildUrl());

$array = $response->json();
$array = json_decode($response->getBody(), true);

if (isset($array['jobs'])) {
$jobs = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function call($info = false)
$ei = parent::call();

set_error_handler(function() { /* ignore errors */ });
$arr = $ei->getResponse()->json(['big_int_strings' => true]);
$arr = json_decode((string)$ei->getResponse()->getBody(), true, 512, 1);
restore_error_handler();

unset($arr['request']);
Expand Down
10 changes: 8 additions & 2 deletions src/Diffbot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Swader\Diffbot;

use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Swader\Diffbot\Api\Crawl;
use Swader\Diffbot\Api\Custom;
use Swader\Diffbot\Api\Search;
Expand All @@ -11,7 +13,7 @@
use Swader\Diffbot\Api\Analyze;
use Swader\Diffbot\Api\Article;
use Swader\Diffbot\Api\Discussion;
use GuzzleHttp\Client;
use Http\Client\Utils\HttpMethodsClient as Client;
use Swader\Diffbot\Factory\Entity;
use Swader\Diffbot\Interfaces\Api;
use Swader\Diffbot\Interfaces\EntityFactory;
Expand Down Expand Up @@ -90,12 +92,16 @@ public function getToken()
* Sets the client to be used for querying the API endpoints
*
* @param Client $client
* @see http://php-http.readthedocs.org/en/latest/utils/#httpmethodsclient
* @return $this
*/
public function setHttpClient(Client $client = null)
{
if ($client === null) {
$client = new Client();
$client = new Client(
HttpClientDiscovery::find(),
MessageFactoryDiscovery::find()
);
}
$this->client = $client;
return $this;
Expand Down
3 changes: 2 additions & 1 deletion src/Entity/EntityIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Swader\Diffbot\Entity;

use GuzzleHttp\Message\ResponseInterface as Response;
use Psr\Http\Message\ResponseInterface as Response;
use Swader\Diffbot\Abstracts\Entity;

class EntityIterator implements \Countable, \Iterator, \ArrayAccess
Expand Down Expand Up @@ -134,6 +134,7 @@ public function offsetGet($offset)
if ($this->offsetExists($offset)) {
return $this->data[$offset];
}
throw new \OutOfBoundsException("Offset '$offset' not present");
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/Factory/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Swader\Diffbot\Factory;

use GuzzleHttp\Message\ResponseInterface as Response;
use Psr\Http\Message\ResponseInterface as Response;
use Swader\Diffbot\Entity\EntityIterator;
use Swader\Diffbot\Exceptions\DiffbotException;
use Swader\Diffbot\Interfaces\EntityFactory;
Expand Down Expand Up @@ -30,9 +30,8 @@ public function createAppropriateIterator(Response $response)
{
$this->checkResponseFormat($response);


set_error_handler(function() { /* ignore errors */ });
$arr = $response->json(['big_int_strings' => true]);
$arr = json_decode((string)$response->getBody(), true, 512, 1);
restore_error_handler();

$objects = [];
Expand All @@ -58,7 +57,7 @@ public function createAppropriateIterator(Response $response)
protected function checkResponseFormat(Response $response)
{
set_error_handler(function() { /* ignore errors */ });
$arr = $response->json(['big_int_strings' => true]);
$arr = json_decode((string)$response->getBody(), true, 512, 1);
restore_error_handler();

if (isset($arr['error'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/EntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Swader\Diffbot\Interfaces;

use GuzzleHttp\Message\ResponseInterface as Response;
use Psr\Http\Message\ResponseInterface as Response;
use Swader\Diffbot\Entity\EntityIterator;

interface EntityFactory
Expand Down
39 changes: 9 additions & 30 deletions tests/Abstracts/EntityIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

namespace Swader\Diffbot\Test;

use GuzzleHttp\Client;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Subscriber\Mock;
use Swader\Diffbot\Abstracts\Entity;
use Swader\Diffbot\Factory\Entity;

class EntityIteratorTest extends \PHPUnit_Framework_TestCase
class EntityIteratorTest extends ResponseProvider
{

/** @var array */
Expand All @@ -19,28 +15,11 @@ class EntityIteratorTest extends \PHPUnit_Framework_TestCase
'Images/multi_images_smittenkitchen.json'
];

protected function prepareResponses()
{
if (empty($this->responses)) {
$mockInput = [];
foreach ($this->files as $file) {
$mockInput[] = file_get_contents(__DIR__ . '/../Mocks/' . $file);
}
unset($file);
$mock = new Mock($mockInput);
$client = new Client();
$client->getEmitter()->attach($mock);
foreach ($this->files as $file) {
$this->responses[$file] = $client->get('sampleurl.com');
}
unset($file);
}
return $this->responses;
}
protected $folder = '/../Mocks/';

public function testBadMethodCall()
{
$ef = new \Swader\Diffbot\Factory\Entity();
$ef = new Entity();
$ei = $ef->createAppropriateIterator($this->prepareResponses()['Images/one_image_zola.json']);

$this->setExpectedException('BadMethodCallException');
Expand All @@ -49,7 +28,7 @@ public function testBadMethodCall()

public function testMagic()
{
$ef = new \Swader\Diffbot\Factory\Entity();
$ef = new Entity();
$ei = $ef->createAppropriateIterator($this->prepareResponses()['Images/one_image_zola.json']);

$this->assertEquals('image', $ei->type);
Expand All @@ -63,7 +42,7 @@ public function testCount()
'Images/multi_images_smittenkitchen.json' => 9
];

$ef = new \Swader\Diffbot\Factory\Entity();
$ef = new Entity();

foreach ($fileExpectations as $fileName => $expectation) {
$ei = $ef->createAppropriateIterator($this->prepareResponses()[$fileName]);
Expand All @@ -73,17 +52,17 @@ public function testCount()

public function testGetResponse()
{
$ef = new \Swader\Diffbot\Factory\Entity();
$ef = new Entity();

foreach ($this->files as $fileName) {
$ei = $ef->createAppropriateIterator($this->prepareResponses()[$fileName]);
$this->assertInstanceOf('GuzzleHttp\Message\Response', $ei->getResponse());
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $ei->getResponse());
}
}

public function testIteration()
{
$ef = new \Swader\Diffbot\Factory\Entity();
$ef = new Entity();
foreach ($this->files as $fileName) {

$ei = $ef->createAppropriateIterator($this->prepareResponses()[$fileName]);
Expand Down
26 changes: 8 additions & 18 deletions tests/Api/AnalyzeApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Swader\Diffbot\Test\Api;

use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Mock;
use Swader\Diffbot\Diffbot;
use Swader\Diffbot\Entity\Article;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;

class AnalyzeApiTest extends \PHPUnit_Framework_TestCase
{
use setterUpper;

protected $validMock;

Expand All @@ -19,27 +18,18 @@ class AnalyzeApiTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$diffbot = $this->getValidDiffbotInstance();
$fakeClient = new Client();
$fakeClient->getEmitter()->attach($this->getValidMock());

$diffbot->setHttpClient($fakeClient);
$diffbot->setEntityFactory();
$diffbot = $this->preSetUp();

$this->apiWithMock = $diffbot->createAnalyzeAPI('https://article-mock.com');
}

protected function getValidDiffbotInstance()
{
return new Diffbot('demo');
}

protected function getValidMock()
{
if (!$this->validMock) {
$this->validMock = new Mock(
[file_get_contents(__DIR__ . '/../Mocks/Articles/hi_quicktip_basic.json')]
);
$this->validMock = new MockHandler([
new Response(200, [],
file_get_contents(__DIR__ . '/../Mocks/Articles/hi_quicktip_basic.json'))
]);
}

return $this->validMock;
Expand Down
26 changes: 8 additions & 18 deletions tests/Api/ArticleApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Swader\Diffbot\Test\Api;

use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Mock;
use Swader\Diffbot\Diffbot;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use Swader\Diffbot\Entity\Article;

class ArticleApiTest extends \PHPUnit_Framework_TestCase
{
use setterUpper;

protected $validMock;

Expand All @@ -19,27 +19,17 @@ class ArticleApiTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$diffbot = $this->getValidDiffbotInstance();
$fakeClient = new Client();
$fakeClient->getEmitter()->attach($this->getValidMock());

$diffbot->setHttpClient($fakeClient);
$diffbot->setEntityFactory();

$diffbot = $this->preSetUp();
$this->apiWithMock = $diffbot->createArticleAPI('https://article-mock.com');
}

protected function getValidDiffbotInstance()
{
return new Diffbot('demo');
}

protected function getValidMock()
{
if (!$this->validMock) {
$this->validMock = new Mock(
[file_get_contents(__DIR__ . '/../Mocks/Articles/hi_quicktip_basic.json')]
);
$this->validMock = new MockHandler([
new Response(200, [],
file_get_contents(__DIR__ . '/../Mocks/Articles/hi_quicktip_basic.json'))
]);
}

return $this->validMock;
Expand Down
Loading