Repository navigation
Expand file tree
/
Copy pathDiffbotTest.php
More file actions
172 lines (148 loc) · 4.51 KB
/
Copy pathDiffbotTest.php
File metadata and controls
172 lines (148 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Swader\Diffbot\Test;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Http\Adapter\Guzzle6\Client as Guzzle6;
use Http\Client\Common\HttpMethodsClient;
use Http\Discovery\MessageFactoryDiscovery;
use Swader\Diffbot\Diffbot;
class DiffbotTest extends \PHPUnit_Framework_TestCase
{
public function invalidTokens()
{
return [
'empty' => [''],
'a' => ['a'],
'ab' => ['ab'],
'abc' => ['abc'],
'digit' => [1],
'double-digit' => [12],
'triple-digit' => [123],
'bool' => [true],
'array' => [['token']],
];
}
public function validTokens()
{
return [
'token' => ['token'],
'short-hash' => ['123456789'],
'full-hash' => ['akrwejhtn983z420qrzc8397r4'],
];
}
/**
* @dataProvider invalidTokens
* @param $token
*/
public function testSetTokenRaisesExceptionOnInvalidToken($token)
{
$this->setExpectedException('InvalidArgumentException');
Diffbot::setToken($token);
}
/**
* @dataProvider validTokens
* @param $token
*/
public function testSetTokenSucceedsOnValidToken($token)
{
Diffbot::setToken($token);
$bot = new Diffbot();
$this->assertInstanceOf('\Swader\Diffbot\Diffbot', $bot);
}
/**
* @runInSeparateProcess
*/
public function testInstantiationWithNoGlobalTokenAndNoArgumentRaisesAnException(
)
{
$this->setExpectedException('\Swader\Diffbot\Exceptions\DiffbotException');
new Diffbot();
}
public function testInstantiationWithGlobalTokenAndNoArgumentSucceeds()
{
Diffbot::setToken('token');
$bot = new Diffbot();
$this->assertInstanceOf('Swader\Diffbot\Diffbot', $bot);
}
public function testInstantiationWithNoGlobalTokenButWithArgumentSucceeds()
{
$bot = new Diffbot('token');
$this->assertInstanceOf('Swader\Diffbot\Diffbot', $bot);
}
public function testGetToken()
{
Diffbot::setToken('testing');
$d1 = new Diffbot;
$this->assertEquals('testing', $d1->getToken());
$sampleToken = 'abcdef';
$d2 = new Diffbot($sampleToken);
$this->assertEquals($sampleToken, $d2->getToken());
}
public function testSetHttpClient()
{
$bot = new Diffbot('token');
$mock = new MockHandler([
new Response(200, [],
file_get_contents(__DIR__ . '/Mocks/Products/dogbrush.json'))
]);
$handler = HandlerStack::create($mock);
$guzzleClient = new Client(['handler' => $handler]);
$methodsClient = new HttpMethodsClient(
new Guzzle6($guzzleClient),
MessageFactoryDiscovery::find());
try {
$bot->setHttpClient($methodsClient);
} catch (\Exception $e) {
$this->fail("Could not set fake client: " . $e->getMessage());
}
}
public function methodnameProvider()
{
return [
['product'],
['image'],
['analyze'],
['article'],
['discussion']
];
}
/**
* @param $methodNamePart
* @dataProvider methodnameProvider
*/
public function testAPIcreation($methodNamePart)
{
$bot = new Diffbot('token');
$methodName = 'create' . ucfirst($methodNamePart) . 'API';
$api = $bot->$methodName('https://foolink.bar');
$this->assertInstanceOf(
'Swader\Diffbot\Api\\' . ucfirst($methodNamePart), $api
);
}
public function testCustomApiCreation()
{
$bot = new Diffbot('token');
$api = $bot->createCustomAPI('http://someurl.com', 'apiName');
$this->assertInstanceOf(
'Swader\Diffbot\Api\Custom', $api
);
}
public function testCrawlCreation()
{
$bot = new Diffbot('token');
$api = $bot->crawl('test');
$this->assertInstanceOf(
'Swader\Diffbot\Api\Crawl', $api
);
}
public function testSearchCreation()
{
$bot = new Diffbot('token');
$api = $bot->search('test');
$this->assertInstanceOf(
'Swader\Diffbot\Api\Search', $api
);
}
}