AdSenseManagementTest

Copyright 2012 Google Inc.

Defined (1)

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

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