/vendor/php-on-couch/php-on-couch/tests/CouchClientViewTest.php

https://github.com/patadejaguar/S.A.F.E.-Open-Source-Microfinance-Suite · PHP · 520 lines · 475 code · 30 blank · 15 comment · 6 complexity · aa483426759b7366a04dddc1d53b6227 MD5 · raw file

  1. <?php
  2. // error_reporting(E_STRICT);
  3. error_reporting(E_ALL);
  4. use PHPOnCouch\CouchClient,
  5. PHPOnCouch\CouchDocument,
  6. PHPOnCouch\CouchAdmin,
  7. PHPOnCouch\Exceptions,
  8. PHPOnCouch\Replicator;
  9. require_once join(DIRECTORY_SEPARATOR, [__DIR__, '_config', 'config.php']);
  10. /**
  11. * @backupGlobals disabled
  12. * @backupStaticAttributes disabled
  13. */
  14. class CouchClientViewTest extends PHPUnit_Framework_TestCase
  15. {
  16. private $host = 'localhost';
  17. private $port = '5984';
  18. public function setUp()
  19. {
  20. $config = config::getInstance();
  21. $this->host = $config->getHost();
  22. $this->port = $config->getPort();
  23. $url = $config->getUrl($this->host, $this->port, $config->getFirstNormalUser());
  24. $aUrl = $config->getUrl($this->host, $this->port, $config->getFirstAdmin());
  25. $this->aclient = new CouchClient($url, 'couchclienttest');
  26. $this->aclient = new CouchClient($aUrl, 'couchclienttest');
  27. try {
  28. $this->aclient->deleteDatabase();
  29. } catch (Exceptions\CouchNotFoundException $e) {
  30. }
  31. $this->aclient->createDatabase();
  32. }
  33. public function tearDown()
  34. {
  35. try {
  36. $this->aclient->deleteDatabase();
  37. } catch (\Exception $e) {
  38. }
  39. $this->aclient = null;
  40. }
  41. protected function _makeView()
  42. {
  43. $doc = new CouchDocument($this->aclient);
  44. $doc->_id = "_design/test";
  45. $views = array();
  46. $map = "function (doc) {
  47. if ( doc.type && doc.type == 'test' ) {
  48. emit(doc.type,1);
  49. }
  50. }";
  51. $reduce = "function (keys, values) {
  52. return sum(values);
  53. }";
  54. $map2 = "function (doc) {
  55. if ( doc.type ) {
  56. emit(doc.type,1);
  57. }
  58. }";
  59. $doc->views = array(
  60. "simple" => array(
  61. "map" => $map,
  62. "reduce" => $reduce
  63. ),
  64. "complex" => array(
  65. "map" => $map2,
  66. "reduce" => $reduce
  67. )
  68. );
  69. }
  70. public function testSimpleViews()
  71. {
  72. $this->_makeView();
  73. $docs = array(
  74. array("_id" => "one", "type" => "test", "param" => null),
  75. array("_id" => "two", "type" => "test", "param" => null),
  76. array("_id" => "three", "type" => "test", "param" => null),
  77. array("_id" => "four", "type" => "test2", "param" => null),
  78. array("_id" => "five", "type" => "test", "param" => null)
  79. );
  80. $this->aclient->storeDocs($docs);
  81. $infos = $this->aclient->getDatabaseInfos();
  82. $this->assertEquals($infos->doc_count, 6);
  83. $test = $this->aclient->reduce(false)->getView("test", "simple");
  84. $this->assertInternalType("object", $test);
  85. $this->assertObjectHasAttribute("total_rows", $test);
  86. $this->assertEquals($test->total_rows, 4);
  87. $this->assertObjectHasAttribute("offset", $test);
  88. $this->assertEquals($test->offset, 0);
  89. $this->assertObjectHasAttribute("rows", $test);
  90. $this->assertInternalType("array", $test->rows);
  91. $this->assertEquals(count($test->rows), 4);
  92. foreach ($test->rows as $row) {
  93. $this->assertObjectHasAttribute("id", $row);
  94. $this->assertObjectHasAttribute("key", $row);
  95. $this->assertObjectHasAttribute("value", $row);
  96. $this->assertObjectNotHasAttribute("doc", $row);
  97. }
  98. }
  99. public function testSimpleReduceViews()
  100. {
  101. $this->_makeView();
  102. $docs = array(
  103. array("_id" => "one", "type" => "test", "param" => null),
  104. array("_id" => "two", "type" => "test", "param" => null),
  105. array("_id" => "three", "type" => "test", "param" => null),
  106. array("_id" => "four", "type" => "test2", "param" => null),
  107. array("_id" => "five", "type" => "test", "param" => null)
  108. );
  109. $this->aclient->storeDocs($docs);
  110. $infos = $this->aclient->getDatabaseInfos();
  111. $this->assertEquals($infos->doc_count, 6);
  112. $test = $this->aclient->getView("test", "simple");
  113. // print_r($test);
  114. $this->assertInternalType("object", $test);
  115. $this->assertObjectHasAttribute("rows", $test);
  116. $this->assertInternalType("array", $test->rows);
  117. $this->assertEquals(count($test->rows), 1);
  118. $row = reset($test->rows);
  119. $this->assertInternalType("object", $row);
  120. $this->assertObjectHasAttribute("value", $row);
  121. $this->assertEquals($row->value, 4);
  122. }
  123. public function testIncludeDocs()
  124. {
  125. $this->_makeView();
  126. $docs = array(
  127. array("_id" => "one", "type" => "test", "param" => null),
  128. array("_id" => "two", "type" => "test", "param" => null),
  129. array("_id" => "three", "type" => "test", "param" => null),
  130. array("_id" => "four", "type" => "test2", "param" => null),
  131. array("_id" => "five", "type" => "test", "param" => null)
  132. );
  133. $this->aclient->storeDocs($docs);
  134. $infos = $this->aclient->getDatabaseInfos();
  135. $this->assertEquals($infos->doc_count, 6);
  136. $test = $this->aclient->reduce(false)->include_docs(true)->getView("test", "simple");
  137. $this->assertInternalType("object", $test);
  138. $this->assertObjectHasAttribute("total_rows", $test);
  139. $this->assertEquals($test->total_rows, 4);
  140. $this->assertObjectHasAttribute("offset", $test);
  141. $this->assertEquals($test->offset, 0);
  142. $this->assertObjectHasAttribute("rows", $test);
  143. $this->assertInternalType("array", $test->rows);
  144. $this->assertEquals(count($test->rows), 4);
  145. foreach ($test->rows as $row) {
  146. $this->assertObjectHasAttribute("id", $row);
  147. $this->assertObjectHasAttribute("key", $row);
  148. $this->assertObjectHasAttribute("value", $row);
  149. $this->assertObjectHasAttribute("doc", $row);
  150. }
  151. }
  152. public function testViewKey()
  153. {
  154. $this->_makeView();
  155. $docs = array(
  156. array("_id" => "one", "type" => "test", "param" => null),
  157. array("_id" => "two", "type" => "test2", "param" => null),
  158. array("_id" => "three", "type" => "test", "param" => null),
  159. array("_id" => "four", "type" => "test2", "param" => null),
  160. array("_id" => "five", "type" => "test2", "param" => null)
  161. );
  162. $this->aclient->storeDocs($docs);
  163. $infos = $this->aclient->getDatabaseInfos();
  164. $this->assertEquals($infos->doc_count, 6);
  165. $test = $this->aclient->reduce(false)->key("test")->getView("test", "complex");
  166. // print_r($test);
  167. $this->assertInternalType("object", $test);
  168. $this->assertObjectHasAttribute("total_rows", $test);
  169. $this->assertEquals($test->total_rows, 5);
  170. $this->assertObjectHasAttribute("offset", $test);
  171. $this->assertEquals($test->offset, 0);
  172. $this->assertObjectHasAttribute("rows", $test);
  173. $this->assertInternalType("array", $test->rows);
  174. $this->assertEquals(count($test->rows), 2);
  175. foreach ($test->rows as $row) {
  176. $this->assertObjectHasAttribute("id", $row);
  177. $this->assertObjectHasAttribute("key", $row);
  178. $this->assertEquals($row->key, "test");
  179. $this->assertObjectHasAttribute("value", $row);
  180. }
  181. }
  182. public function testViewKeys()
  183. {
  184. $this->_makeView();
  185. $docs = array(
  186. array("_id" => "one", "type" => "test", "param" => null),
  187. array("_id" => "two", "type" => "test2", "param" => null),
  188. array("_id" => "three", "type" => "test", "param" => null),
  189. array("_id" => "four", "type" => "test3", "param" => null),
  190. array("_id" => "five", "type" => "test2", "param" => null)
  191. );
  192. $this->aclient->storeDocs($docs);
  193. $infos = $this->aclient->getDatabaseInfos();
  194. $this->assertEquals($infos->doc_count, 6);
  195. $test = $this->aclient->reduce(false)->keys(array("test", "test3"))->getView("test", "complex");
  196. // print_r($test);
  197. $this->assertInternalType("object", $test);
  198. $this->assertObjectHasAttribute("total_rows", $test);
  199. $this->assertEquals($test->total_rows, 5);
  200. $this->assertObjectHasAttribute("offset", $test);
  201. $this->assertEquals($test->offset, 2);
  202. $this->assertObjectHasAttribute("rows", $test);
  203. $this->assertInternalType("array", $test->rows);
  204. $this->assertEquals(count($test->rows), 3);
  205. foreach ($test->rows as $row) {
  206. $this->assertObjectHasAttribute("id", $row);
  207. $this->assertObjectHasAttribute("key", $row);
  208. $this->assertObjectHasAttribute("value", $row);
  209. }
  210. }
  211. public function testViewStartkey()
  212. {
  213. $this->_makeView();
  214. $docs = array(
  215. array("_id" => "one", "type" => "test", "param" => null),
  216. array("_id" => "two", "type" => "test2", "param" => null),
  217. array("_id" => "three", "type" => "test", "param" => null),
  218. array("_id" => "four", "type" => "test3", "param" => null),
  219. array("_id" => "five", "type" => "test2", "param" => null)
  220. );
  221. $this->aclient->storeDocs($docs);
  222. $infos = $this->aclient->getDatabaseInfos();
  223. $this->assertEquals($infos->doc_count, 6);
  224. $test = $this->aclient->reduce(false)->startkey("test3")->getView("test", "complex");
  225. $this->assertInternalType("object", $test);
  226. $this->assertObjectHasAttribute("total_rows", $test);
  227. $this->assertEquals($test->total_rows, 5);
  228. $this->assertObjectHasAttribute("offset", $test);
  229. $this->assertEquals($test->offset, 4);
  230. $this->assertObjectHasAttribute("rows", $test);
  231. $this->assertInternalType("array", $test->rows);
  232. $this->assertEquals(count($test->rows), 1);
  233. foreach ($test->rows as $row) {
  234. $this->assertObjectHasAttribute("id", $row);
  235. $this->assertObjectHasAttribute("key", $row);
  236. $this->assertObjectHasAttribute("value", $row);
  237. }
  238. }
  239. public function testViewEndkey()
  240. {
  241. $this->_makeView();
  242. $docs = array(
  243. array("_id" => "one", "type" => "test", "param" => null),
  244. array("_id" => "two", "type" => "test2", "param" => null),
  245. array("_id" => "three", "type" => "test", "param" => null),
  246. array("_id" => "four", "type" => "test3", "param" => null),
  247. array("_id" => "five", "type" => "test2", "param" => null)
  248. );
  249. $this->aclient->storeDocs($docs);
  250. $infos = $this->aclient->getDatabaseInfos();
  251. $this->assertEquals($infos->doc_count, 6);
  252. $test = $this->aclient->reduce(false)->endkey("test")->getView("test", "complex");
  253. $this->assertInternalType("object", $test);
  254. $this->assertObjectHasAttribute("total_rows", $test);
  255. $this->assertEquals($test->total_rows, 5);
  256. $this->assertObjectHasAttribute("offset", $test);
  257. $this->assertEquals($test->offset, 0);
  258. $this->assertObjectHasAttribute("rows", $test);
  259. $this->assertInternalType("array", $test->rows);
  260. $this->assertEquals(count($test->rows), 2);
  261. foreach ($test->rows as $row) {
  262. $this->assertObjectHasAttribute("id", $row);
  263. $this->assertObjectHasAttribute("key", $row);
  264. $this->assertObjectHasAttribute("value", $row);
  265. }
  266. $test = $this->aclient->reduce(false)->endkey("test")->inclusive_end(false)->getView("test", "complex");
  267. $this->assertInternalType("object", $test);
  268. $this->assertObjectHasAttribute("total_rows", $test);
  269. $this->assertEquals($test->total_rows, 5);
  270. $this->assertObjectHasAttribute("offset", $test);
  271. $this->assertEquals($test->offset, 0);
  272. $this->assertObjectHasAttribute("rows", $test);
  273. $this->assertInternalType("array", $test->rows);
  274. $this->assertEquals(count($test->rows), 0);
  275. foreach ($test->rows as $row) {
  276. $this->assertObjectHasAttribute("id", $row);
  277. $this->assertObjectHasAttribute("key", $row);
  278. $this->assertObjectHasAttribute("value", $row);
  279. }
  280. }
  281. public function testViewStartkeyDocid()
  282. {
  283. $this->_makeView();
  284. $docs = array(
  285. array("_id" => "one", "type" => "test", "param" => null),
  286. array("_id" => "two", "type" => "test2", "param" => null),
  287. array("_id" => "three", "type" => "test", "param" => null),
  288. array("_id" => "four", "type" => "test3", "param" => null),
  289. array("_id" => "five", "type" => "test2", "param" => null)
  290. );
  291. $this->aclient->storeDocs($docs);
  292. $infos = $this->aclient->getDatabaseInfos();
  293. $this->assertEquals($infos->doc_count, 6);
  294. $test = $this->aclient->reduce(false)->startkey("test")->startkey_docid("three")->getView("test", "complex");
  295. // print_r($test);
  296. $this->assertInternalType("object", $test);
  297. $this->assertObjectHasAttribute("total_rows", $test);
  298. $this->assertEquals($test->total_rows, 5);
  299. $this->assertObjectHasAttribute("offset", $test);
  300. $this->assertEquals($test->offset, 1);
  301. $this->assertObjectHasAttribute("rows", $test);
  302. $this->assertInternalType("array", $test->rows);
  303. $this->assertEquals(count($test->rows), 4);
  304. foreach ($test->rows as $row) {
  305. $this->assertObjectHasAttribute("id", $row);
  306. $this->assertObjectHasAttribute("key", $row);
  307. $this->assertObjectHasAttribute("value", $row);
  308. }
  309. }
  310. public function testViewEndkeyDocid()
  311. {
  312. $this->_makeView();
  313. $docs = array(
  314. array("_id" => "one", "type" => "test", "param" => null),
  315. array("_id" => "two", "type" => "test2", "param" => null),
  316. array("_id" => "three", "type" => "test", "param" => null),
  317. array("_id" => "four", "type" => "test3", "param" => null),
  318. array("_id" => "five", "type" => "test2", "param" => null)
  319. );
  320. $this->aclient->storeDocs($docs);
  321. $infos = $this->aclient->getDatabaseInfos();
  322. $this->assertEquals($infos->doc_count, 6);
  323. $test = $this->aclient->reduce(false)->endkey("test2")->endkey_docid("five")->getView("test", "complex");
  324. // print_r($test);
  325. $this->assertInternalType("object", $test);
  326. $this->assertObjectHasAttribute("total_rows", $test);
  327. $this->assertEquals($test->total_rows, 5);
  328. $this->assertObjectHasAttribute("offset", $test);
  329. $this->assertEquals($test->offset, 0);
  330. $this->assertObjectHasAttribute("rows", $test);
  331. $this->assertInternalType("array", $test->rows);
  332. $this->assertEquals(count($test->rows), 3);
  333. foreach ($test->rows as $row) {
  334. $this->assertObjectHasAttribute("id", $row);
  335. $this->assertObjectHasAttribute("key", $row);
  336. $this->assertObjectHasAttribute("value", $row);
  337. }
  338. }
  339. public function testViewLimit()
  340. {
  341. $this->_makeView();
  342. $docs = array(
  343. array("_id" => "one", "type" => "test", "param" => null),
  344. array("_id" => "two", "type" => "test2", "param" => null),
  345. array("_id" => "three", "type" => "test", "param" => null),
  346. array("_id" => "four", "type" => "test3", "param" => null),
  347. array("_id" => "five", "type" => "test2", "param" => null)
  348. );
  349. $this->aclient->storeDocs($docs);
  350. $infos = $this->aclient->getDatabaseInfos();
  351. $this->assertEquals($infos->doc_count, 6);
  352. $test = $this->aclient->reduce(false)->limit(2)->getView("test", "complex");
  353. // print_r($test);
  354. $this->assertInternalType("object", $test);
  355. $this->assertObjectHasAttribute("total_rows", $test);
  356. $this->assertEquals($test->total_rows, 5);
  357. $this->assertObjectHasAttribute("offset", $test);
  358. $this->assertEquals($test->offset, 0);
  359. $this->assertObjectHasAttribute("rows", $test);
  360. $this->assertInternalType("array", $test->rows);
  361. $this->assertEquals(count($test->rows), 2);
  362. foreach ($test->rows as $row) {
  363. $this->assertObjectHasAttribute("id", $row);
  364. $this->assertObjectHasAttribute("key", $row);
  365. $this->assertObjectHasAttribute("value", $row);
  366. }
  367. }
  368. public function testViewSkip()
  369. {
  370. $this->_makeView();
  371. $docs = array(
  372. array("_id" => "one", "type" => "test", "param" => null),
  373. array("_id" => "two", "type" => "test2", "param" => null),
  374. array("_id" => "three", "type" => "test", "param" => null),
  375. array("_id" => "four", "type" => "test3", "param" => null),
  376. array("_id" => "five", "type" => "test2", "param" => null)
  377. );
  378. $this->aclient->storeDocs($docs);
  379. $infos = $this->aclient->getDatabaseInfos();
  380. $this->assertEquals($infos->doc_count, 6);
  381. $test = $this->aclient->reduce(false)->skip(2)->getView("test", "complex");
  382. // print_r($test);
  383. $this->assertInternalType("object", $test);
  384. $this->assertObjectHasAttribute("total_rows", $test);
  385. $this->assertEquals($test->total_rows, 5);
  386. $this->assertObjectHasAttribute("offset", $test);
  387. $this->assertEquals($test->offset, 2);
  388. $this->assertObjectHasAttribute("rows", $test);
  389. $this->assertInternalType("array", $test->rows);
  390. $this->assertEquals(count($test->rows), 3);
  391. foreach ($test->rows as $row) {
  392. $this->assertObjectHasAttribute("id", $row);
  393. $this->assertObjectHasAttribute("key", $row);
  394. $this->assertObjectHasAttribute("value", $row);
  395. }
  396. }
  397. public function testViewDescending()
  398. {
  399. $this->_makeView();
  400. $docs = array(
  401. array("_id" => "one", "type" => "test", "param" => null),
  402. array("_id" => "two", "type" => "test2", "param" => null),
  403. array("_id" => "three", "type" => "test", "param" => null),
  404. array("_id" => "four", "type" => "test3", "param" => null),
  405. array("_id" => "five", "type" => "test2", "param" => null)
  406. );
  407. $this->aclient->storeDocs($docs);
  408. $infos = $this->aclient->getDatabaseInfos();
  409. $this->assertEquals($infos->doc_count, 6);
  410. $test = $this->aclient->reduce(false)->getView("test", "complex");
  411. // print_r($test);
  412. $this->assertInternalType("object", $test);
  413. $this->assertObjectHasAttribute("total_rows", $test);
  414. $this->assertEquals($test->total_rows, 5);
  415. $this->assertObjectHasAttribute("offset", $test);
  416. $this->assertEquals($test->offset, 0);
  417. $this->assertObjectHasAttribute("rows", $test);
  418. $this->assertInternalType("array", $test->rows);
  419. $this->assertEquals(count($test->rows), 5);
  420. $row = reset($test->rows);
  421. $this->assertObjectHasAttribute("key", $row);
  422. $this->assertEquals($row->key, "test");
  423. $test = $this->aclient->reduce(false)->descending(true)->getView("test", "complex");
  424. // print_r($test);
  425. $this->assertInternalType("object", $test);
  426. $this->assertObjectHasAttribute("total_rows", $test);
  427. $this->assertEquals($test->total_rows, 5);
  428. $this->assertObjectHasAttribute("offset", $test);
  429. $this->assertEquals($test->offset, 0);
  430. $this->assertObjectHasAttribute("rows", $test);
  431. $this->assertInternalType("array", $test->rows);
  432. $this->assertEquals(count($test->rows), 5);
  433. $row = reset($test->rows);
  434. $this->assertObjectHasAttribute("key", $row);
  435. $this->assertEquals($row->key, "test3");
  436. }
  437. public function testViewGroup()
  438. {
  439. $this->_makeView();
  440. $docs = array(
  441. array("_id" => "one", "type" => "test", "param" => 1),
  442. array("_id" => "two", "type" => "test2", "param" => 2),
  443. array("_id" => "three", "type" => "test", "param" => 2),
  444. array("_id" => "four", "type" => "test3", "param" => 1),
  445. array("_id" => "five", "type" => "test2", "param" => 1)
  446. );
  447. $this->aclient->storeDocs($docs);
  448. $infos = $this->aclient->getDatabaseInfos();
  449. $this->assertEquals($infos->doc_count, 6);
  450. $doc = CouchDocument::getInstance($this->aclient, "_design/test");
  451. $views = $doc->views;
  452. $views->multigroup = new stdClass();
  453. $views->multigroup->map = "function (doc) {
  454. if ( doc.type && doc.param ) {
  455. emit( [doc.type, doc.param], 1);
  456. }
  457. }";
  458. $views->multigroup->reduce = "function(keys,values) {
  459. return sum(values);
  460. }";
  461. $doc->views = $views;
  462. $test = $this->aclient->group(true)->getView("test", "multigroup");
  463. $this->assertInternalType("object", $test);
  464. $this->assertObjectHasAttribute("rows", $test);
  465. $this->assertInternalType("array", $test->rows);
  466. $this->assertEquals(count($test->rows), 5);
  467. $test = $this->aclient->group(true)->group_level(1)->getView("test", "multigroup");
  468. $this->assertInternalType("object", $test);
  469. $this->assertObjectHasAttribute("rows", $test);
  470. $this->assertInternalType("array", $test->rows);
  471. $this->assertEquals(count($test->rows), 3);
  472. }
  473. public function testViewAsArray()
  474. {
  475. $this->_makeView();
  476. $docs = array(
  477. array("_id" => "one", "type" => "test", "param" => null),
  478. array("_id" => "two", "type" => "test2", "param" => null),
  479. array("_id" => "three", "type" => "test", "param" => null),
  480. array("_id" => "four", "type" => "test3", "param" => null),
  481. array("_id" => "five", "type" => "test2", "param" => null)
  482. );
  483. $this->aclient->storeDocs($docs);
  484. $infos = $this->aclient->getDatabaseInfos();
  485. $this->assertEquals($infos->doc_count, 6);
  486. $test = $this->aclient->reduce(false)->asArray()->getView("test", "complex");
  487. // print_r($test);
  488. $this->assertInternalType("array", $test);
  489. }
  490. }