Skip to content

Commit ec30eff

Browse files
committed
Merge pull request #32 from Swader/feature-httplug
Converted to PHP-HTTP and PSR-7.
2 parents e434358 + 436ed2a commit ec30eff

File tree

68 files changed

+1458
-846
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1458
-846
lines changed

.travis.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ php:
55
- 7.0
66
- hhvm
77

8+
# This triggers builds to run on the new TravisCI infrastructure.
9+
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
10+
sudo: false
11+
812
matrix:
913
allow_failures:
1014
- php: 7.0
@@ -17,5 +21,4 @@ script:
1721
- phpunit --coverage-text --coverage-clover=coverage.clover
1822

1923
after_script:
20-
- wget https://scrutinizer-ci.com/ocular.phar
21-
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
24+
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' && $TRAVIS_PHP_VERSION != '7.0' ]]; then php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover; fi

composer.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
],
1717
"require": {
1818
"php" : ">=5.4.0",
19-
"guzzlehttp/guzzle": "~5.0"
19+
"php-http/client-implementation": "^1.0",
20+
"php-http/utils": "^0.1.0@dev",
21+
"php-http/discovery": "^0.2.0@dev"
2022
},
2123
"require-dev": {
2224
"symfony/var-dumper": "~2",
23-
"phpunit/phpunit": "^5.0"
25+
"phpunit/phpunit": "^5.0",
26+
"php-http/guzzle6-adapter": "~0.2@dev",
27+
"scrutinizer/ocular": "^1.1"
2428
},
2529
"autoload": {
2630
"psr-4": {
@@ -36,5 +40,7 @@
3640
"branch-alias": {
3741
"dev-master": "0.5-dev"
3842
}
39-
}
43+
},
44+
"prefer-stable": true,
45+
"minimum-stability": "dev"
4046
}

src/Api/Crawl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ public function call()
442442
{
443443
$response = $this->diffbot->getHttpClient()->get($this->buildUrl());
444444

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

447447
if (isset($array['jobs'])) {
448448
$jobs = [];

src/Api/Search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function call($info = false)
138138
$ei = parent::call();
139139

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

144144
unset($arr['request']);

src/Diffbot.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Swader\Diffbot;
44

5+
use Http\Discovery\HttpClientDiscovery;
6+
use Http\Discovery\MessageFactoryDiscovery;
57
use Swader\Diffbot\Api\Crawl;
68
use Swader\Diffbot\Api\Custom;
79
use Swader\Diffbot\Api\Search;
@@ -11,7 +13,7 @@
1113
use Swader\Diffbot\Api\Analyze;
1214
use Swader\Diffbot\Api\Article;
1315
use Swader\Diffbot\Api\Discussion;
14-
use GuzzleHttp\Client;
16+
use Http\Client\Utils\HttpMethodsClient as Client;
1517
use Swader\Diffbot\Factory\Entity;
1618
use Swader\Diffbot\Interfaces\Api;
1719
use Swader\Diffbot\Interfaces\EntityFactory;
@@ -90,12 +92,16 @@ public function getToken()
9092
* Sets the client to be used for querying the API endpoints
9193
*
9294
* @param Client $client
95+
* @see http://php-http.readthedocs.org/en/latest/utils/#httpmethodsclient
9396
* @return $this
9497
*/
9598
public function setHttpClient(Client $client = null)
9699
{
97100
if ($client === null) {
98-
$client = new Client();
101+
$client = new Client(
102+
HttpClientDiscovery::find(),
103+
MessageFactoryDiscovery::find()
104+
);
99105
}
100106
$this->client = $client;
101107
return $this;

src/Entity/EntityIterator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Swader\Diffbot\Entity;
44

5-
use GuzzleHttp\Message\ResponseInterface as Response;
5+
use Psr\Http\Message\ResponseInterface as Response;
66
use Swader\Diffbot\Abstracts\Entity;
77

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

139140
/**

src/Factory/Entity.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Swader\Diffbot\Factory;
44

5-
use GuzzleHttp\Message\ResponseInterface as Response;
5+
use Psr\Http\Message\ResponseInterface as Response;
66
use Swader\Diffbot\Entity\EntityIterator;
77
use Swader\Diffbot\Exceptions\DiffbotException;
88
use Swader\Diffbot\Interfaces\EntityFactory;
@@ -30,9 +30,8 @@ public function createAppropriateIterator(Response $response)
3030
{
3131
$this->checkResponseFormat($response);
3232

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

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

6463
if (isset($arr['error'])) {

src/Interfaces/EntityFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Swader\Diffbot\Interfaces;
44

5-
use GuzzleHttp\Message\ResponseInterface as Response;
5+
use Psr\Http\Message\ResponseInterface as Response;
66
use Swader\Diffbot\Entity\EntityIterator;
77

88
interface EntityFactory

tests/Abstracts/EntityIteratorTest.php

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
namespace Swader\Diffbot\Test;
44

5-
use GuzzleHttp\Client;
6-
use GuzzleHttp\Message\Response;
7-
use GuzzleHttp\Stream\Stream;
8-
use GuzzleHttp\Subscriber\Mock;
9-
use Swader\Diffbot\Abstracts\Entity;
5+
use Swader\Diffbot\Factory\Entity;
106

11-
class EntityIteratorTest extends \PHPUnit_Framework_TestCase
7+
class EntityIteratorTest extends ResponseProvider
128
{
139

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

22-
protected function prepareResponses()
23-
{
24-
if (empty($this->responses)) {
25-
$mockInput = [];
26-
foreach ($this->files as $file) {
27-
$mockInput[] = file_get_contents(__DIR__ . '/../Mocks/' . $file);
28-
}
29-
unset($file);
30-
$mock = new Mock($mockInput);
31-
$client = new Client();
32-
$client->getEmitter()->attach($mock);
33-
foreach ($this->files as $file) {
34-
$this->responses[$file] = $client->get('sampleurl.com');
35-
}
36-
unset($file);
37-
}
38-
return $this->responses;
39-
}
18+
protected $folder = '/../Mocks/';
4019

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

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

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

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

66-
$ef = new \Swader\Diffbot\Factory\Entity();
45+
$ef = new Entity();
6746

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

7453
public function testGetResponse()
7554
{
76-
$ef = new \Swader\Diffbot\Factory\Entity();
55+
$ef = new Entity();
7756

7857
foreach ($this->files as $fileName) {
7958
$ei = $ef->createAppropriateIterator($this->prepareResponses()[$fileName]);
80-
$this->assertInstanceOf('GuzzleHttp\Message\Response', $ei->getResponse());
59+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $ei->getResponse());
8160
}
8261
}
8362

8463
public function testIteration()
8564
{
86-
$ef = new \Swader\Diffbot\Factory\Entity();
65+
$ef = new Entity();
8766
foreach ($this->files as $fileName) {
8867

8968
$ei = $ef->createAppropriateIterator($this->prepareResponses()[$fileName]);

tests/Api/AnalyzeApiTest.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace Swader\Diffbot\Test\Api;
44

5-
use GuzzleHttp\Client;
6-
use GuzzleHttp\Subscriber\Mock;
7-
use Swader\Diffbot\Diffbot;
8-
use Swader\Diffbot\Entity\Article;
5+
use GuzzleHttp\Handler\MockHandler;
6+
use GuzzleHttp\Psr7\Response;
97

108
class AnalyzeApiTest extends \PHPUnit_Framework_TestCase
119
{
10+
use setterUpper;
1211

1312
protected $validMock;
1413

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

2019
protected function setUp()
2120
{
22-
$diffbot = $this->getValidDiffbotInstance();
23-
$fakeClient = new Client();
24-
$fakeClient->getEmitter()->attach($this->getValidMock());
25-
26-
$diffbot->setHttpClient($fakeClient);
27-
$diffbot->setEntityFactory();
21+
$diffbot = $this->preSetUp();
2822

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

32-
protected function getValidDiffbotInstance()
33-
{
34-
return new Diffbot('demo');
35-
}
36-
3726
protected function getValidMock()
3827
{
3928
if (!$this->validMock) {
40-
$this->validMock = new Mock(
41-
[file_get_contents(__DIR__ . '/../Mocks/Articles/hi_quicktip_basic.json')]
42-
);
29+
$this->validMock = new MockHandler([
30+
new Response(200, [],
31+
file_get_contents(__DIR__ . '/../Mocks/Articles/hi_quicktip_basic.json'))
32+
]);
4333
}
4434

4535
return $this->validMock;

0 commit comments

Comments
 (0)