PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/RegressionTest1.php

https://bitbucket.org/javierbuilder/mongo-php-driver
PHP | 452 lines | 327 code | 85 blank | 40 comment | 24 complexity | 53ac8b78ca0f1f6f188fd1011c029d59 MD5 | raw file
  1. <?php
  2. require_once 'PHPUnit/Framework.php';
  3. class RegressionTest1 extends PHPUnit_Framework_TestCase
  4. {
  5. /**
  6. * Bug PHP-7
  7. * @expectedException MongoConnectionException
  8. */
  9. public function testConnectException1() {
  10. $x = new Mongo("localhost:9923");
  11. }
  12. /**
  13. * Bug PHP-9
  14. */
  15. public function testMem() {
  16. $c = $this->sharedFixture->selectCollection("phpunit", "c");
  17. $arr = array("test" => "1, 2, 3");
  18. $start = memory_get_usage(true);
  19. for($i = 1; $i < 2000; $i++) {
  20. $c->insert($arr);
  21. }
  22. $this->assertEquals($start, memory_get_usage(true));
  23. $c->drop();
  24. }
  25. public function testTinyInsert() {
  26. $c = $this->sharedFixture->selectCollection("phpunit", "c");
  27. $c->drop();
  28. $c->insert(array('_id' => 1));
  29. $obj = $c->findOne();
  30. $this->assertEquals($obj['_id'], 1);
  31. }
  32. /**
  33. * @expectedException MongoException
  34. */
  35. public function testTinyInsert2() {
  36. $c = $this->sharedFixture->selectCollection("phpunit", "c");
  37. $c->drop();
  38. $c->remove();
  39. $c->insert(array());
  40. $obj = $c->findOne();
  41. $this->assertEquals($obj, NULL);
  42. }
  43. public function testIdInsert() {
  44. if (preg_match($this->sharedFixture->version_51, phpversion())) {
  45. $this->markTestSkipped("No implicit __toString in 5.1");
  46. return;
  47. }
  48. $c = $this->sharedFixture->selectCollection("phpunit", "c");
  49. $a = array('_id' => 1);
  50. $c->insert($a);
  51. $this->assertArrayHasKey('_id', $a);
  52. $c->drop();
  53. $a = array('x' => 1, '_id' => new MongoId());
  54. $id = (string)$a['_id'];
  55. $c->insert($a);
  56. $x = $c->findOne();
  57. $this->assertArrayHasKey('_id', $x);
  58. $this->assertEquals((string)$x['_id'], $id);
  59. }
  60. public function testFatalClone() {
  61. if (phpversion() == "5.2.9") {
  62. $output = "";
  63. $exit_code = 0;
  64. exec("php tests/fatal1.php", $output, $exit_code);
  65. $unclonable = "Fatal error: Trying to clone an uncloneable object";
  66. if (count($output) > 0) {
  67. $this->assertEquals($unclonable, substr($output[1], 0, strlen($unclonable)), json_encode($output));
  68. }
  69. $this->assertEquals(255, $exit_code);
  70. exec("php tests/fatal2.php", $output, $exit_code);
  71. if (count($output) > 0) {
  72. $this->assertEquals($unclonable, substr($output[3], 0, strlen($unclonable)), json_encode($output));
  73. }
  74. $this->assertEquals(255, $exit_code);
  75. }
  76. }
  77. public function testRealloc() {
  78. if (preg_match($this->sharedFixture->version_51, phpversion())) {
  79. $this->markTestSkipped('Cannot open files w/out absolute path in 5.1.');
  80. return;
  81. }
  82. $db = $this->sharedFixture->selectDB('webgenius');
  83. $tbColl = $db->selectCollection('Text_Block');
  84. $text = file_get_contents('tests/mongo-bug.txt');
  85. $arr = array('text' => $text,);
  86. $tbColl->insert($arr);
  87. }
  88. public function testIdRealloc() {
  89. if (preg_match($this->sharedFixture->version_51, phpversion())) {
  90. $this->markTestSkipped('Cannot open files w/out absolute path in 5.1.');
  91. return;
  92. }
  93. $db = $this->sharedFixture->selectDB('webgenius');
  94. $tbColl = $db->selectCollection('Text_Block');
  95. $text = file_get_contents('tests/id-alloc.txt');
  96. $arr = array('text' => $text, 'id2' => new MongoId());
  97. $tbColl->insert($arr);
  98. }
  99. public function testSafeInsertRealloc() {
  100. if (preg_match($this->sharedFixture->version_51, phpversion())) {
  101. $this->markTestSkipped('Cannot open files w/out absolute path in 5.1.');
  102. return;
  103. }
  104. $db = $this->sharedFixture->selectDB('webgenius');
  105. $tbColl = $db->selectCollection('Text_Block');
  106. $text = file_get_contents('tests/id-alloc.txt');
  107. $arr = array('text' => $text);
  108. $x = $tbColl->insert($arr, true);
  109. $this->assertEquals($x['err'], null);
  110. }
  111. public function testForEachKey() {
  112. $c = $this->sharedFixture->selectCollection('x', 'y');
  113. $c->drop();
  114. $c->insert(array('_id' => "xsf0", 'x' => 1));
  115. $c->insert(array('_id' => 1, 'x' => 2));
  116. $c->insert(array('_id' => true, 'x' => 3));
  117. $c->insert(array('_id' => null, 'x' => 4));
  118. $c->insert(array('_id' => new MongoId(), 'x' => 5));
  119. $c->insert(array('_id' => new MongoDate(), 'x' => 6));
  120. $cursor = $c->find()->sort(array('x'=>1));
  121. $data = array();
  122. foreach($cursor as $k=>$v) {
  123. $data[] = $k;
  124. }
  125. $this->assertEquals('xsf0', $data[0]);
  126. $this->assertEquals('1', $data[1]);
  127. $this->assertEquals('1', $data[2]);
  128. $this->assertEquals('', $data[3]);
  129. $this->assertEquals(24, strlen($data[4]), "key: ".$data[4]);
  130. $this->assertEquals(21, strlen($data[5]), "key: ".$data[5]);
  131. }
  132. public function testIn() {
  133. $c = $this->sharedFixture->selectCollection('x', 'y');
  134. $x = $c->findOne(array('oldId' =>array('$in' =>array ())));
  135. if ($x != NULL) {
  136. $this->assertArrayNotHasKey('$err', $x, json_encode($x));
  137. }
  138. }
  139. public function testBatchInsert() {
  140. $c = $this->sharedFixture->selectCollection('x', 'y');
  141. $c->drop();
  142. $a = array();
  143. for($i=0; $i < 10; $i++) {
  144. $a[] = array('time' => new MongoDate(), 'x' => $i, "label" => "ooo$i");
  145. }
  146. $c->batchInsert($a);
  147. for ($i=0; $i <10; $i++) {
  148. $this->assertArrayHasKey('_id', $a[$i], json_encode($a));
  149. }
  150. }
  151. /**
  152. * Mongo::toString() was destroying Mongo::server
  153. */
  154. public function testMongoToString() {
  155. $m = new Mongo();
  156. $str1 = $m->__toString();
  157. $str2 = $m->__toString();
  158. $this->assertEquals("localhost:27017", $str2);
  159. $this->assertEquals($str1, $str2);
  160. }
  161. public function testCursorCount() {
  162. $c = $this->sharedFixture->selectCollection('x', 'y');
  163. $c->drop();
  164. for($i=0; $i < 10; $i++) {
  165. $c->insert(array('foo'=>'bar'));
  166. }
  167. $cursor = $c->find();
  168. $this->assertEquals(10, $cursor->count());
  169. $cursor->limit(2);
  170. $this->assertEquals(10, $cursor->count());
  171. $this->assertEquals(2, $cursor->count(true));
  172. }
  173. public function testCursorConversion() {
  174. $c = $this->sharedFixture->selectCollection('x', 'y');
  175. $c->drop();
  176. for($i=0; $i < 10; $i++) {
  177. $c->insert(array('foo'=>'bar'));
  178. }
  179. $cursor = $c->find();
  180. foreach ($cursor as $k=>$v) {
  181. $this->assertTrue(is_string($k));
  182. $this->assertTrue($v['_id'] instanceof MongoId);
  183. }
  184. $c->remove();
  185. $c->insert(array("_id" => 20));
  186. $cursor = $c->find();
  187. $cursor->next();
  188. $this->assertTrue(is_string($cursor->key()));
  189. $v = $cursor->current();
  190. $this->assertTrue(is_int($v['_id']));
  191. }
  192. public function testCountReturnsInt() {
  193. $c = $this->sharedFixture->selectCollection('x', 'y');
  194. $c->drop();
  195. $this->assertTrue(is_int($c->count()));
  196. $cursor = $c->find();
  197. $this->assertTrue(is_int($cursor->count()));
  198. for($i=0; $i < 10; $i++) {
  199. $c->insert(array('foo'=>'bar'));
  200. }
  201. $cursor = $c->find();
  202. $x = $cursor->count();
  203. $this->assertTrue(is_int($x), $x);
  204. $cursor->limit(4);
  205. $this->assertTrue(is_int($cursor->count()));
  206. $this->assertTrue(is_int($c->count()));
  207. }
  208. public function testNestedArray() {
  209. $count = 1500;
  210. $mongo = new Mongo();
  211. $mongoDB = $mongo->selectDb('testdb');
  212. $mongoCollection = $mongoDB->selectCollection('testcollection');
  213. $values = array();
  214. for($i = 0; $i < $count; $i++) {
  215. $values[] = new MongoId($i);
  216. }
  217. $mongoCursor = $mongoCollection->find(array('_id' => array('$in' => $values)));
  218. while ($mongoCursor->hasNext()) {
  219. $mongoCursor->getNext();
  220. }
  221. }
  222. public function testEnsureIndex() {
  223. $mongoConnection = new Mongo('127.0.0.1:27017');
  224. $collection = $mongoConnection->selectCollection("debug", "col1");
  225. $data = array("field"=>"some data","date"=>date("Y-m-s"));
  226. $this->assertEquals(true, $collection->save($data));
  227. $tmp = array("date" => 1);
  228. $this->assertEquals(1, $tmp['date']);
  229. $this->assertEquals(true, $collection->ensureIndex($tmp));
  230. $this->assertEquals(1, $tmp['date']);
  231. }
  232. public function testGridFSProps() {
  233. $m = new Mongo();
  234. $grid = $m->foo->getGridFS();
  235. $x = new MongoGridFSFile($grid, array());
  236. $this->assertNull($x->getFilename());
  237. $this->assertNull($x->getSize());
  238. }
  239. public function testCryllic() {
  240. $c = $this->sharedFixture->phpunit->c;
  241. try {
  242. $c->insert(array("x" => "\xC3\x84"));
  243. }
  244. catch (MongoException $e) {
  245. $this->assertTrue(false, $e);
  246. }
  247. }
  248. public function testEmptyQuery() {
  249. $c = $this->sharedFixture->phpunit->c;
  250. $c->drop();
  251. for ($i=0; $i<10; $i++) {
  252. $c->insert(array("x" => $i));
  253. }
  254. $cursor = $c->find(array(), array("x" => 1))->sort(array("x" => 1));
  255. $count = 0;
  256. foreach ($cursor as $doc) {
  257. $count++;
  258. }
  259. $this->assertEquals(10, $count);
  260. }
  261. /**
  262. * @expectedException MongoException
  263. */
  264. public function testNonUTF81() {
  265. $c = $this->sharedFixture->phpunit->c;
  266. $c->insert(array("x" => "\xFE"));
  267. }
  268. public function testNonUTF82() {
  269. $c = $this->sharedFixture->phpunit->c;
  270. ini_set("mongo.utf8", 0);
  271. $c->insert(array("x" => "\xFE"));
  272. ini_set("mongo.utf8", 1);
  273. $c->drop();
  274. }
  275. /**
  276. * @expectedException MongoConnectionException
  277. */
  278. public function testNoPassword1() {
  279. new Mongo("mongodb://admin@anyhost-and-nonexistent");
  280. }
  281. /**
  282. * @expectedException MongoConnectionException
  283. */
  284. public function testNoPassword2() {
  285. new Mongo("mongodb://admin@anyhost-and-nonexistent:foo");
  286. }
  287. /**
  288. * @expectedException MongoConnectionException
  289. */
  290. public function testNoPassword3() {
  291. new Mongo("mongodb://admin@anyhost-and-nonexistent:27017");
  292. }
  293. /**
  294. * @expectedException MongoConnectionException
  295. */
  296. public function testNoPassword4() {
  297. new Mongo("mongodb://@anyhost-and-nonexistent:27017");
  298. }
  299. /**
  300. * @expectedException MongoConnectionException
  301. */
  302. public function testNoPassword5() {
  303. new Mongo("mongodb://:@anyhost-and-nonexistent");
  304. }
  305. public function testFatalRecursion() {
  306. if (preg_match($this->sharedFixture->version_51, phpversion())) {
  307. $this->markTestSkipped('annoying output in 5.1.');
  308. return;
  309. }
  310. $output = "";
  311. $exit_code = 0;
  312. exec("php tests/fatal4.php", $output, $exit_code);
  313. $msg = "Fatal error: Nesting level too deep";
  314. if (count($output) > 0) {
  315. $this->assertEquals($msg, substr($output[1], 0, strlen($msg)), json_encode($output));
  316. }
  317. }
  318. public function testStaticDtor() {
  319. $f = new Foo2();
  320. $f->f();
  321. }
  322. public function testGetMore() {
  323. $c = $this->sharedFixture->phpunit->c;
  324. for($i=0; $i<500; $i++) {
  325. $c->insert(array("x" => new MongoDate(), "count" => $i, "my string" => "doo dee doo"));
  326. }
  327. $cursor = $c->find();
  328. foreach ($cursor as $v) {
  329. }
  330. }
  331. /**
  332. * @expectedException MongoException
  333. */
  334. public function testNonUTF8Embed() {
  335. $this->sharedFixture->phpunit->c->insert(array('x'=>array("y" => "\xFF"), "y" => array()));
  336. }
  337. /**
  338. * @expectedException MongoConnectionException
  339. */
  340. public function testInvalidConnectionSyntax() {
  341. $m = new Mongo("mongodb://name:password@localhost/");
  342. }
  343. /**
  344. * @expectedException MongoException
  345. */
  346. public function testTooBigInsert() {
  347. if (preg_match($this->sharedFixture->version_51, phpversion())) {
  348. $this->markTestSkipped('annoying output in 5.1.');
  349. return;
  350. }
  351. $contents = file_get_contents('tests/pycon-poster.pdf');
  352. $arr = array();
  353. for ($i=0; $i<7; $i++) {
  354. $arr[] = array("content" => new MongoBinData($contents), "i" => $i);
  355. }
  356. $this->sharedFixture->phpunit->c->batchInsert($arr);
  357. }
  358. }
  359. class Foo2 {
  360. static $c;
  361. public function f() {
  362. $m = new Mongo();
  363. $db = new MongoDB($m, 'foo');
  364. Foo2::$c = new MongoCollection($db, 'bar');
  365. }
  366. }
  367. ?>