TestCase.php 14 KB

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