SpreadsheetsOnlineTest.php 12 KB

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