/tests/unit/suites/libraries/joomla/github/JGithubPackageAuthorizationsTest.php

https://github.com/andreatarr/joomla-cms · PHP · 557 lines · 321 code · 77 blank · 159 comment · 0 complexity · 92f47e31ea41f957250de74799daa539 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. * @subpackage Github
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. /**
  10. * Test class for JGithubAccount.
  11. *
  12. * @package Joomla.UnitTest
  13. * @subpackage Github
  14. *
  15. * @since 11.1
  16. */
  17. class JGithubPackageAuthorizationsTest extends PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var JRegistry Options for the GitHub object.
  21. * @since 12.3
  22. */
  23. protected $options;
  24. /**
  25. * @var JGithubHttp Mock client object.
  26. * @since 12.3
  27. */
  28. protected $client;
  29. /**
  30. * @var JGithubPackageAuthorization Object under test.
  31. * @since 12.3
  32. */
  33. protected $object;
  34. /**
  35. * @var string Sample JSON string.
  36. * @since 12.3
  37. */
  38. protected $sampleString = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
  39. /**
  40. * @var string Sample JSON error message.
  41. * @since 12.3
  42. */
  43. protected $errorString = '{"message": "Generic Error"}';
  44. /**
  45. * Sets up the fixture, for example, opens a network connection.
  46. * This method is called before a test is executed.
  47. *
  48. * @return void
  49. *
  50. * @since 12.3
  51. */
  52. protected function setUp()
  53. {
  54. parent::setUp();
  55. $this->options = new JRegistry;
  56. $this->client = $this->getMock('JGithubHttp', array('get', 'post', 'delete', 'patch', 'put'));
  57. $this->object = new JGithubPackageAuthorization($this->options, $this->client);
  58. }
  59. /**
  60. * Tests the createAuthorisation method
  61. *
  62. * @return void
  63. *
  64. * @since 12.3
  65. */
  66. public function testCreate()
  67. {
  68. $returnData = new stdClass;
  69. $returnData->code = 201;
  70. $returnData->body = $this->sampleString;
  71. $authorisation = new stdClass;
  72. $authorisation->scopes = array('public_repo');
  73. $authorisation->note = 'My test app';
  74. $authorisation->note_url = 'http://www.joomla.org';
  75. $this->client->expects($this->once())
  76. ->method('post')
  77. ->with('/authorizations', json_encode($authorisation))
  78. ->will($this->returnValue($returnData));
  79. $this->assertThat(
  80. $this->object->create(array('public_repo'), 'My test app', 'http://www.joomla.org'),
  81. $this->equalTo(json_decode($this->sampleString))
  82. );
  83. }
  84. /**
  85. * Tests the createAuthorisation method - simulated failure
  86. *
  87. * @return void
  88. *
  89. * @since 12.3
  90. */
  91. public function testCreateFailure()
  92. {
  93. $exception = false;
  94. $returnData = new stdClass;
  95. $returnData->code = 500;
  96. $returnData->body = $this->errorString;
  97. $authorisation = new stdClass;
  98. $authorisation->scopes = array('public_repo');
  99. $authorisation->note = 'My test app';
  100. $authorisation->note_url = 'http://www.joomla.org';
  101. $this->client->expects($this->once())
  102. ->method('post')
  103. ->with('/authorizations', json_encode($authorisation))
  104. ->will($this->returnValue($returnData));
  105. try
  106. {
  107. $this->object->create(array('public_repo'), 'My test app', 'http://www.joomla.org');
  108. }
  109. catch (DomainException $e)
  110. {
  111. $exception = true;
  112. $this->assertThat(
  113. $e->getMessage(),
  114. $this->equalTo(json_decode($this->errorString)->message)
  115. );
  116. }
  117. $this->assertTrue($exception);
  118. }
  119. /**
  120. * Tests the deleteAuthorisation method
  121. *
  122. * @return void
  123. *
  124. * @since 12.3
  125. */
  126. public function testDelete()
  127. {
  128. $returnData = new stdClass;
  129. $returnData->code = 204;
  130. $returnData->body = $this->sampleString;
  131. $this->client->expects($this->once())
  132. ->method('delete')
  133. ->with('/authorizations/42')
  134. ->will($this->returnValue($returnData));
  135. $this->assertThat(
  136. $this->object->delete(42),
  137. $this->equalTo(json_decode($this->sampleString))
  138. );
  139. }
  140. /**
  141. * Tests the deleteAuthorisation method - simulated failure
  142. *
  143. * @return void
  144. *
  145. * @since 12.3
  146. */
  147. public function testDeleteFailure()
  148. {
  149. $exception = false;
  150. $returnData = new stdClass;
  151. $returnData->code = 500;
  152. $returnData->body = $this->errorString;
  153. $this->client->expects($this->once())
  154. ->method('delete')
  155. ->with('/authorizations/42')
  156. ->will($this->returnValue($returnData));
  157. try
  158. {
  159. $this->object->delete(42);
  160. }
  161. catch (DomainException $e)
  162. {
  163. $exception = true;
  164. $this->assertThat(
  165. $e->getMessage(),
  166. $this->equalTo(json_decode($this->errorString)->message)
  167. );
  168. }
  169. $this->assertTrue($exception);
  170. }
  171. /**
  172. * Tests the editAuthorisation method - Add scopes
  173. *
  174. * @return void
  175. *
  176. * @since 12.3
  177. */
  178. public function testEditAddScopes()
  179. {
  180. $returnData = new stdClass;
  181. $returnData->code = 200;
  182. $returnData->body = $this->sampleString;
  183. $authorisation = new stdClass;
  184. $authorisation->add_scopes = array('public_repo', 'gist');
  185. $authorisation->note = 'My test app';
  186. $authorisation->note_url = 'http://www.joomla.org';
  187. $this->client->expects($this->once())
  188. ->method('patch')
  189. ->with('/authorizations/42', json_encode($authorisation))
  190. ->will($this->returnValue($returnData));
  191. $this->assertThat(
  192. $this->object->edit(42, array(), array('public_repo', 'gist'), array(), 'My test app', 'http://www.joomla.org'),
  193. $this->equalTo(json_decode($this->sampleString))
  194. );
  195. }
  196. /**
  197. * Tests the editAuthorisation method - Remove scopes
  198. *
  199. * @return void
  200. *
  201. * @since 12.3
  202. */
  203. public function testEditRemoveScopes()
  204. {
  205. $returnData = new stdClass;
  206. $returnData->code = 200;
  207. $returnData->body = $this->sampleString;
  208. $authorisation = new stdClass;
  209. $authorisation->remove_scopes = array('public_repo', 'gist');
  210. $authorisation->note = 'My test app';
  211. $authorisation->note_url = 'http://www.joomla.org';
  212. $this->client->expects($this->once())
  213. ->method('patch')
  214. ->with('/authorizations/42', json_encode($authorisation))
  215. ->will($this->returnValue($returnData));
  216. $this->assertThat(
  217. $this->object->edit(42, array(), array(), array('public_repo', 'gist'), 'My test app', 'http://www.joomla.org'),
  218. $this->equalTo(json_decode($this->sampleString))
  219. );
  220. }
  221. /**
  222. * Tests the editAuthorisation method - Scopes param
  223. *
  224. * @return void
  225. *
  226. * @since 12.3
  227. */
  228. public function testEditScopes()
  229. {
  230. $returnData = new stdClass;
  231. $returnData->code = 200;
  232. $returnData->body = $this->sampleString;
  233. $authorisation = new stdClass;
  234. $authorisation->scopes = array('public_repo', 'gist');
  235. $authorisation->note = 'My test app';
  236. $authorisation->note_url = 'http://www.joomla.org';
  237. $this->client->expects($this->once())
  238. ->method('patch')
  239. ->with('/authorizations/42', json_encode($authorisation))
  240. ->will($this->returnValue($returnData));
  241. $this->assertThat(
  242. $this->object->edit(42, array('public_repo', 'gist'), array(), array(), 'My test app', 'http://www.joomla.org'),
  243. $this->equalTo(json_decode($this->sampleString))
  244. );
  245. }
  246. /**
  247. * Tests the editAuthorisation method - simulated failure
  248. *
  249. * @return void
  250. *
  251. * @since 12.3
  252. */
  253. public function testEditFailure()
  254. {
  255. $exception = false;
  256. $returnData = new stdClass;
  257. $returnData->code = 500;
  258. $returnData->body = $this->errorString;
  259. $authorisation = new stdClass;
  260. $authorisation->add_scopes = array('public_repo', 'gist');
  261. $authorisation->note = 'My test app';
  262. $authorisation->note_url = 'http://www.joomla.org';
  263. $this->client->expects($this->once())
  264. ->method('patch')
  265. ->with('/authorizations/42', json_encode($authorisation))
  266. ->will($this->returnValue($returnData));
  267. try
  268. {
  269. $this->object->edit(42, array(), array('public_repo', 'gist'), array(), 'My test app', 'http://www.joomla.org');
  270. }
  271. catch (DomainException $e)
  272. {
  273. $exception = true;
  274. $this->assertThat(
  275. $e->getMessage(),
  276. $this->equalTo(json_decode($this->errorString)->message)
  277. );
  278. }
  279. $this->assertTrue($exception);
  280. }
  281. /**
  282. * Tests the editAuthorisation method - too many scope params
  283. *
  284. * @return void
  285. *
  286. * @since 12.3
  287. *
  288. * @expectedException RuntimeException
  289. */
  290. public function testEditTooManyScopes()
  291. {
  292. $this->object->edit(42, array(), array('public_repo', 'gist'), array('public_repo', 'gist'), 'My test app', 'http://www.joomla.org');
  293. }
  294. /**
  295. * Tests the getAuthorisation method
  296. *
  297. * @return void
  298. *
  299. * @since 12.3
  300. */
  301. public function testGet()
  302. {
  303. $returnData = new stdClass;
  304. $returnData->code = 200;
  305. $returnData->body = $this->sampleString;
  306. $this->client->expects($this->once())
  307. ->method('get')
  308. ->with('/authorizations/42')
  309. ->will($this->returnValue($returnData));
  310. $this->assertThat(
  311. $this->object->get(42),
  312. $this->equalTo(json_decode($this->sampleString))
  313. );
  314. }
  315. /**
  316. * Tests the getAuthorisation method - failure
  317. *
  318. * @return void
  319. *
  320. * @since 12.1
  321. *
  322. * @expectedException DomainException
  323. */
  324. public function testGetFailure()
  325. {
  326. $returnData = new stdClass;
  327. $returnData->code = 500;
  328. $returnData->body = $this->errorString;
  329. $this->client->expects($this->once())
  330. ->method('get')
  331. ->with('/authorizations/42')
  332. ->will($this->returnValue($returnData));
  333. $this->object->get(42);
  334. }
  335. /**
  336. * Tests the getAuthorisations method
  337. *
  338. * @return void
  339. *
  340. * @since 12.3
  341. */
  342. public function testGetList()
  343. {
  344. $returnData = new stdClass;
  345. $returnData->code = 200;
  346. $returnData->body = $this->sampleString;
  347. $this->client->expects($this->once())
  348. ->method('get')
  349. ->with('/authorizations')
  350. ->will($this->returnValue($returnData));
  351. $this->assertThat(
  352. $this->object->getList(),
  353. $this->equalTo(json_decode($this->sampleString))
  354. );
  355. }
  356. /**
  357. * Tests the getAuthorisations method - failure
  358. *
  359. * @return void
  360. *
  361. * @since 12.3
  362. *
  363. * @expectedException DomainException
  364. */
  365. public function testGetListFailure()
  366. {
  367. $returnData = new stdClass;
  368. $returnData->code = 500;
  369. $returnData->body = $this->errorString;
  370. $this->client->expects($this->once())
  371. ->method('get')
  372. ->with('/authorizations')
  373. ->will($this->returnValue($returnData));
  374. $this->object->getList();
  375. }
  376. /**
  377. * Tests the getRateLimit method
  378. *
  379. * @return void
  380. *
  381. * @since 12.3
  382. */
  383. public function testGetRateLimit()
  384. {
  385. $returnData = new stdClass;
  386. $returnData->code = 200;
  387. $returnData->body = $this->sampleString;
  388. $this->client->expects($this->once())
  389. ->method('get')
  390. ->with('/rate_limit')
  391. ->will($this->returnValue($returnData));
  392. $this->assertThat(
  393. $this->object->getRateLimit(),
  394. $this->equalTo(json_decode($this->sampleString))
  395. );
  396. }
  397. /**
  398. * Tests the getRateLimit method - failure
  399. *
  400. * @return void
  401. *
  402. * @since 12.3
  403. *
  404. * @expectedException DomainException
  405. */
  406. public function testGetRateLimitFailure()
  407. {
  408. $returnData = new stdClass;
  409. $returnData->code = 500;
  410. $returnData->body = $this->errorString;
  411. $this->client->expects($this->once())
  412. ->method('get')
  413. ->with('/rate_limit')
  414. ->will($this->returnValue($returnData));
  415. $this->object->getRateLimit();
  416. }
  417. public function testGetAuthorizationLink()
  418. {
  419. $returnData = new stdClass;
  420. $returnData->code = 200;
  421. $returnData->body = 'https://github.com/login/oauth/authorize?client_id=12345'
  422. . '&redirect_uri=aaa&scope=bbb&state=ccc';
  423. $this->assertThat(
  424. $this->object->getAuthorizationLink('12345', 'aaa', 'bbb', 'ccc'),
  425. $this->equalTo($returnData->body)
  426. );
  427. }
  428. public function testRequestToken()
  429. {
  430. $returnData = new JHttpResponse;
  431. $returnData->code = 200;
  432. $returnData->body = '';
  433. $this->client->expects($this->once())
  434. ->method('post')
  435. ->with('https://github.com/login/oauth/access_token')
  436. ->will($this->returnValue($returnData));
  437. $this->assertThat(
  438. $this->object->requestToken('12345', 'aaa', 'bbb', 'ccc'),
  439. $this->equalTo($returnData->body)
  440. );
  441. }
  442. public function testRequestTokenJson()
  443. {
  444. $returnData = new JHttpResponse;
  445. $returnData->code = 200;
  446. $returnData->body = '';
  447. $this->client->expects($this->once())
  448. ->method('post')
  449. ->with('https://github.com/login/oauth/access_token')
  450. ->will($this->returnValue($returnData));
  451. $this->assertThat(
  452. $this->object->requestToken('12345', 'aaa', 'bbb', 'ccc', 'json'),
  453. $this->equalTo($returnData->body)
  454. );
  455. }
  456. public function testRequestTokenXml()
  457. {
  458. $returnData = new JHttpResponse;
  459. $returnData->code = 200;
  460. $returnData->body = '';
  461. $this->client->expects($this->once())
  462. ->method('post')
  463. ->with('https://github.com/login/oauth/access_token')
  464. ->will($this->returnValue($returnData));
  465. $this->assertThat(
  466. $this->object->requestToken('12345', 'aaa', 'bbb', 'ccc', 'xml'),
  467. $this->equalTo($returnData->body)
  468. );
  469. }
  470. /**
  471. * @expectedException UnexpectedValueException
  472. */
  473. public function testRequestTokenInvalidFormat()
  474. {
  475. $returnData = new JHttpResponse;
  476. $returnData->code = 200;
  477. $returnData->body = '';
  478. $this->object->requestToken('12345', 'aaa', 'bbb', 'ccc', 'invalid');
  479. }
  480. }