forked from dailymotion/dailymotion-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDailymotionTest.php
181 lines (158 loc) · 4.96 KB
/
DailymotionTest.php
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
173
174
175
176
177
178
179
180
181
<?php
$apiKey = null;
$apiSecret = null;
$testUser = null;
$testPassword = null;
$testVideoFile = null;
$proxy = null;
$apiEndpointUrl = null;
$oauthAuthorizeEndpointUrl = null;
$oauthTokenEndpointUrl = null;
@include 'local_config.php';
if (!function_exists('readline'))
{
function readline($prompt = '')
{
echo $prompt;
return rtrim(fgets(STDIN), "\n");
}
}
if (!$apiKey) $apiKey = readline('API Key: ');
if (!$apiSecret) $apiSecret = readline('API Secret: ');
if (!$testUser) $testUser = readline('Test User: ');
if (!$testPassword) $testPassword = readline('Test User Password: ');
if (!$testVideoFile) $testVideoFile = readline('Test Video File: ');
require_once 'Dailymotion.php';
class DailymotionTest extends PHPUnit_Framework_TestCase
{
protected
$api = null,
$apiKey = null,
$apiSecret = null;
protected function setUp()
{
global $apiKey,
$apiSecret,
$proxy,
$apiEndpointUrl,
$oauthAuthorizeEndpointUrl,
$oauthTokenEndpointUrl;
$this->api = new Dailymotion();
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
if (!empty($proxy))
{
$this->api->proxy = $proxy;
}
if (!empty($apiEndpointUrl))
{
$this->api->apiEndpointUrl = $apiEndpointUrl;
}
if (!empty($oauthAuthorizeEndpointUrl))
{
$this->api->oauthAuthorizeEndpointUrl = $oauthAuthorizeEndpointUrl;
}
if (!empty($oauthTokenEndpointUrl))
{
$this->api->oauthTokenEndpointUrl = $oauthTokenEndpointUrl;
}
}
public function testNoGrantTypePublicData()
{
$message = 'test';
$result = $this->api->get('/echo', array('message' => $message));
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('message', $result);
$this->assertContains($message, $result);
}
/**
* @expectedException DailymotionApiException
*/
public function testError()
{
$this->api->get('/echo');
}
/**
* @expectedException DailymotionAuthRequiredException
*/
public function testNoGrantTypeAuthRequired()
{
$result = $this->api->get('/videos/subscriptions');
}
/**
* @expectedException DailymotionApiException
*/
public function testNoGrantTypeAuthRequiredMe()
{
$result = $this->api->get('/me');
}
public function testGrantTypeClientCredentials()
{
$this->api->setGrantType(
Dailymotion::GRANT_TYPE_CLIENT_CREDENTIALS,
$this->apiKey, $this->apiSecret
);
$result = $this->api->get('/auth');
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('username', $result);
}
public function testGrantTypePassword()
{
global $testUser,
$testPassword;
$this->api->setGrantType(
Dailymotion::GRANT_TYPE_PASSWORD,
$this->apiKey, $this->apiSecret, array(),
array('username' => $testUser, 'password' => $testPassword)
);
$result = $this->api->get('/auth');
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('username', $result);
$this->assertEquals($result['username'], $testUser);
}
/**
* @expectedException DailymotionAuthRequiredException
*/
public function testGrantTypeChangeFromSessionRequired()
{
try
{
$this->api->setGrantType(
Dailymotion::GRANT_TYPE_CLIENT_CREDENTIALS,
$this->apiKey, $this->apiSecret
);
$result = $this->api->get('/auth');
$this->assertInternalType('array', $result);
}
catch (DailymotionAuthRequiredException $e)
{
// Test must not succeed if this call throws the exception
}
$this->api->setGrantType(
Dailymotion::GRANT_TYPE_PASSWORD,
$this->apiKey, $this->apiSecret
);
$this->api->get('/auth'); // should throw auth required exception
}
public function testVideoUpload()
{
global $testUser,
$testPassword,
$testVideoFile;
$this->api->setGrantType(
Dailymotion::GRANT_TYPE_PASSWORD,
$this->apiKey,
$this->apiSecret,
array('read', 'write', 'delete', 'manage_videos'),
array('username' => $testUser, 'password' => $testPassword)
);
$url = $this->api->uploadFile($testVideoFile);
$this->assertInternalType('string', $url);
$this->assertContains('http://', $url);
$result = $this->api->post('/me/videos', array('url' => $url));
$this->assertArrayHasKey('id', $result);
sleep(2);
$result = $this->api->delete("/video/{$result['id']}");
$this->assertInternalType('array', $result);
}
}