SpreadsheetsOnlineTest.php 12 KB

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