TestCase.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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_Cloud
  17. * @subpackage DocumentService
  18. * @copyright Copyright (c) 2005-2010 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(__FILE__) . '/../../../TestHelper.php';
  22. /**
  23. * @see Zend_Cloud_DocumentService_Adapter
  24. */
  25. require_once 'Zend/Cloud/DocumentService/Adapter.php';
  26. /**
  27. * @see Zend_Cloud_DocumenteService_Document
  28. */
  29. require_once 'Zend/Cloud/DocumentService/Document.php';
  30. /**
  31. * This class forces the adapter tests to implement tests for all methods on
  32. * Zend_Cloud_DocumentService.
  33. *
  34. * @category Zend
  35. * @package Zend_Cloud
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. abstract class Zend_Cloud_DocumentService_TestCase extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Reference to Document adapter to test
  44. *
  45. * @var Zend_Cloud_DocumentService
  46. */
  47. protected $_commonDocument;
  48. protected $_dummyCollectionNamePrefix = 'TestCollection';
  49. protected $_dummyDataPrefix = 'TestData';
  50. protected $_clientType = 'stdClass';
  51. const ID_FIELD = "__id";
  52. /**
  53. * Config object
  54. *
  55. * @var Zend_Config
  56. */
  57. protected $_config;
  58. /**
  59. * Period to wait for propagation in seconds
  60. * Should be set by adapter
  61. *
  62. * @var int
  63. */
  64. protected $_waitPeriod = 1;
  65. public function testDocumentService()
  66. {
  67. $this->assertTrue($this->_commonDocument instanceof Zend_Cloud_DocumentService_Adapter);
  68. }
  69. public function testGetClient()
  70. {
  71. $this->assertTrue(is_a($this->_commonDocument->getClient(), $this->_clientType));
  72. }
  73. public function testCreateCollection()
  74. {
  75. $name = $this->_collectionName("testCreate");
  76. $this->_commonDocument->deleteCollection($name);
  77. $this->_wait();
  78. $this->_commonDocument->createCollection($name);
  79. $this->_wait();
  80. $collections = $this->_commonDocument->listCollections();
  81. $this->assertContains($name, $collections, "New collection not in the list");
  82. $this->_wait();
  83. $this->_commonDocument->deleteCollection($name);
  84. }
  85. public function testDeleteCollection()
  86. {
  87. $name = $this->_collectionName("testDC");
  88. $this->_commonDocument->createCollection($name);
  89. $this->_wait();
  90. $collections = $this->_commonDocument->listCollections();
  91. $this->assertContains($name, $collections, "New collection not in the list");
  92. $this->_wait();
  93. $this->_commonDocument->deleteCollection($name);
  94. $this->_wait();
  95. $this->_wait();
  96. $collections = $this->_commonDocument->listCollections();
  97. $this->assertNotContains($name, $collections, "New collection not in the list");
  98. }
  99. public function testListCollections()
  100. {
  101. $this->_commonDocument->createCollection($this->_collectionName("test3"));
  102. $this->_commonDocument->createCollection($this->_collectionName("test4"));
  103. $this->_wait();
  104. $collections = $this->_commonDocument->listCollections();
  105. $this->assertContains($this->_collectionName("test3"), $collections, "New collection test3 not in the list");
  106. $this->assertContains($this->_collectionName("test4"), $collections, "New collection test4 not in the list");
  107. $this->_wait();
  108. $this->_commonDocument->deleteCollection($this->_collectionName("test3"));
  109. $this->_commonDocument->deleteCollection($this->_collectionName("test4"));
  110. }
  111. public function testInsertDocument()
  112. {
  113. $data = $this->_getDocumentData();
  114. $name = $this->_collectionName("testID");
  115. $this->_commonDocument->createCollection($name);
  116. $doc = $this->_makeDocument($data[0]);
  117. $this->_commonDocument->insertDocument($name, $doc);
  118. $this->_wait();
  119. $fetchdoc = $this->_commonDocument->fetchDocument($name, $doc->getId());
  120. $this->assertTrue($fetchdoc instanceof Zend_Cloud_DocumentService_Document, "New document not found");
  121. $this->assertEquals($doc->name, $fetchdoc->name, "Name field wrong");
  122. $this->assertEquals($doc->keyword, $fetchdoc->keyword, "Keyword field wrong");
  123. $this->_commonDocument->deleteCollection($name);
  124. }
  125. public function testDeleteDocument()
  126. {
  127. $data = $this->_getDocumentData();
  128. $name = $this->_collectionName("testDel");
  129. $this->_commonDocument->createCollection($name);
  130. $doc1 = $this->_makeDocument($data[0]);
  131. $this->_commonDocument->insertDocument($name, $doc1);
  132. $this->_wait();
  133. $doc2 = $this->_makeDocument($data[1]);
  134. $this->_commonDocument->insertDocument($name, $doc2);
  135. $this->_wait();
  136. $this->_commonDocument->deleteDocument($name, $doc1->getId());
  137. $this->_wait();
  138. $fetchdoc = $this->_commonDocument->fetchDocument($name, $doc1->getId());
  139. $this->assertFalse($fetchdoc, "Delete failed");
  140. $fetchdoc = $this->_commonDocument->fetchDocument($name, $doc2->getId());
  141. $this->assertTrue($fetchdoc instanceof Zend_Cloud_DocumentService_Document, "New document not found");
  142. $this->assertEquals($doc2->name, $fetchdoc->name, "Name field wrong");
  143. $this->_commonDocument->deleteCollection($name);
  144. }
  145. public function testReplaceDocument()
  146. {
  147. $data = $this->_getDocumentData();
  148. $name = $this->_collectionName("testRD");
  149. $this->_commonDocument->createCollection($name);
  150. $doc1 = $this->_makeDocument($data[0]);
  151. $this->_commonDocument->insertDocument($name, $doc1);
  152. $doc2 = $this->_makeDocument($data[1]);
  153. $this->_commonDocument->insertDocument($name, $doc2);
  154. $this->_wait();
  155. $doc3 = $this->_makeDocument($data[2]);
  156. $newdoc = new Zend_Cloud_DocumentService_Document($doc3->getFields(), $doc1->getId());
  157. $this->_commonDocument->replaceDocument($name, $newdoc);
  158. $fetchdoc = $this->_commonDocument->fetchDocument($name, $doc1->getId());
  159. $this->assertTrue($fetchdoc instanceof Zend_Cloud_DocumentService_Document, "New document not found");
  160. $this->assertEquals($doc3->name, $fetchdoc->name, "Name field did not update");
  161. $this->assertEquals($doc3->keyword, $fetchdoc->keyword, "Keywords did not update");
  162. $this->_commonDocument->deleteCollection($name);
  163. }
  164. public function testUpdateDocumentIDFields()
  165. {
  166. $data = $this->_getDocumentData();
  167. $name = $this->_collectionName("testUD1");
  168. $this->_commonDocument->createCollection($name);
  169. $doc = $this->_makeDocument($data[0]);
  170. $this->_commonDocument->insertDocument($name, $doc);
  171. $this->_wait();
  172. $doc1 = $this->_makeDocument($data[1]);
  173. $this->_commonDocument->updateDocument($name, $doc->getId(), $doc1->getFields());
  174. $this->_wait();
  175. $fetchdoc = $this->_commonDocument->fetchDocument($name, $doc->getId());
  176. $this->assertTrue($fetchdoc instanceof Zend_Cloud_DocumentService_Document, "New document not found");
  177. $this->assertEquals($doc1->name, $fetchdoc->name, "Name field did not update");
  178. $this->_commonDocument->deleteCollection($name);
  179. }
  180. public function testUpdateDocumentIDDoc()
  181. {
  182. $data = $this->_getDocumentData();
  183. $name = $this->_collectionName("testUD2");
  184. $this->_commonDocument->createCollection($name);
  185. // id is specified, fields from another doc
  186. $doc1 = $this->_makeDocument($data[1]);
  187. $this->_commonDocument->insertDocument($name, $doc1);
  188. $doc2 = $this->_makeDocument($data[2]);
  189. $this->_commonDocument->updateDocument($name, $doc1->getId(), $doc2);
  190. $this->_wait();
  191. $fetchdoc = $this->_commonDocument->fetchDocument($name, $doc1->getId());
  192. $this->assertTrue($fetchdoc instanceof Zend_Cloud_DocumentService_Document, "New document not found");
  193. $this->assertEquals($doc2->name, $fetchdoc->name, "Name field did not update");
  194. $this->assertEquals($doc2->keyword, $fetchdoc->keyword, "Keywords did not update");
  195. $this->_commonDocument->deleteCollection($name);
  196. }
  197. public function testUpdateDocumentDoc()
  198. {
  199. $data = $this->_getDocumentData();
  200. $name = $this->_collectionName("testUD3");
  201. $this->_commonDocument->createCollection($name);
  202. // id is not specified
  203. $doc2 = $this->_makeDocument($data[2]);
  204. $doc3 = new Zend_Cloud_DocumentService_Document($this->_makeDocument($data[3])->getFields(), $doc2->getId());
  205. $this->_commonDocument->insertDocument($name, $doc2);
  206. $this->_wait();
  207. $this->_commonDocument->updateDocument($name, null, $doc3);
  208. $this->_wait();
  209. $fetchdoc = $this->_commonDocument->fetchDocument($name, $doc2->getId());
  210. $this->assertTrue($fetchdoc instanceof Zend_Cloud_DocumentService_Document, "New document not found");
  211. $this->assertEquals($doc3->name, $fetchdoc->name, "Name field did not update");
  212. $this->assertEquals($doc3->keyword, $fetchdoc->keyword, "Keywords did not update");
  213. $this->_commonDocument->deleteCollection($name);
  214. }
  215. public function testQueryString()
  216. {
  217. $name = $this->_collectionName("testQuery");
  218. $doc = $this->_loadData($name);
  219. $query = $this->_queryString($name, $doc[1]->getId(), $doc[2]->getId());
  220. $fetchdocs = $this->_commonDocument->query($name, $query);
  221. $this->assertTrue(count($fetchdocs) >= 2, "Query failed to fetch 2 fields");
  222. foreach($fetchdocs as $fdoc) {
  223. $this->assertContains($fdoc["name"], array($doc[1]->name, $doc[2]->name), "Wrong name in results");
  224. $this->assertContains($fdoc["author"], array($doc[1]->author, $doc[2]->author), "Wrong name in results");
  225. }
  226. $this->_commonDocument->deleteCollection($name);
  227. }
  228. public function testQueryStruct()
  229. {
  230. $name = $this->_collectionName("testStructQuery1");
  231. $doc = $this->_loadData($name);
  232. // query by ID
  233. $query = $this->_commonDocument->select();
  234. $this->assertTrue($query instanceof Zend_Cloud_DocumentService_QueryAdapter);
  235. $query->from($name)->whereId($doc[1]->getId());
  236. $fetchdocs = $this->_commonDocument->query($name, $query);
  237. $this->assertEquals(1, count($fetchdocs), 'Query: ' . $query->assemble() . "\nDocuments:\n" . var_export($fetchdocs, 1));
  238. foreach ($fetchdocs as $fdoc) {
  239. $this->assertEquals($doc[1]->name, $fdoc["name"], "Wrong name in results");
  240. $this->assertEquals($doc[1]->author, $fdoc["author"], "Wrong author in results");
  241. }
  242. $this->_commonDocument->deleteCollection($name);
  243. }
  244. public function testQueryStructWhere()
  245. {
  246. $name = $this->_collectionName("testStructQuery2");
  247. $doc = $this->_loadData($name);
  248. // query by field condition
  249. $query = $this->_commonDocument->select()
  250. ->from($name)->where("year > ?", array(1945));
  251. $fetchdocs = $this->_commonDocument->query($name, $query);
  252. $this->assertEquals(3, count($fetchdocs));
  253. foreach($fetchdocs as $fdoc) {
  254. $this->assertTrue($fdoc["year"] > 1945);
  255. }
  256. $this->_commonDocument->deleteCollection($name);
  257. }
  258. public function testQueryStructLimit()
  259. {
  260. $name = $this->_collectionName("testStructQuery3");
  261. $doc = $this->_loadData($name);
  262. // query with limit
  263. $query = $this->_commonDocument->select()
  264. ->from($name)->where("year > ?", array(1945))->limit(1);
  265. $fetchdocs = $this->_commonDocument->query($name, $query);
  266. $this->assertEquals(1, count($fetchdocs));
  267. foreach($fetchdocs as $fdoc) {
  268. $this->assertTrue($fdoc["year"] > 1945);
  269. $this->assertContains($fdoc["name"], array($doc[0]->name, $doc[2]->name, $doc[3]->name), "Wrong name in results");
  270. }
  271. $this->_commonDocument->deleteCollection($name);
  272. }
  273. public function testQueryStructOrder()
  274. {
  275. $name = $this->_collectionName("testStructQuery4");
  276. $doc = $this->_loadData($name);
  277. // query with sort
  278. $query = $this->_commonDocument->select()
  279. ->from($name)->where("year > ?", array(1945))->order("year", "desc");
  280. $fetchdocs = $this->_commonDocument->query($name, $query);
  281. $this->assertEquals(3, count($fetchdocs));
  282. foreach ($fetchdocs as $fdoc) {
  283. $this->assertEquals($doc[2]->name, $fdoc["name"]);
  284. break;
  285. }
  286. $this->_commonDocument->deleteCollection($name);
  287. }
  288. public function setUp()
  289. {
  290. $this->_config = $this->_getConfig();
  291. $this->_commonDocument = Zend_Cloud_DocumentService_Factory::getAdapter($this->_config);
  292. parent::setUp();
  293. }
  294. abstract protected function _getConfig();
  295. abstract protected function _getDocumentData();
  296. abstract protected function _queryString($domain, $s1, $s2);
  297. protected function _collectionName($name)
  298. {
  299. return $this->_dummyCollectionNamePrefix . $name; //.mt_rand();
  300. }
  301. protected function _wait() {
  302. sleep($this->_waitPeriod);
  303. }
  304. protected function _makeDocument($arr)
  305. {
  306. $id = $arr[self::ID_FIELD];
  307. unset($arr[self::ID_FIELD]);
  308. return new Zend_Cloud_DocumentService_Document($arr, $id);
  309. }
  310. protected function _loadData($name)
  311. {
  312. $data = $this->_getDocumentData();
  313. $this->_commonDocument->createCollection($name);
  314. for($i=0; $i<count($data); $i++) {
  315. $doc[$i] = $this->_makeDocument($data[$i]);
  316. $this->_commonDocument->insertDocument($name, $doc[$i]);
  317. }
  318. $this->_wait();
  319. return $doc;
  320. }
  321. }