2
0

SpreadsheetsOnlineTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  22. require_once 'Zend/Gdata/Spreadsheets.php';
  23. require_once 'Zend/Http/Client.php';
  24. require_once 'Zend/Gdata/ClientLogin.php';
  25. /**
  26. * @package Zend_Gdata
  27. * @subpackage UnitTests
  28. */
  29. class Zend_Gdata_SpreadsheetsOnlineTest extends PHPUnit_Framework_TestCase
  30. {
  31. public function setUp()
  32. {
  33. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  34. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  35. $this->sprKey = constant('TESTS_ZEND_GDATA_SPREADSHEETS_SPREADSHEETKEY');
  36. $this->wksId = constant('TESTS_ZEND_GDATA_SPREADSHEETS_WORKSHEETID');
  37. $service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
  38. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  39. $this->gdata = new Zend_Gdata_Spreadsheets($client);
  40. }
  41. public function testGetSpreadsheetsAndWorksheetsAndData()
  42. {
  43. $spreadsheetCount = 0;
  44. $spreadsheets = $this->gdata->getSpreadsheets();
  45. $testedContents = false;
  46. foreach($spreadsheets as $spreadsheet) {
  47. $spreadsheetCount++;
  48. $worksheetCount = 0;
  49. $this->assertTrue($spreadsheet instanceof Zend_Gdata_Spreadsheets_SpreadsheetEntry, 'not instance of SpreadsheetEntry');
  50. foreach($spreadsheet->getWorksheets() as $worksheet) {
  51. $this->assertTrue($worksheet instanceof Zend_Gdata_Spreadsheets_WorksheetEntry, 'not instance of WorksheetEntry');
  52. $worksheetCount++;
  53. if ($spreadsheet->getTitle()->getText() == 'PHP Unit Test Sheet') {
  54. $testedContents = true;
  55. $contentAsCells = $worksheet->getContentsAsCells();
  56. $this->assertEquals('a1', $contentAsCells['A1']['value']);
  57. $this->assertEquals('new', $contentAsCells['A2']['value']);
  58. $this->assertEquals('row', $contentAsCells['B2']['value']);
  59. $contentAsRows = $worksheet->getContentsAsRows();
  60. $this->assertEquals('new', $contentAsRows[0]['a1']);
  61. $this->assertEquals('data', $contentAsRows[0]['c1']);
  62. $this->assertEquals('here', $contentAsRows[0]['d1']);
  63. }
  64. }
  65. $this->assertTrue($worksheetCount >= 1, 'didn\'t get >= 1 worksheet');
  66. }
  67. $this->assertTrue($spreadsheetCount > 1, 'didn\'t get >1 spreadsheet');
  68. $this->assertTrue($testedContents, 'didn\'t test the contents of the worksheet');
  69. }
  70. public function testGetSpreadsheetFeed()
  71. {
  72. $feed = $this->gdata->getSpreadsheetFeed();
  73. $this->assertTrue($feed instanceof Zend_Gdata_Spreadsheets_SpreadsheetFeed);
  74. foreach ($feed->entries as $entry) {
  75. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_SpreadsheetEntry);
  76. $this->assertTrue($entry->getHttpClient() == $feed->getHttpClient());
  77. }
  78. $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
  79. $feed = $this->gdata->getSpreadsheetFeed($query);
  80. $this->assertTrue($feed instanceof Zend_Gdata_Spreadsheets_SpreadsheetFeed);
  81. foreach ($feed->entries as $entry) {
  82. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_SpreadsheetEntry);
  83. $this->assertTrue($entry->getHttpClient() == $feed->getHttpClient());
  84. }
  85. $uri = $query->getQueryUrl();
  86. $feed = $this->gdata->getSpreadsheetFeed($uri);
  87. $this->assertTrue($feed instanceof Zend_Gdata_Spreadsheets_SpreadsheetFeed);
  88. foreach ($feed->entries as $entry) {
  89. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_SpreadsheetEntry);
  90. $this->assertTrue($entry->getHttpClient() == $feed->getHttpClient());
  91. }
  92. }
  93. public function testGetWorksheetFeed()
  94. {
  95. $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
  96. $query->setSpreadsheetKey($this->sprKey);
  97. $feed = $this->gdata->getWorksheetFeed($query);
  98. $this->assertTrue($feed instanceof Zend_Gdata_Spreadsheets_WorksheetFeed);
  99. foreach ($feed->entries as $entry) {
  100. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_WorksheetEntry);
  101. $this->assertTrue($entry->getHttpClient() == $feed->getHttpClient());
  102. }
  103. $uri = $query->getQueryUrl();
  104. $feed = $this->gdata->getWorksheetFeed($uri);
  105. $this->assertTrue($feed instanceof Zend_Gdata_Spreadsheets_WorksheetFeed);
  106. foreach ($feed->entries as $entry) {
  107. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_WorksheetEntry);
  108. $this->assertTrue($entry->getHttpClient() == $feed->getHttpClient());
  109. }
  110. }
  111. public function testGetCellFeed()
  112. {
  113. $query = new Zend_Gdata_Spreadsheets_CellQuery();
  114. $query->setSpreadsheetKey($this->sprKey);
  115. $query->setWorksheetId($this->wksId);
  116. $feed = $this->gdata->getCellFeed($query);
  117. $this->assertTrue($feed instanceof Zend_Gdata_Spreadsheets_CellFeed);
  118. foreach ($feed->entries as $entry) {
  119. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_CellEntry);
  120. $this->assertTrue($entry->getHttpClient() == $feed->getHttpClient());
  121. }
  122. $feed = $this->gdata->getCellFeed($query->getQueryUrl());
  123. $this->assertTrue($feed instanceof Zend_Gdata_Spreadsheets_CellFeed);
  124. foreach ($feed->entries as $entry) {
  125. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_CellEntry);
  126. $this->assertTrue($entry->getHttpClient() == $feed->getHttpClient());
  127. }
  128. }
  129. public function testGetListFeed()
  130. {
  131. $query = new Zend_Gdata_Spreadsheets_ListQuery();
  132. $query->setSpreadsheetKey($this->sprKey);
  133. $query->setWorksheetId($this->wksId);
  134. $feed = $this->gdata->getListFeed($query);
  135. $this->assertTrue($feed instanceof Zend_Gdata_Spreadsheets_ListFeed);
  136. foreach ($feed->entries as $entry) {
  137. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_ListEntry);
  138. $this->assertTrue($entry->getHttpClient() == $feed->getHttpClient());
  139. }
  140. $feed = $this->gdata->getListFeed($query->getQueryUrl());
  141. $this->assertTrue($feed instanceof Zend_Gdata_Spreadsheets_ListFeed);
  142. foreach ($feed->entries as $entry) {
  143. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_ListEntry);
  144. $this->assertTrue($entry->getHttpClient() == $feed->getHttpClient());
  145. }
  146. }
  147. public function testGetSpreadsheetEntry()
  148. {
  149. $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
  150. $query->setSpreadsheetKey($this->sprKey);
  151. $entry = $this->gdata->getSpreadsheetEntry($query);
  152. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_SpreadsheetEntry);
  153. $entry = $this->gdata->getSpreadsheetEntry($query->getQueryUrl());
  154. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_SpreadsheetEntry);
  155. }
  156. public function testGetWorksheetEntry()
  157. {
  158. $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
  159. $query->setSpreadsheetKey($this->sprKey);
  160. $query->setWorksheetId($this->wksId);
  161. $entry = $this->gdata->getWorksheetEntry($query);
  162. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_WorksheetEntry);
  163. $entry = $this->gdata->getWorksheetEntry($query->getQueryUrl());
  164. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_WorksheetEntry);
  165. }
  166. public function testGetCellEntry()
  167. {
  168. $query = new Zend_Gdata_Spreadsheets_CellQuery();
  169. $query->setSpreadsheetKey($this->sprKey);
  170. $query->setCellId('R1C1');
  171. $entry = $this->gdata->getCellEntry($query);
  172. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_CellEntry);
  173. $entry = $this->gdata->getCellEntry($query->getQueryUrl());
  174. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_CellEntry);
  175. }
  176. public function testGetListEntry()
  177. {
  178. $query = new Zend_Gdata_Spreadsheets_ListQuery();
  179. $query->setSpreadsheetKey($this->sprKey);
  180. $query->setStartIndex('1');
  181. $query->setMaxResults('1');
  182. $entry = $this->gdata->getListEntry($query);
  183. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_ListEntry);
  184. $entry = $this->gdata->getListEntry($query->getQueryUrl());
  185. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_ListEntry);
  186. }
  187. public function testUpdateCell()
  188. {
  189. $this->gdata->updateCell(5, 1, 'updated data', $this->sprKey, $this->wksId);
  190. $query = new Zend_Gdata_Spreadsheets_CellQuery();
  191. $query->setSpreadsheetKey($this->sprKey);
  192. $query->setCellId('R5C1');
  193. $entry = $this->gdata->getCellEntry($query);
  194. $this->assertTrue($entry instanceof Zend_Gdata_Spreadsheets_CellEntry);
  195. $this->assertTrue($entry->cell->getText() == 'updated data');
  196. $this->gdata->updateCell(5, 1, '', $this->sprKey, $this->wksId);
  197. }
  198. public function testInsertUpdateDeleteRow()
  199. {
  200. $rowData = array();
  201. $rowData['a1'] = 'new';
  202. $rowData['b1'] = 'row';
  203. $rowData['c1'] = 'data';
  204. $rowData['d1'] = 'here';
  205. $entry = $this->gdata->insertRow($rowData, $this->sprKey);
  206. $rowData['a1'] = 'newer';
  207. $entry = $this->gdata->updateRow($entry, $rowData);
  208. $this->gdata->deleteRow($entry);
  209. }
  210. public function testInsertUpdateDeleteRow2()
  211. {
  212. $rowData = array();
  213. $rowData['a1'] = 'new';
  214. $rowData['b1'] = 'row';
  215. $rowData['c1'] = 'data';
  216. $rowData['d1'] = 'here';
  217. $entry = $this->gdata->insertRow($rowData, $this->sprKey);
  218. $rowData['a1'] = 'newer';
  219. $entry = $this->gdata->updateRow($entry, $rowData);
  220. $ssTest = new Zend_Gdata_Spreadsheets($entry->getHttpClient());
  221. $ssTest->delete($entry->getEditLink()->href);
  222. }
  223. public function testInsertUpdateDeleteRow3()
  224. {
  225. $rowData = array();
  226. $rowData['a1'] = 'new';
  227. $rowData['b1'] = 'row';
  228. $rowData['c1'] = 'data';
  229. $rowData['d1'] = 'here';
  230. $entry = $this->gdata->insertRow($rowData, $this->sprKey);
  231. $rowData['a1'] = 'newer';
  232. $entry = $this->gdata->updateRow($entry, $rowData);
  233. $ssTest = new Zend_Gdata_Spreadsheets($entry->getHttpClient());
  234. $ssTest->delete($entry);
  235. }
  236. public function testCustomElementsCollected() {
  237. $rowData = array();
  238. $rowData['a1'] = 'new';
  239. $rowData['b1'] = 'row';
  240. $rowData['c1'] = 'data';
  241. $rowData['d1'] = 'here';
  242. $entry = $this->gdata->insertRow($rowData, $this->sprKey);
  243. $this->assertEquals(4, count($entry->custom));
  244. $this->assertEquals(4, count($entry->customByName));
  245. $this->assertEquals('new', $entry->custom[0]->getText());
  246. $this->assertEquals('row', $entry->custom[1]->getText());
  247. $this->assertEquals('data', $entry->custom[2]->getText());
  248. $this->assertEquals('here', $entry->custom[3]->getText());
  249. $this->assertEquals('new', $entry->customByName['a1']->getText());
  250. $this->assertEquals('row', $entry->customByName['b1']->getText());
  251. $this->assertEquals('data', $entry->customByName['c1']->getText());
  252. $this->assertEquals('here', $entry->customByName['d1']->getText());
  253. $ssTest = new Zend_Gdata_Spreadsheets($entry->getHttpClient());
  254. $ssTest->delete($entry);
  255. }
  256. }