AdSenseManagementTest

Copyright 2012 Google Inc.

Defined (1)

The class is defined in the following location(s).

/vendor/google/apiclient/tests/adsense/AdSenseTest.php  
  1. class AdSenseManagementTest extends BaseTest 
  2. public $adsense; 
  3. public function __construct() 
  4. parent::__construct(); 
  5. $this->adsense = new Google_Service_AdSense($this->getClient()); 
  6.  
  7. public function testAccountsList() 
  8. if (!$this->checkToken()) { 
  9. return; 
  10. $accounts = $this->adsense->accounts->listAccounts(); 
  11. $this->assertArrayHasKey('kind', $accounts); 
  12. $this->assertEquals($accounts['kind'], 'adsense#accounts'); 
  13. $account = $this->getRandomElementFromArray($accounts['items']); 
  14. $this->checkAccountElement($account); 
  15.  
  16. /** 
  17. * @depends testAccountsList 
  18. */ 
  19. public function testAccountsGet() 
  20. $accounts = $this->adsense->accounts->listAccounts(); 
  21. $account = $this->getRandomElementFromArray($accounts['items']); 
  22. $retrievedAccount = $this->adsense->accounts->get($account['id']); 
  23. $this->checkAccountElement($retrievedAccount); 
  24.  
  25. /** 
  26. * @depends testAccountsList 
  27. */ 
  28. public function testAccountsReportGenerate() 
  29. $startDate = '2011-01-01'; 
  30. $endDate = '2011-01-31'; 
  31. $optParams = $this->getReportOptParams(); 
  32. $accounts = $this->adsense->accounts->listAccounts(); 
  33. $accountId = $accounts['items'][0]['id']; 
  34. $report = $this->adsense->accounts_reports->generate( 
  35. $accountId,  
  36. $startDate,  
  37. $endDate,  
  38. $optParams 
  39. ); 
  40. $this->checkReport($report); 
  41.  
  42. /** 
  43. * @depends testAccountsList 
  44. */ 
  45. public function testAccountsAdClientsList() 
  46. $accounts = $this->adsense->accounts->listAccounts(); 
  47. $account = $this->getRandomElementFromArray($accounts['items']); 
  48. $adClients = 
  49. $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); 
  50. $this->checkAdClientsCollection($adClients); 
  51.  
  52. /** 
  53. * @depends testAccountsList 
  54. * @depends testAccountsAdClientsList 
  55. */ 
  56. public function testAccountsAdUnitsList() 
  57. $accounts = $this->adsense->accounts->listAccounts(); 
  58. foreach ($accounts['items'] as $account) { 
  59. $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); 
  60. foreach ($adClients['items'] as $adClient) { 
  61. $adUnits = $this->adsense->accounts_adunits->listAccountsAdunits( 
  62. $account['id'],  
  63. $adClient['id'] 
  64. ); 
  65. $this->checkAdUnitsCollection($adUnits); 
  66. break 2; 
  67.  
  68. /** 
  69. * @depends testAccountsList 
  70. * @depends testAccountsAdClientsList 
  71. */ 
  72. public function testAccountsAdUnitsGet() 
  73. $accounts = $this->adsense->accounts->listAccounts(); 
  74. foreach ($accounts['items'] as $account) { 
  75. $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); 
  76. foreach ($adClients['items'] as $adClient) { 
  77. $adUnits = $this->adsense->accounts_adunits->listAccountsAdunits( 
  78. $account['id'],  
  79. $adClient['id'] 
  80. ); 
  81. if (array_key_exists('items', $adUnits)) { 
  82. $adUnit = $this->getRandomElementFromArray($adUnits['items']); 
  83. $this->checkAdUnitElement($adUnit); 
  84. break 2; 
  85.  
  86. /** 
  87. * @depends testAccountsList 
  88. * @depends testAccountsAdClientsList 
  89. */ 
  90. public function testAccountsCustomChannelsList() 
  91. $accounts = $this->adsense->accounts->listAccounts(); 
  92. foreach ($accounts['items'] as $account) { 
  93. $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); 
  94. foreach ($adClients['items'] as $adClient) { 
  95. $customChannels = $this->adsense->accounts_customchannels 
  96. ->listAccountsCustomchannels($account['id'], $adClient['id']); 
  97. $this->checkCustomChannelsCollection($customChannels); 
  98. break 2; 
  99.  
  100. /** 
  101. * @depends testAccountsList 
  102. * @depends testAccountsAdClientsList 
  103. */ 
  104. public function testAccountsCustomChannelsGet() 
  105. $accounts = $this->adsense->accounts->listAccounts(); 
  106. foreach ($accounts['items'] as $account) { 
  107. $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); 
  108. foreach ($adClients['items'] as $adClient) { 
  109. $customChannels = 
  110. $this->adsense->accounts_customchannels->listAccountsCustomchannels( 
  111. $account['id'],  
  112. $adClient['id'] 
  113. ); 
  114. if (array_key_exists('items', $customChannels)) { 
  115. $customChannel = 
  116. $this->getRandomElementFromArray($customChannels['items']); 
  117. $this->checkCustomChannelElement($customChannel); 
  118. break 2; 
  119.  
  120. /** 
  121. * @depends testAccountsList 
  122. * @depends testAccountsAdClientsList 
  123. */ 
  124. public function testAccountsUrlChannelsList() 
  125. $accounts = $this->adsense->accounts->listAccounts(); 
  126. foreach ($accounts['items'] as $account) { 
  127. $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); 
  128. foreach ($adClients['items'] as $adClient) { 
  129. $urlChannels = 
  130. $this->adsense->accounts_urlchannels->listAccountsUrlchannels( 
  131. $account['id'],  
  132. $adClient['id'] 
  133. ); 
  134. $this->checkUrlChannelsCollection($urlChannels); 
  135. break 2; 
  136.  
  137. /** 
  138. * @depends testAccountsList 
  139. * @depends testAccountsAdClientsList 
  140. * @depends testAccountsAdUnitsList 
  141. */ 
  142. public function testAccountsAdUnitsCustomChannelsList() 
  143. $accounts = $this->adsense->accounts->listAccounts(); 
  144. foreach ($accounts['items'] as $account) { 
  145. $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); 
  146. foreach ($adClients['items'] as $adClient) { 
  147. $adUnits = 
  148. $this->adsense->accounts_adunits->listAccountsAdunits($account['id'], $adClient['id']); 
  149. if (array_key_exists('items', $adUnits)) { 
  150. foreach ($adUnits['items'] as $adUnit) { 
  151. $customChannels = 
  152. $this->adsense->accounts_adunits_customchannels->listAccountsAdunitsCustomchannels( 
  153. $account['id'],  
  154. $adClient['id'],  
  155. $adUnit['id'] 
  156. ); 
  157. $this->checkCustomChannelsCollection($customChannels); 
  158. // it's too expensive to go through each, if one is correct good 
  159. break 3; 
  160.  
  161. /** 
  162. * @depends testAccountsList 
  163. * @depends testAccountsAdClientsList 
  164. * @depends testAccountsCustomChannelsList 
  165. */ 
  166. public function testAccountsCustomChannelsAdUnitsList() 
  167. $accounts = $this->adsense->accounts->listAccounts(); 
  168. foreach ($accounts['items'] as $account) { 
  169. $adClients = 
  170. $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); 
  171. foreach ($adClients['items'] as $adClient) { 
  172. $customChannels = 
  173. $this->adsense->accounts_customchannels->listAccountsCustomchannels( 
  174. $account['id'],  
  175. $adClient['id'] 
  176. ); 
  177. if (array_key_exists('items', $customChannels)) { 
  178. foreach ($customChannels['items'] as $customChannel) { 
  179. $adUnits = 
  180. $this->adsense->accounts_customchannels_adunits->listAccountsCustomchannelsAdunits( 
  181. $account['id'],  
  182. $adClient['id'],  
  183. $customChannel['id'] 
  184. ); 
  185. $this->checkAdUnitsCollection($adUnits); 
  186. // it's too expensive to go through each, if one is correct good 
  187. break 3; 
  188.  
  189. public function testAdClientsList() 
  190. if (!$this->checkToken()) { 
  191. return; 
  192. $adClients = $this->adsense->adclients->listAdclients(); 
  193. $this->checkAdClientsCollection($adClients); 
  194.  
  195. /** 
  196. * @depends testAdClientsList 
  197. */ 
  198. public function testAdUnitsList() 
  199. $adClients = $this->adsense->adclients->listAdclients(); 
  200. foreach ($adClients['items'] as $adClient) { 
  201. $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); 
  202. $this->checkAdUnitsCollection($adUnits); 
  203.  
  204. /** 
  205. * @depends testAdClientsList 
  206. */ 
  207. public function testAdUnitsGet() 
  208. $adClients = $this->adsense->adclients->listAdclients(); 
  209. foreach ($adClients['items'] as $adClient) { 
  210. $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); 
  211. if (array_key_exists('items', $adUnits)) { 
  212. $adUnit = $this->getRandomElementFromArray($adUnits['items']); 
  213. $this->checkAdUnitElement($adUnit); 
  214. break 1; 
  215.  
  216. /** 
  217. * @depends testAdClientsList 
  218. * @depends testAdUnitsList 
  219. */ 
  220. public function testAdUnitsCustomChannelsList() 
  221. $adClients = $this->adsense->adclients->listAdclients(); 
  222. foreach ($adClients['items'] as $adClient) { 
  223. $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); 
  224. if (array_key_exists('items', $adUnits)) { 
  225. foreach ($adUnits['items'] as $adUnit) { 
  226. $customChannels = 
  227. $this->adsense->adunits_customchannels->listAdunitsCustomchannels( 
  228. $adClient['id'],  
  229. $adUnit['id'] 
  230. ); 
  231. $this->checkCustomChannelsCollection($customChannels); 
  232. break 2; 
  233.  
  234. /** 
  235. * @depends testAdClientsList 
  236. */ 
  237. public function testCustomChannelsList() 
  238. $adClients = $this->adsense->adclients->listAdclients(); 
  239. foreach ($adClients['items'] as $adClient) { 
  240. $customChannels = 
  241. $this->adsense->customchannels->listCustomchannels($adClient['id']); 
  242. $this->checkCustomChannelsCollection($customChannels); 
  243.  
  244. /** 
  245. * @depends testAdClientsList 
  246. */ 
  247. public function testCustomChannelsGet() 
  248. $adClients = $this->adsense->adclients->listAdclients(); 
  249. foreach ($adClients['items'] as $adClient) { 
  250. $customChannels = $this->adsense->customchannels->listCustomchannels($adClient['id']); 
  251. if (array_key_exists('items', $customChannels)) { 
  252. $customChannel = $this->getRandomElementFromArray($customChannels['items']); 
  253. $this->checkCustomChannelElement($customChannel); 
  254. break 1; 
  255.  
  256. /** 
  257. * @depends testAdClientsList 
  258. * @depends testCustomChannelsList 
  259. */ 
  260. public function testCustomChannelsAdUnitsList() 
  261. $adClients = $this->adsense->adclients->listAdclients(); 
  262. foreach ($adClients['items'] as $adClient) { 
  263. $customChannels = $this->adsense->customchannels->listCustomchannels($adClient['id']); 
  264. if (array_key_exists('items', $customChannels)) { 
  265. foreach ($customChannels['items'] as $customChannel) { 
  266. $adUnits = 
  267. $this->adsense->customchannels_adunits->listCustomchannelsAdunits( 
  268. $adClient['id'],  
  269. $customChannel['id'] 
  270. ); 
  271. $this->checkAdUnitsCollection($adUnits); 
  272. break 2; 
  273.  
  274. /** 
  275. * @depends testAdClientsList 
  276. */ 
  277. public function testUrlChannelsList() 
  278. $adClients = $this->adsense->adclients->listAdclients(); 
  279. foreach ($adClients['items'] as $adClient) { 
  280. $urlChannels = $this->adsense->urlchannels->listUrlchannels($adClient['id']); 
  281. $this->checkUrlChannelsCollection($urlChannels); 
  282.  
  283. public function testReportsGenerate() 
  284. if (!$this->checkToken()) { 
  285. return; 
  286. $startDate = '2011-01-01'; 
  287. $endDate = '2011-01-31'; 
  288. $optParams = $this->getReportOptParams(); 
  289. $report = $this->adsense->reports->generate($startDate, $endDate, $optParams); 
  290. $this->checkReport($report); 
  291.  
  292. private function checkAccountElement($account) 
  293. $this->assertArrayHasKey('kind', $account); 
  294. $this->assertArrayHasKey('id', $account); 
  295. $this->assertArrayHasKey('name', $account); 
  296.  
  297. private function checkAdClientsCollection($adClients) 
  298. $this->assertArrayHasKey('kind', $adClients); 
  299. $this->assertEquals($adClients['kind'], 'adsense#adClients'); 
  300. foreach ($adClients['items'] as $adClient) { 
  301. $this->assertArrayHasKey('id', $adClient); 
  302. $this->assertArrayHasKey('kind', $adClient); 
  303. $this->assertArrayHasKey('productCode', $adClient); 
  304. $this->assertArrayHasKey('supportsReporting', $adClient); 
  305.  
  306. private function checkAdUnitsCollection($adUnits) 
  307. $this->assertArrayHasKey('kind', $adUnits); 
  308. $this->assertEquals($adUnits['kind'], 'adsense#adUnits'); 
  309. if (array_key_exists('items', $adUnits)) { 
  310. foreach ($adUnits['items'] as $adUnit) { 
  311. $this->checkAdUnitElement($adUnit); 
  312.  
  313. private function checkAdUnitElement($adUnit) 
  314. $this->assertArrayHasKey('code', $adUnit); 
  315. $this->assertArrayHasKey('id', $adUnit); 
  316. $this->assertArrayHasKey('kind', $adUnit); 
  317. $this->assertArrayHasKey('name', $adUnit); 
  318. $this->assertArrayHasKey('status', $adUnit); 
  319.  
  320. private function checkCustomChannelsCollection($customChannels) 
  321. $this->assertArrayHasKey('kind', $customChannels); 
  322. $this->assertEquals($customChannels['kind'], 'adsense#customChannels'); 
  323. if (array_key_exists('items', $customChannels)) { 
  324. foreach ($customChannels['items'] as $customChannel) { 
  325. $this->checkCustomChannelElement($customChannel); 
  326.  
  327. private function checkCustomChannelElement($customChannel) 
  328. $this->assertArrayHasKey('kind', $customChannel); 
  329. $this->assertArrayHasKey('id', $customChannel); 
  330. $this->assertArrayHasKey('code', $customChannel); 
  331. $this->assertArrayHasKey('name', $customChannel); 
  332.  
  333. private function checkUrlChannelsCollection($urlChannels) 
  334. $this->assertArrayHasKey('kind', $urlChannels); 
  335. $this->assertEquals($urlChannels['kind'], 'adsense#urlChannels'); 
  336. if (array_key_exists('items', $urlChannels)) { 
  337. foreach ($urlChannels['items'] as $urlChannel) { 
  338. $this->assertArrayHasKey('kind', $urlChannel); 
  339. $this->assertArrayHasKey('id', $urlChannel); 
  340. $this->assertArrayHasKey('urlPattern', $urlChannel); 
  341.  
  342. private function getReportOptParams() 
  343. return array( 
  344. 'metric' => array('PAGE_VIEWS', 'AD_REQUESTS'),  
  345. 'dimension' => array ('DATE', 'AD_CLIENT_ID'),  
  346. 'sort' => array('DATE'),  
  347. 'filter' => array('COUNTRY_NAME==United States'),  
  348. ); 
  349.  
  350. private function checkReport($report) 
  351. $this->assertArrayHasKey('kind', $report); 
  352. $this->assertEquals($report['kind'], 'adsense#report'); 
  353. $this->assertArrayHasKey('totalMatchedRows', $report); 
  354. $this->assertGreaterThan(0, count($report->headers)); 
  355. foreach ($report['headers'] as $header) { 
  356. $this->assertArrayHasKey('name', $header); 
  357. $this->assertArrayHasKey('type', $header); 
  358. if (array_key_exists('items', $report)) { 
  359. foreach ($report['items'] as $row) { 
  360. $this->assertCount(4, $row); 
  361. $this->assertArrayHasKey('totals', $report); 
  362. $this->assertArrayHasKey('averages', $report); 
  363.  
  364. private function getRandomElementFromArray($array) 
  365. $elementKey = array_rand($array); 
  366. return $array[$elementKey];