Soundcloud_Test.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. require_once 'Soundcloud_Test_Helper.php';
  3. class Soundcloud_Test extends PHPUnit_Framework_TestCase {
  4. protected $soundcloud;
  5. function setUp() {
  6. $this->soundcloud = new Services_Soundcloud_Expose(
  7. '1337',
  8. '1337',
  9. 'http://soundcloud.local/callback'
  10. );
  11. }
  12. function tearDown() {
  13. $this->soundcloud = null;
  14. }
  15. function testVersionFormat() {
  16. $this->assertRegExp(
  17. '/^[0-9]+\.[0-9]+\.[0-9]+$/',
  18. Services_Soundcloud_Version::get()
  19. );
  20. }
  21. function testGetUserAgent() {
  22. $this->assertRegExp(
  23. '/^PHP\-SoundCloud\/[0-9]+\.[0-9]+\.[0-9]+$/',
  24. $this->soundcloud->getUserAgent()
  25. );
  26. }
  27. function testApiVersion() {
  28. $this->assertEquals(1, $this->soundcloud->getApiVersion());
  29. }
  30. function testGetAudioMimeTypes() {
  31. $supportedExtensions = array(
  32. 'aac' => 'video/mp4',
  33. 'aiff' => 'audio/x-aiff',
  34. 'flac' => 'audio/flac',
  35. 'mp3' => 'audio/mpeg',
  36. 'ogg' => 'audio/ogg',
  37. 'wav' => 'audio/x-wav'
  38. );
  39. $unsupportedExtensions = array('gif', 'html', 'jpg', 'mp4', 'xml', 'xspf');
  40. foreach ($supportedExtensions as $extension => $mimeType) {
  41. $this->assertEquals(
  42. $mimeType,
  43. $this->soundcloud->getAudioMimeType($extension)
  44. );
  45. }
  46. foreach ($unsupportedExtensions as $extension => $mimeType) {
  47. $this->setExpectedException('Services_Soundcloud_Unsupported_Audio_Format_Exception');
  48. $this->soundcloud->getAudioMimeType($extension);
  49. }
  50. }
  51. function testGetAuthorizeUrl() {
  52. $this->assertEquals(
  53. 'https://soundcloud.com/connect?client_id=1337&redirect_uri=http%3A%2F%2Fsoundcloud.local%2Fcallback&response_type=code',
  54. $this->soundcloud->getAuthorizeUrl()
  55. );
  56. }
  57. function testGetAuthorizeUrlWithCustomQueryParameters() {
  58. $this->assertEquals(
  59. 'https://soundcloud.com/connect?client_id=1337&redirect_uri=http%3A%2F%2Fsoundcloud.local%2Fcallback&response_type=code&foo=bar',
  60. $this->soundcloud->getAuthorizeUrl(array('foo' => 'bar'))
  61. );
  62. $this->assertEquals(
  63. 'https://soundcloud.com/connect?client_id=1337&redirect_uri=http%3A%2F%2Fsoundcloud.local%2Fcallback&response_type=code&foo=bar&bar=foo',
  64. $this->soundcloud->getAuthorizeUrl(array('foo' => 'bar', 'bar' => 'foo'))
  65. );
  66. }
  67. function testGetAccessTokenUrl() {
  68. $this->assertEquals(
  69. 'https://api.soundcloud.com/oauth2/token',
  70. $this->soundcloud->getAccessTokenUrl()
  71. );
  72. }
  73. function testSetAccessToken() {
  74. $this->soundcloud->setAccessToken('1337');
  75. $this->assertEquals('1337', $this->soundcloud->getAccessToken());
  76. }
  77. function testSetDevelopment() {
  78. $this->soundcloud->setDevelopment(true);
  79. $this->assertTrue($this->soundcloud->getDevelopment());
  80. }
  81. function testSetRedirectUri() {
  82. $this->soundcloud->setRedirectUri('http://soundcloud.local/callback');
  83. $this->assertEquals(
  84. 'http://soundcloud.local/callback',
  85. $this->soundcloud->getRedirectUri()
  86. );
  87. }
  88. function testDefaultResponseFormat() {
  89. $this->assertEquals(
  90. 'application/json',
  91. $this->soundcloud->getResponseFormat()
  92. );
  93. }
  94. function testSetResponseFormatHtml() {
  95. $this->setExpectedException('Services_Soundcloud_Unsupported_Response_Format_Exception');
  96. $this->soundcloud->setResponseFormat('html');
  97. }
  98. function testSetResponseFormatAll() {
  99. $this->soundcloud->setResponseFormat('*');
  100. $this->assertEquals(
  101. '*/*',
  102. $this->soundcloud->getResponseFormat()
  103. );
  104. }
  105. function testSetResponseFormatJson() {
  106. $this->soundcloud->setResponseFormat('json');
  107. $this->assertEquals(
  108. 'application/json',
  109. $this->soundcloud->getResponseFormat()
  110. );
  111. }
  112. function testSetResponseFormatXml() {
  113. $this->soundcloud->setResponseFormat('xml');
  114. $this->assertEquals(
  115. 'application/xml',
  116. $this->soundcloud->getResponseFormat()
  117. );
  118. }
  119. function testResponseCodeSuccess() {
  120. $this->assertTrue($this->soundcloud->validResponseCode(200));
  121. }
  122. function testResponseCodeRedirect() {
  123. $this->assertFalse($this->soundcloud->validResponseCode(301));
  124. }
  125. function testResponseCodeClientError() {
  126. $this->assertFalse($this->soundcloud->validResponseCode(400));
  127. }
  128. function testResponseCodeServerError() {
  129. $this->assertFalse($this->soundcloud->validResponseCode(500));
  130. }
  131. function testBuildDefaultHeaders() {
  132. $this->assertEquals(
  133. array('Accept: application/json'),
  134. $this->soundcloud->buildDefaultHeaders()
  135. );
  136. }
  137. function testBuildDefaultHeadersWithAccessToken() {
  138. $this->soundcloud->setAccessToken('1337');
  139. $this->assertEquals(
  140. array('Accept: application/json', 'Authorization: OAuth 1337'),
  141. $this->soundcloud->buildDefaultHeaders()
  142. );
  143. }
  144. function testBuildUrl() {
  145. $this->assertEquals(
  146. 'https://api.soundcloud.com/v1/me',
  147. $this->soundcloud->buildUrl('me')
  148. );
  149. }
  150. function testBuildUrlWithQueryParameters() {
  151. $this->assertEquals(
  152. 'https://api.soundcloud.com/v1/tracks?q=rofl+dubstep',
  153. $this->soundcloud->buildUrl(
  154. 'tracks',
  155. array('q' => 'rofl dubstep')
  156. )
  157. );
  158. $this->assertEquals(
  159. 'https://api.soundcloud.com/v1/tracks?q=rofl+dubstep&filter=public',
  160. $this->soundcloud->buildUrl(
  161. 'tracks',
  162. array('q' => 'rofl dubstep', 'filter' => 'public')
  163. )
  164. );
  165. }
  166. function testBuildUrlWithDevelopmentDomain() {
  167. $this->soundcloud->setDevelopment(true);
  168. $this->assertEquals(
  169. 'https://api.sandbox-soundcloud.com/v1/me',
  170. $this->soundcloud->buildUrl('me')
  171. );
  172. }
  173. function testBuildUrlWithoutApiVersion() {
  174. $this->assertEquals(
  175. 'https://api.soundcloud.com/me',
  176. $this->soundcloud->buildUrl('me', null, false)
  177. );
  178. }
  179. function testBuildUrlWithAbsoluteUrl() {
  180. $this->assertEquals(
  181. 'https://api.soundcloud.com/me',
  182. $this->soundcloud->buildUrl('https://api.soundcloud.com/me')
  183. );
  184. }
  185. /**
  186. * @dataProvider dataProviderHttpHeaders
  187. */
  188. function testParseHttpHeaders($rawHeaders, $expectedHeaders) {
  189. $parsedHeaders = $this->soundcloud->parseHttpHeaders($rawHeaders);
  190. foreach ($parsedHeaders as $key => $val) {
  191. $this->assertEquals($val, $expectedHeaders[$key]);
  192. }
  193. }
  194. function testSoundcloudMissingConsumerKeyException() {
  195. $this->setExpectedException('Services_Soundcloud_Missing_Client_Id_Exception');
  196. $soundcloud = new Services_Soundcloud('', '');
  197. }
  198. function testSoundcloudInvalidHttpResponseCodeException() {
  199. $this->setExpectedException('Services_Soundcloud_Invalid_Http_Response_Code_Exception');
  200. $this->soundcloud->get('me');
  201. }
  202. /**
  203. * @dataProvider dataProviderSoundcloudInvalidHttpResponseCode
  204. */
  205. function testSoundcloudInvalidHttpResponseCode($expectedHeaders) {
  206. try {
  207. $this->soundcloud->get('me');
  208. } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
  209. $this->assertEquals(
  210. '{"error":"401 - Unauthorized"}',
  211. $e->getHttpBody()
  212. );
  213. $this->assertEquals(401, $e->getHttpCode());
  214. foreach ($expectedHeaders as $key => $val) {
  215. $this->assertEquals(
  216. $val,
  217. $this->soundcloud->getHttpHeader($key)
  218. );
  219. }
  220. }
  221. }
  222. static function dataProviderHttpHeaders() {
  223. $rawHeaders = <<<HEADERS
  224. HTTP/1.1 200 OK
  225. Date: Wed, 17 Nov 2010 15:39:52 GMT
  226. Cache-Control: public
  227. Content-Type: text/html; charset=utf-8
  228. Content-Encoding: gzip
  229. Server: foobar
  230. Content-Length: 1337
  231. HEADERS;
  232. $expectedHeaders = array(
  233. 'date' => 'Wed, 17 Nov 2010 15:39:52 GMT',
  234. 'cache_control' => 'public',
  235. 'content_type' => 'text/html; charset=utf-8',
  236. 'content_encoding' => 'gzip',
  237. 'server' => 'foobar',
  238. 'content_length' => '1337'
  239. );
  240. return array(array($rawHeaders, $expectedHeaders));
  241. }
  242. static function dataProviderSoundcloudInvalidHttpResponseCode() {
  243. $expectedHeaders = array(
  244. 'server' => 'nginx',
  245. 'content_type' => 'application/json; charset=utf-8',
  246. 'connection' => 'keep-alive',
  247. 'cache_control' => 'no-cache',
  248. 'content_length' => '30'
  249. );
  250. return array(array($expectedHeaders));
  251. }
  252. }