Skip to content

Commit 2dd842f

Browse files
committed
Part 2 done
1 parent 7c1e156 commit 2dd842f

File tree

7 files changed

+181
-45
lines changed

7 files changed

+181
-45
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
}
1616
],
1717
"require": {
18-
"php" : ">=5.5.0"
18+
"php" : ">=5.5.0",
19+
"guzzlehttp/guzzle": "~5.0"
1920
},
2021
"require-dev": {
21-
"phpunit/phpunit" : "4.*"
22+
"phpunit/phpunit" : "~4"
2223
},
2324
"autoload": {
2425
"psr-4": {

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
processIsolation="false"
1111
stopOnFailure="false">
1212
<testsuites>
13-
<testsuite name="League Test Suite">
13+
<testsuite name="Diffbot Test Suite">
1414
<directory>tests</directory>
1515
</testsuite>
1616
</testsuites>

src/Abstracts/Api.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Abstracts;
4+
5+
/**
6+
* Class Api
7+
* @package Swader\Diffbot\Abstracts
8+
*/
9+
abstract class Api
10+
{
11+
/** @var int Timeout value in ms - defaults to 30s if empty */
12+
private $timeout = 30000;
13+
14+
/**
15+
* Setting the timeout will define how long Diffbot will keep trying
16+
* to fetch the API results. A timeout can happen for various reasons, from
17+
* Diffbot's failure, to the site being crawled being exceptionally slow, and more.
18+
*
19+
* @param int|null $timeout Defaults to 30000 even if not set
20+
*
21+
* @return $this
22+
*/
23+
public function setTimeout($timeout = null)
24+
{
25+
if ($timeout === null) {
26+
$timeout = 30000;
27+
}
28+
if (!is_int($timeout)) {
29+
throw new \InvalidArgumentException('Parameter is not an integer');
30+
}
31+
if ($timeout < 0) {
32+
throw new \InvalidArgumentException('Parameter is negative. Only positive timeouts accepted.');
33+
}
34+
35+
$this->timeout = $timeout;
36+
return $this;
37+
}
38+
}

src/SkeletonClass.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/Abstracts/ApiTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Test;
4+
5+
use Swader\Diffbot\Abstracts\Api;
6+
7+
class ApiTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @return \PHPUnit_Framework_MockObject_MockObject
11+
*/
12+
private function buildMock()
13+
{
14+
return $this->getMockForAbstractClass('Swader\Diffbot\Abstracts\Api');
15+
}
16+
17+
public function testSetTimeout()
18+
{
19+
/** @var Api $mock */
20+
$mock = $this->buildMock();
21+
22+
$validTimeouts = [
23+
0,
24+
1000,
25+
2000,
26+
3000,
27+
3000000,
28+
40000000,
29+
null
30+
];
31+
32+
$invalidTimeouts = [
33+
-298979879827,
34+
-4983,
35+
'abcef',
36+
'',
37+
false
38+
];
39+
40+
try {
41+
$mock->setTimeout();
42+
} catch (\InvalidArgumentException $e) {
43+
$this->fail('Failed with supposedly valid (empty) timeout.');
44+
}
45+
46+
foreach ($validTimeouts as $timeout) {
47+
try {
48+
$mock->setTimeout($timeout);
49+
} catch (\InvalidArgumentException $e) {
50+
$this->fail('Failed with supposedly valid timeout: ' . $timeout);
51+
}
52+
}
53+
54+
foreach ($invalidTimeouts as $timeout) {
55+
try {
56+
$mock->setTimeout($timeout);
57+
} catch (\InvalidArgumentException $e) {
58+
// Got expected exception
59+
continue;
60+
}
61+
$this->fail('Failed, assumed invalid parameter was valid.');
62+
}
63+
}
64+
}

tests/DiffbotTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Test;
4+
5+
use Swader\Diffbot\Diffbot;
6+
use Swader\Diffbot\Exceptions\DiffbotException;
7+
8+
/**
9+
* @runTestsInSeparateProcesses
10+
*/
11+
class DiffbotTest extends \PHPUnit_Framework_TestCase
12+
{
13+
14+
private $invalidTokens = [
15+
'',
16+
'a',
17+
'ab',
18+
'abc',
19+
1,
20+
12,
21+
123,
22+
true,
23+
array('token')
24+
];
25+
26+
private $validTokens = [
27+
'token',
28+
'123456789',
29+
'akrwejhtn983z420qrzc8397r4'
30+
];
31+
32+
public function testStaticSetToken()
33+
{
34+
foreach ($this->invalidTokens as $value) {
35+
try {
36+
Diffbot::setToken($value);
37+
} catch (\InvalidArgumentException $e) {
38+
// Good, we got an exception!
39+
continue;
40+
}
41+
$this->fail('Expected exception not raised on value: "' . $value . '".');
42+
}
43+
44+
foreach ($this->validTokens as $value) {
45+
Diffbot::setToken($value);
46+
}
47+
}
48+
49+
public function testInstantiation()
50+
{
51+
$exceptionTriggered = false;
52+
try {
53+
new Diffbot();
54+
} catch (DiffbotException $e) {
55+
// Great, got it!
56+
$exceptionTriggered = true;
57+
}
58+
if (!$exceptionTriggered) {
59+
$this->fail('Empty token did not produce exception!');
60+
}
61+
62+
try {
63+
Diffbot::setToken('token');
64+
new Diffbot();
65+
} catch (DiffbotException $e) {
66+
$this->fail('Scenario failed!');
67+
}
68+
69+
try {
70+
new Diffbot('token');
71+
} catch (DiffbotException $e) {
72+
$this->fail('Scenario failed!');
73+
}
74+
}
75+
}

tests/ExampleTest.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)