/tests/MongoInt32Test.php

https://github.com/hghazal/mongo-php-driver · PHP · 352 lines · 293 code · 43 blank · 16 comment · 5 complexity · af7606f9e4d25972c1371c233fb47092 MD5 · raw file

  1. <?php
  2. require_once 'PHPUnit/Framework.php';
  3. class MongoInt32Test extends PHPUnit_Framework_TestCase
  4. {
  5. public function __call($method, $args) {
  6. if ($method == 'assertInternalType') {
  7. $this->assertType($args[0], $args[1]);
  8. }
  9. }
  10. function setup()
  11. {
  12. if (PHP_INT_SIZE != 4) {
  13. $this->markTestSkipped("Only for 32 bit platforms");
  14. }
  15. ini_set('mongo.native_long', 0);
  16. ini_set('mongo.long_as_object', 0);
  17. $m = new Mongo();
  18. $this->object = $m->selectCollection("phpunit", "ints");
  19. $this->object->drop();
  20. }
  21. function testNormalInsert32()
  22. {
  23. $c = $this->object;
  24. $c->insert(array('int32' => 1234567890));
  25. $x = $c->findOne();
  26. $this->assertSame(1234567890, $x['int32']);
  27. }
  28. function testNormalInsert64()
  29. {
  30. $c = $this->object;
  31. $c->insert(array('int64' => 12345678901234567890));
  32. $x = $c->findOne();
  33. $this->assertSame(12345678901234567890, $x['int64']);
  34. }
  35. function testNormalInsertNativeLong()
  36. {
  37. ini_set('mongo.native_long', 1);
  38. $c = $this->object;
  39. $c->insert(array('int64' => 12345678901234567890));
  40. $x = $c->findOne();
  41. $this->assertSame(12345678901234567890, $x['int64']);
  42. }
  43. function testNormalLongAsObject()
  44. {
  45. ini_set('mongo.long_as_object', 1);
  46. $c = $this->object;
  47. $c->insert(array('int64' => 123456789012345));
  48. $x = $c->findOne();
  49. $this->assertInternalType('float', $x['int64']);
  50. $this->assertSame(123456789012345, $x['int64']);
  51. }
  52. function testNativeLongAsObject()
  53. {
  54. ini_set('mongo.native_long', 1);
  55. ini_set('mongo.long_as_object', 1);
  56. $c = $this->object;
  57. $c->insert(array('int64' => 123456789012345));
  58. $x = $c->findOne();
  59. $this->assertInternalType('float', $x['int64']);
  60. $this->assertSame(123456789012345, $x['int64']);
  61. }
  62. /** objects with int ctor */
  63. function testInsertIntMongo32()
  64. {
  65. $c = $this->object;
  66. $c->insert(array('int32' => new MongoInt32(1234567890)));
  67. $x = $c->findOne();
  68. $this->assertSame(1234567890, $x['int32']);
  69. }
  70. function testInsertIntMongo32B()
  71. {
  72. $c = $this->object;
  73. $c->insert(array('int32' => new MongoInt32(1234567890123)));
  74. $x = $c->findOne();
  75. $this->assertSame(PHP_INT_MAX, $x['int32']);
  76. }
  77. function testInsertIntMongo32NativeLong()
  78. {
  79. ini_set('mongo.native_long', 1);
  80. $c = $this->object;
  81. $c->insert(array('int32' => new MongoInt32(1234567890)));
  82. $x = $c->findOne();
  83. $this->assertSame(1234567890, $x['int32']);
  84. }
  85. function testInsertIntMongo32NativeLongB()
  86. {
  87. ini_set('mongo.native_long', 1);
  88. $c = $this->object;
  89. $c->insert(array('int32' => new MongoInt32(1234567890123)));
  90. $x = $c->findOne();
  91. $this->assertSame(PHP_INT_MAX, $x['int32']);
  92. }
  93. function testInsertIntMongo32LongAsObject()
  94. {
  95. ini_set('mongo.long_as_object', 1);
  96. $c = $this->object;
  97. $c->insert(array('int32' => new MongoInt32(1234567890)));
  98. $x = $c->findOne();
  99. $this->assertSame(1234567890, $x['int32']);
  100. }
  101. function testInsertIntMongo32LongAsObjectB()
  102. {
  103. ini_set('mongo.long_as_object', 1);
  104. $c = $this->object;
  105. $c->insert(array('int32' => new MongoInt32(1234567890123)));
  106. $x = $c->findOne();
  107. $this->assertSame(PHP_INT_MAX, $x['int32']);
  108. }
  109. function testInsertIntMongo64()
  110. {
  111. $c = $this->object;
  112. $c->insert(array('int64' => new MongoInt64(1234567890)));
  113. $x = $c->findOne();
  114. $this->assertInternalType('float', $x['int64']);
  115. $this->assertSame(1234567890.0, $x['int64']);
  116. }
  117. function testInsertIntMongo64B()
  118. {
  119. $c = $this->object;
  120. $c->insert(array('int64' => new MongoInt64(123456789012345)));
  121. $x = $c->findOne();
  122. $this->assertInternalType('float', $x['int64']);
  123. $this->assertSame(1.0, $x['int64']);
  124. }
  125. /**
  126. * @expectedException MongoCursorException
  127. */
  128. function testInsertIntMongo64NativeLong()
  129. {
  130. ini_set('mongo.native_long', 1);
  131. $c = $this->object;
  132. $c->insert(array('int64' => new MongoInt64(1234567890)));
  133. $x = $c->findOne();
  134. }
  135. /**
  136. * @expectedException MongoCursorException
  137. */
  138. function testInsertIntMongo64NativeLongB()
  139. {
  140. ini_set('mongo.native_long', 1);
  141. $c = $this->object;
  142. $c->insert(array('int64' => new MongoInt64(123456789012345)));
  143. $x = $c->findOne();
  144. }
  145. function testInsertIntMongo64LongAsObject()
  146. {
  147. ini_set('mongo.long_as_object', 1);
  148. $c = $this->object;
  149. $c->insert(array('int64' => new MongoInt64(1234567890)));
  150. $x = $c->findOne();
  151. $this->assertEquals(new MongoInt64('1234567890'), $x['int64']);
  152. }
  153. function testInsertIntMongo64LongAsObjectB()
  154. {
  155. ini_set('mongo.long_as_object', 1);
  156. $c = $this->object;
  157. $c->insert(array('int64' => new MongoInt64(1234567890123)));
  158. $x = $c->findOne();
  159. $this->assertEquals(new MongoInt64('1234567890123'), $x['int64']);
  160. }
  161. function testInsertIntMongo64LongAsObjectC()
  162. {
  163. ini_set('mongo.long_as_object', 1);
  164. $c = $this->object;
  165. $c->insert(array('int64' => new MongoInt64(123456789012345)));
  166. $x = $c->findOne();
  167. $this->assertEquals(new MongoInt64('1'), $x['int64']);
  168. }
  169. /** objects with string ctor */
  170. function testInsertStringMongo32()
  171. {
  172. $c = $this->object;
  173. $c->insert(array('int32' => new MongoInt32('1234567890')));
  174. $x = $c->findOne();
  175. $this->assertSame(1234567890, $x['int32']);
  176. }
  177. function testInsertStringMongo32B()
  178. {
  179. $c = $this->object;
  180. $c->insert(array('int32' => new MongoInt32('123456789012345')));
  181. $x = $c->findOne();
  182. $this->assertSame(PHP_INT_MAX, $x['int32']);
  183. }
  184. function testInsertStringMongo32NativeLong()
  185. {
  186. ini_set('mongo.native_long', 1);
  187. $c = $this->object;
  188. $c->insert(array('int32' => new MongoInt32('1234567890')));
  189. $x = $c->findOne();
  190. $this->assertSame(1234567890, $x['int32']);
  191. }
  192. function testInsertStringMongo32NativeLongB()
  193. {
  194. ini_set('mongo.native_long', 1);
  195. $c = $this->object;
  196. $c->insert(array('int32' => new MongoInt32('123456789012345')));
  197. $x = $c->findOne();
  198. $this->assertSame(PHP_INT_MAX, $x['int32']);
  199. }
  200. function testInsertStringMongo32LongAsObject()
  201. {
  202. ini_set('mongo.long_as_object', 1);
  203. $c = $this->object;
  204. $c->insert(array('int32' => new MongoInt32('1234567890')));
  205. $x = $c->findOne();
  206. $this->assertSame(1234567890, $x['int32']);
  207. }
  208. function testInsertStringMongo32LongAsObjectB()
  209. {
  210. ini_set('mongo.long_as_object', 1);
  211. $c = $this->object;
  212. $c->insert(array('int32' => new MongoInt32('123456789012345')));
  213. $x = $c->findOne();
  214. $this->assertSame(PHP_INT_MAX, $x['int32']);
  215. }
  216. function testInsertStringMongo64()
  217. {
  218. $c = $this->object;
  219. $c->insert(array('int64' => new MongoInt64('1234567890')));
  220. $x = $c->findOne();
  221. $this->assertInternalType('float', $x['int64']);
  222. $this->assertSame(1234567890.0, $x['int64']);
  223. }
  224. function testInsertStringMongo64B()
  225. {
  226. $c = $this->object;
  227. $c->insert(array('int64' => new MongoInt64('123456789012345')));
  228. $x = $c->findOne();
  229. $this->assertInternalType('float', $x['int64']);
  230. $this->assertSame(123456789012345.0, $x['int64']);
  231. }
  232. /**
  233. * @expectedException MongoCursorException
  234. */
  235. function testInsertStringMongo64NativeLong()
  236. {
  237. ini_set('mongo.native_long', 1);
  238. $c = $this->object;
  239. $c->insert(array('int64' => new MongoInt64('1234567890')));
  240. $x = $c->findOne();
  241. $this->assertSame(1234567890, $x['int64']);
  242. }
  243. /**
  244. * @expectedException MongoCursorException
  245. */
  246. function testInsertStringMongo64NativeLongB()
  247. {
  248. ini_set('mongo.native_long', 1);
  249. $c = $this->object;
  250. $c->insert(array('int64' => new MongoInt64('123456789012345')));
  251. $x = $c->findOne();
  252. $this->assertSame(123456789012345, $x['int64']);
  253. }
  254. function testInsertStringMongo64LongAsObject()
  255. {
  256. ini_set('mongo.long_as_object', 1);
  257. $c = $this->object;
  258. $c->insert(array('int64' => new MongoInt64('1234567890')));
  259. $x = $c->findOne();
  260. $this->assertEquals(new MongoInt64('1234567890'), $x['int64']);
  261. $this->assertSame('1234567890', $x['int64']->__toString());
  262. }
  263. function testInsertStringMongo64LongAsObjectB()
  264. {
  265. ini_set('mongo.long_as_object', 1);
  266. $c = $this->object;
  267. $c->insert(array('int64' => new MongoInt64('123456789012345')));
  268. $x = $c->findOne();
  269. $this->assertEquals(new MongoInt64('123456789012345'), $x['int64']);
  270. $this->assertSame('123456789012345', $x['int64']->__toString());
  271. }
  272. /** Tests for object generation */
  273. function testObjectCreationInt32()
  274. {
  275. $a = new MongoInt32('1234567890');
  276. $this->assertSame('1234567890', $a->__toString());
  277. $a = new MongoInt32('1234567890123456');
  278. $this->assertSame('1234567890123456', $a->__toString());
  279. $a = new MongoInt32('123456789012345678901234567890');
  280. $this->assertSame('123456789012345678901234567890', $a->__toString());
  281. }
  282. function testObjectCreationInt64()
  283. {
  284. $a = new MongoInt64('1234567890');
  285. $this->assertSame('1234567890', $a->__toString());
  286. $a = new MongoInt64('1234567890123456');
  287. $this->assertSame('1234567890123456', $a->__toString());
  288. $a = new MongoInt64('123456789012345678901234567890');
  289. $this->assertSame('123456789012345678901234567890', $a->__toString());
  290. }
  291. /** Tests for things outside of the int64 range */
  292. function testInsertIntMongo64Big()
  293. {
  294. $c = $this->object;
  295. $c->insert(array('int64' => new MongoInt64(123456789012345678901234567890)));
  296. $x = $c->findOne();
  297. $this->assertInternalType('float', $x['int64']);
  298. $this->assertSame((double) 1, $x['int64']);
  299. }
  300. function testInsertStringMongo64Big()
  301. {
  302. $c = $this->object;
  303. $c->insert(array('int64' => new MongoInt64('123456789012345678901234567890')));
  304. $x = $c->findOne();
  305. $this->assertInternalType('float', $x['int64']);
  306. $this->assertSame(9223372036854775807, $x['int64']);
  307. }
  308. }
  309. ?>