Skip to content

Commit 7a444f6

Browse files
committed
Updated for Part 3 start
1 parent 2dd842f commit 7a444f6

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

src/Abstracts/Api.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ abstract class Api
1111
/** @var int Timeout value in ms - defaults to 30s if empty */
1212
private $timeout = 30000;
1313

14+
/** @var string The URL onto which to unleash the API in question */
15+
private $url;
16+
17+
public function __construct($url)
18+
{
19+
if (!is_string($url)) {
20+
throw new \InvalidArgumentException('URL param must be a string.');
21+
}
22+
$url = trim($url);
23+
if (strlen($url) < 4) {
24+
throw new \InvalidArgumentException('URL must be at least four characters in length');
25+
}
26+
if ($parts = parse_url($url)) {
27+
if (!isset($parts["scheme"])) {
28+
$url = "http://$url";
29+
}
30+
}
31+
$filtered_url = filter_var($url, FILTER_VALIDATE_URL);
32+
if (false === $filtered_url) {
33+
throw new \InvalidArgumentException('You provided an invalid URL: ' . $url);
34+
}
35+
36+
$this->url = $filtered_url;
37+
}
38+
1439
/**
1540
* Setting the timeout will define how long Diffbot will keep trying
1641
* to fetch the API results. A timeout can happen for various reasons, from

tests/Abstracts/ApiTest.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
class ApiTest extends \PHPUnit_Framework_TestCase
88
{
9+
private $className = 'Swader\Diffbot\Abstracts\Api';
10+
private $testUrl = 'http://diffbot.com';
11+
912
/**
1013
* @return \PHPUnit_Framework_MockObject_MockObject
1114
*/
1215
private function buildMock()
1316
{
14-
return $this->getMockForAbstractClass('Swader\Diffbot\Abstracts\Api');
17+
return $this->getMockForAbstractClass($this->className, [$this->testUrl]);
1518
}
1619

1720
public function testSetTimeout()
@@ -61,4 +64,56 @@ public function testSetTimeout()
6164
$this->fail('Failed, assumed invalid parameter was valid.');
6265
}
6366
}
67+
68+
public function testConstructor()
69+
{
70+
$validUrls = [
71+
'http://google.com',
72+
'http://gigaom.com/cloud/silicon-valley-royalty-pony-up-2m-to-scale-diffbots-visual-learning-robot/',
73+
<<<'TAG'
74+
http://techcrunch.com/2012/05/31/diffbot-raises-2-million-seed-round-for-web-content-extraction-technology/
75+
TAG
76+
,
77+
'http://www.theverge.com/2012/5/31/3054444/diffbot-raises-2-million-apps-open-web',
78+
'http://venturebeat.com/2012/08/16/diffbot-api-links',
79+
'http://www.wired.co.uk/news/archive/2012-06/01/diffbot',
80+
'http://www.amazon.com/Oh-The-Places-Youll-Go/dp/0679805273/',
81+
'http://us.levi.com/product/index.jsp?productId=2076855',
82+
<<<'TAG'
83+
http://www.petsmart.com/dog/grooming-supplies/grreat-choice-soft-slicker-dog-brush-zid36-12094/cat-36-catid-100016
84+
TAG
85+
,
86+
'http://instagram.com/p/t879OvgvqS/',
87+
'http://smittenkitchen.com/blog/2012/01/buckwheat-baby-with-salted-caramel-syrup/',
88+
'https://twitter.com/NASA/status/525397368116895744',
89+
'www.example.com',
90+
'example.com'
91+
];
92+
93+
$invalidUrls = [
94+
false,
95+
null,
96+
12345,
97+
'abc',
98+
'35tugz---sdf----?//*****/*//*'
99+
];
100+
101+
foreach ($validUrls as $i => $url) {
102+
try {
103+
$this->getMockForAbstractClass($this->className, [$url]);
104+
} catch (\InvalidArgumentException $e) {
105+
$this->fail('Failed with supposedly valid URL: ' . $url . ' at index ' . $i);
106+
}
107+
}
108+
109+
foreach ($invalidUrls as $i => $url) {
110+
try {
111+
$this->getMockForAbstractClass($this->className, [$url]);
112+
} catch (\InvalidArgumentException $e) {
113+
continue;
114+
}
115+
$this->fail('Did not fail with invalid URL at index ' . $i);
116+
}
117+
118+
}
64119
}

0 commit comments

Comments
 (0)