PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/suites/unit/joomla/google/JGoogleDataAdsenseTest.php

http://github.com/joomla/joomla-platform
PHP | 434 lines | 204 code | 49 blank | 181 comment | 0 complexity | ac62a84b1adb6ba2fe4b32d536c91700 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. *
  5. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. /**
  9. * Test class for JGoogleDataAdsense.
  10. *
  11. * @package Joomla.UnitTest
  12. * @subpackage Google
  13. * @since 12.3
  14. */
  15. class JGoogleDataAdsenseTest extends TestCase
  16. {
  17. /**
  18. * @var JRegistry Options for the JOAuth2Client object.
  19. */
  20. protected $options;
  21. /**
  22. * @var JHttp Mock client object.
  23. */
  24. protected $http;
  25. /**
  26. * @var JInput The input object to use in retrieving GET/POST data.
  27. */
  28. protected $input;
  29. /**
  30. * @var JOAuth2Client The OAuth client for sending requests to Google.
  31. */
  32. protected $oauth;
  33. /**
  34. * @var JGoogleAuthOauth2 The Google OAuth client for sending requests.
  35. */
  36. protected $auth;
  37. /**
  38. * @var JGoogleDataAdsense Object under test.
  39. */
  40. protected $object;
  41. /**
  42. * Sets up the fixture, for example, opens a network connection.
  43. * This method is called before a test is executed.
  44. *
  45. * @access protected
  46. * @return void
  47. */
  48. protected function setUp()
  49. {
  50. parent::setUp();
  51. $_SERVER['HTTP_HOST'] = 'mydomain.com';
  52. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0';
  53. $_SERVER['REQUEST_URI'] = '/index.php';
  54. $_SERVER['SCRIPT_NAME'] = '/index.php';
  55. $this->options = new JRegistry;
  56. $this->http = $this->getMock('JHttp', array('head', 'get', 'delete', 'trace', 'post', 'put', 'patch'), array($this->options));
  57. $this->input = new JInput;
  58. $this->oauth = new JOAuth2Client($this->options, $this->http, $this->input);
  59. $this->auth = new JGoogleAuthOauth2($this->options, $this->oauth);
  60. $this->object = new JGoogleDataAdsense($this->options, $this->auth);
  61. $this->object->setOption('clientid', '01234567891011.apps.googleusercontent.com');
  62. $this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
  63. $this->object->setOption('redirecturi', 'http://localhost/oauth');
  64. $token['access_token'] = 'accessvalue';
  65. $token['refresh_token'] = 'refreshvalue';
  66. $token['created'] = time() - 1800;
  67. $token['expires_in'] = 3600;
  68. $this->oauth->setToken($token);
  69. }
  70. /**
  71. * Tests the auth method
  72. *
  73. * @group JGoogle
  74. * @return void
  75. */
  76. public function testAuth()
  77. {
  78. $this->assertEquals($this->auth->authenticate(), $this->object->authenticate());
  79. }
  80. /**
  81. * Tests the isauth method
  82. *
  83. * @group JGoogle
  84. * @return void
  85. */
  86. public function testIsAuth()
  87. {
  88. $this->assertEquals($this->auth->isAuthenticated(), $this->object->isAuthenticated());
  89. }
  90. /**
  91. * Tests the getAccount method
  92. *
  93. * @group JGoogle
  94. * @return void
  95. */
  96. public function testGetAccount()
  97. {
  98. $this->http->expects($this->once())->method('get')->will($this->returnCallback('jsonAdsenseCallback'));
  99. $result = $this->object->getAccount('accountID');
  100. $this->assertEquals($result, array('items' => array('1' => 1, '2' => 2), 'nextPageToken' => '1234'));
  101. }
  102. /**
  103. * Tests the listAccounts method
  104. *
  105. * @group JGoogle
  106. * @return void
  107. */
  108. public function testListAccounts()
  109. {
  110. $this->http->expects($this->exactly(2))->method('get')->will($this->returnCallback('jsonAdsenseCallback'));
  111. $result = $this->object->listAccounts(array('option' => 'value', 'option2' => 'value2'), 2);
  112. $this->assertEquals($result, array(1, 2, 1, 2));
  113. }
  114. /**
  115. * Tests the listClients method
  116. *
  117. * @group JGoogle
  118. * @return void
  119. */
  120. public function testListClients()
  121. {
  122. $this->http->expects($this->once())->method('get')->will($this->returnCallback('jsonAdsenseCallback'));
  123. $result = $this->object->listClients('accountID', array('option' => 'value'));
  124. $this->assertEquals($result, array('1' => 1, '2' => 2));
  125. }
  126. /**
  127. * Tests the getUnit method
  128. *
  129. * @group JGoogle
  130. * @return void
  131. */
  132. public function testGetUnit()
  133. {
  134. $this->http->expects($this->once())->method('get')->will($this->returnCallback('jsonAdsenseCallback'));
  135. $result = $this->object->getUnit('accountID', 'clientID', 'unitID');
  136. $this->assertEquals($result, array('items' => array('1' => 1, '2' => 2), 'nextPageToken' => '1234'));
  137. }
  138. /**
  139. * Tests the listUnitChannels method
  140. *
  141. * @group JGoogle
  142. * @return void
  143. */
  144. public function testListUnitChannels()
  145. {
  146. $this->http->expects($this->once())->method('get')->will($this->returnCallback('jsonAdsenseCallback'));
  147. $result = $this->object->listUnitChannels('accountID', 'clientID', 'unitID', array('option' => 'value'));
  148. $this->assertEquals($result, array('1' => 1, '2' => 2));
  149. }
  150. /**
  151. * Tests the getChannel method
  152. *
  153. * @group JGoogle
  154. * @return void
  155. */
  156. public function testGetChannel()
  157. {
  158. $this->http->expects($this->once())->method('get')->will($this->returnCallback('jsonAdsenseCallback'));
  159. $result = $this->object->getChannel('accountID', 'clientID', 'channelID');
  160. $this->assertEquals($result, array('items' => array('1' => 1, '2' => 2), 'nextPageToken' => '1234'));
  161. }
  162. /**
  163. * Tests the listChannels method
  164. *
  165. * @group JGoogle
  166. * @return void
  167. */
  168. public function testListChannels()
  169. {
  170. $this->http->expects($this->once())->method('get')->will($this->returnCallback('jsonAdsenseCallback'));
  171. $result = $this->object->listChannels('accountID', 'clientID', array('option' => 'value'));
  172. $this->assertEquals($result, array('1' => 1, '2' => 2));
  173. }
  174. /**
  175. * Tests the listChannelUnits method
  176. *
  177. * @group JGoogle
  178. * @return void
  179. */
  180. public function testListChannelUnits()
  181. {
  182. $this->http->expects($this->once())->method('get')->will($this->returnCallback('jsonAdsenseCallback'));
  183. $result = $this->object->listChannelUnits('accountID', 'clientID', 'channelID', array('option' => 'value'));
  184. $this->assertEquals($result, array('1' => 1, '2' => 2));
  185. }
  186. /**
  187. * Tests the listUrlChannels method
  188. *
  189. * @group JGoogle
  190. * @return void
  191. */
  192. public function testListUrlChannels()
  193. {
  194. $this->http->expects($this->once())->method('get')->will($this->returnCallback('jsonAdsenseCallback'));
  195. $result = $this->object->listUrlChannels('accountID', 'clientID', array('option' => 'value'));
  196. $this->assertEquals($result, array('1' => 1, '2' => 2));
  197. }
  198. /**
  199. * Tests the generateReport method
  200. *
  201. * @group JGoogle
  202. * @return void
  203. */
  204. public function testGenerateReport()
  205. {
  206. $this->http->expects($this->exactly(4))->method('get')->will($this->returnCallback('jsonAdsenseReportCallback'));
  207. $timezone = new DateTimeZone('Europe/London');
  208. $start = new DateTime('now');
  209. $end = new DateTime;
  210. $end->setTimestamp(time() + 3600)->setTimeZone($timezone);
  211. $result = $this->object->generateReport('accountID', time(), time() + 100000, array('option' => 'value'));
  212. $this->assertEquals($result, array('rows' => array(1, 2), 'totalMatchedRows' => 1));
  213. $result = $this->object->generateReport('accountID', time(), false, array('option' => 'value'));
  214. $this->assertEquals($result, array('rows' => array(1, 2), 'totalMatchedRows' => 1));
  215. $result = $this->object->generateReport('accountID', '1-1-2000', '1-2-2012', array('option' => 'value'));
  216. $this->assertEquals($result, array('rows' => array(1, 2), 'totalMatchedRows' => 1));
  217. $result = $this->object->generateReport('accountID', $start, $end, array('option' => 'value'));
  218. $this->assertEquals($result, array('rows' => array(1, 2), 'totalMatchedRows' => 1));
  219. }
  220. /**
  221. * Tests the generateReport method with a bad start date
  222. *
  223. * @group JGoogle
  224. * @expectedException InvalidArgumentException
  225. * @return void
  226. */
  227. public function testGenerateReportStartException()
  228. {
  229. $this->object->generateReport('accountID', array(true));
  230. }
  231. /**
  232. * Tests the generateReport method with a bad end date
  233. *
  234. * @group JGoogle
  235. * @expectedException InvalidArgumentException
  236. * @return void
  237. */
  238. public function testGenerateReportEndException()
  239. {
  240. $this->object->generateReport('accountID', time(), array(true));
  241. }
  242. /**
  243. * Tests the setOption method
  244. *
  245. * @group JGoogle
  246. * @return void
  247. */
  248. public function testSetOption()
  249. {
  250. $this->object->setOption('key', 'value');
  251. $this->assertThat(
  252. $this->options->get('key'),
  253. $this->equalTo('value')
  254. );
  255. }
  256. /**
  257. * Tests the getOption method
  258. *
  259. * @group JGoogle
  260. * @return void
  261. */
  262. public function testGetOption()
  263. {
  264. $this->options->set('key', 'value');
  265. $this->assertThat(
  266. $this->object->getOption('key'),
  267. $this->equalTo('value')
  268. );
  269. }
  270. /**
  271. * Tests that all functions properly return false
  272. *
  273. * @group JGoogle
  274. * @return void
  275. */
  276. public function testFalse()
  277. {
  278. $this->oauth->setToken(false);
  279. $functions['getAccount'] = array('accountID');
  280. $functions['listAccounts'] = array(array('option' => 'value'));
  281. $functions['listClients'] = array(array('option' => 'value'));
  282. $functions['getUnit'] = array('accountID', 'clientID', 'unitID');
  283. $functions['listUnitChannels'] = array('accountID', 'clientID', 'unitID', array('option' => 'value'));
  284. $functions['getChannel'] = array('accountID', 'clientID', 'channelID');
  285. $functions['listChannels'] = array('accountID', 'clientID', array('option' => 'value'));
  286. $functions['listChannelUnits'] = array('accountID', 'clientID', 'channelID', array('option' => 'value'));
  287. $functions['listUrlChannels'] = array('accountID', array('option' => 'value'));
  288. $functions['generateReport'] = array('accountID', time(), time() + 100000, array('option' => 'value'));
  289. foreach ($functions as $function => $params)
  290. {
  291. $this->assertFalse(call_user_func_array(array($this->object, $function), $params));
  292. }
  293. }
  294. /**
  295. * Tests that all functions properly return Exceptions
  296. *
  297. * @group JGoogle
  298. * @return void
  299. */
  300. public function testExceptions()
  301. {
  302. $this->http->expects($this->atLeastOnce())->method('get')->will($this->returnCallback('adsenseExceptionCallback'));
  303. $functions['getAccount'] = array('accountID');
  304. $functions['listAccounts'] = array(array('option' => 'value'));
  305. $functions['listClients'] = array('accountID', array('option' => 'value'));
  306. $functions['getUnit'] = array('accountID', 'clientID', 'unitID');
  307. $functions['listUnitChannels'] = array('accountID', 'clientID', 'unitID', array('option' => 'value'));
  308. $functions['getChannel'] = array('accountID', 'clientID', 'channelID');
  309. $functions['listChannels'] = array('accountID', 'clientID', array('option' => 'value'));
  310. $functions['listChannelUnits'] = array('accountID', 'clientID', 'channelID', array('option' => 'value'));
  311. $functions['listUrlChannels'] = array('accountID', 'clientID', array('option' => 'value'));
  312. $functions['generateReport'] = array('accountID', time(), time() + 100000, array('option' => 'value'));
  313. foreach ($functions as $function => $params)
  314. {
  315. $exception = false;
  316. try
  317. {
  318. call_user_func_array(array($this->object, $function), $params);
  319. }
  320. catch (UnexpectedValueException $e)
  321. {
  322. $exception = true;
  323. $this->assertEquals($e->getMessage(), 'Unexpected data received from Google: `BADDATA`.');
  324. }
  325. $this->assertTrue($exception);
  326. }
  327. }
  328. }
  329. /**
  330. * Dummy method
  331. *
  332. * @param string $url Path to the resource.
  333. * @param array $headers An array of name-value pairs to include in the header of the request.
  334. * @param integer $timeout Read timeout in seconds.
  335. *
  336. * @return JHttpResponse
  337. *
  338. * @since 12.3
  339. */
  340. function jsonAdsenseCallback($url, array $headers = null, $timeout = null)
  341. {
  342. $response = new stdClass;
  343. $response->code = 200;
  344. $response->headers = array('Content-Type' => 'application/json');
  345. $response->body = '{"items":{"1":1,"2":2},"nextPageToken":"1234"}';
  346. return $response;
  347. }
  348. /**
  349. * Dummy method
  350. *
  351. * @param string $url Path to the resource.
  352. * @param array $headers An array of name-value pairs to include in the header of the request.
  353. * @param integer $timeout Read timeout in seconds.
  354. *
  355. * @return JHttpResponse
  356. *
  357. * @since 12.3
  358. */
  359. function jsonAdsenseReportCallback($url, array $headers = null, $timeout = null)
  360. {
  361. $response = new stdClass;
  362. $response->code = 200;
  363. $response->headers = array('Content-Type' => 'application/json');
  364. $response->body = '{"rows":{"0":1,"1":2},"totalMatchedRows":1}';
  365. return $response;
  366. }
  367. /**
  368. * Dummy method
  369. *
  370. * @param string $url Path to the resource.
  371. * @param array $headers An array of name-value pairs to include in the header of the request.
  372. * @param integer $timeout Read timeout in seconds.
  373. *
  374. * @return JHttpResponse
  375. *
  376. * @since 12.3
  377. */
  378. function adsenseExceptionCallback($url, array $headers = null, $timeout = null)
  379. {
  380. $response = new stdClass;
  381. $response->code = 200;
  382. $response->headers = array('Content-Type' => 'application/json');
  383. $response->body = 'BADDATA';
  384. return $response;
  385. }