MongoClientTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  4. /**
  5. * @author alcaeus <alcaeus@alcaeus.org>
  6. */
  7. class MongoClientTest extends TestCase
  8. {
  9. /**
  10. * @dataProvider provideConnectionUri
  11. */
  12. public function testConnectionUri($uri, $expected)
  13. {
  14. $this->skipTestIf(extension_loaded('mongo'));
  15. $this->assertSame($expected, (string) (new \MongoClient($uri, ['connect' => false])));
  16. }
  17. public function provideConnectionUri()
  18. {
  19. yield ['default', sprintf('mongodb://%s:%d', \MongoClient::DEFAULT_HOST, \MongoClient::DEFAULT_PORT)];
  20. yield ['localhost', 'mongodb://localhost'];
  21. yield ['mongodb://localhost', 'mongodb://localhost'];
  22. if (version_compare(phpversion('mongodb'), '1.4.0', '>=')) {
  23. yield ['mongodb+srv://foo.example.com', 'mongodb+srv://foo.example.com'];
  24. }
  25. }
  26. public function testSerialize()
  27. {
  28. $this->assertIsString(serialize($this->getClient()));
  29. }
  30. public function testGetDb()
  31. {
  32. $client = $this->getClient();
  33. $db = $client->selectDB('mongo-php-adapter');
  34. $this->assertInstanceOf('\MongoDB', $db);
  35. $this->assertSame('mongo-php-adapter', (string) $db);
  36. }
  37. public function testSelectDBWithEmptyName()
  38. {
  39. $this->expectException(\Exception::class);
  40. $this->expectExceptionMessage('Database name cannot be empty');
  41. $this->getClient()->selectDB('');
  42. }
  43. public function testSelectDBWithInvalidName()
  44. {
  45. $this->expectException(\Exception::class);
  46. $this->expectExceptionMessage('Database name contains invalid characters');
  47. $this->getClient()->selectDB('/');
  48. }
  49. public function testGetDbProperty()
  50. {
  51. $client = $this->getClient();
  52. $db = $client->{'mongo-php-adapter'};
  53. $this->assertInstanceOf('\MongoDB', $db);
  54. $this->assertSame('mongo-php-adapter', (string) $db);
  55. }
  56. public function testGetCollection()
  57. {
  58. $client = $this->getClient();
  59. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  60. $this->assertInstanceOf('MongoCollection', $collection);
  61. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  62. }
  63. public function testGetHosts()
  64. {
  65. $client = $this->getClient();
  66. $hosts = $client->getHosts();
  67. $this->assertMatches(
  68. [
  69. 'localhost:27017;-;.;' . getmypid() => [
  70. 'host' => 'localhost',
  71. 'port' => 27017,
  72. 'health' => 1,
  73. 'state' => 0,
  74. ],
  75. ],
  76. $hosts
  77. );
  78. }
  79. public function testGetHostsExceptionHandling()
  80. {
  81. $this->expectException(\MongoConnectionException::class);
  82. $this->expectErrorMessageMatches('/fake_host/');
  83. $client = $this->getClient(null, 'mongodb://fake_host');
  84. $client->getHosts();
  85. }
  86. public function testReadPreference()
  87. {
  88. $client = $this->getClient();
  89. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $client->getReadPreference());
  90. $this->assertTrue($client->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  91. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $client->getReadPreference());
  92. }
  93. public function testWriteConcern()
  94. {
  95. $client = $this->getClient();
  96. $this->assertTrue($client->setWriteConcern('majority', 100));
  97. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $client->getWriteConcern());
  98. }
  99. public function testListDBs()
  100. {
  101. $document = ['foo' => 'bar'];
  102. $this->getCollection()->insert($document);
  103. $databases = $this->getClient()->listDBs();
  104. $this->assertSame(1.0, $databases['ok']);
  105. $this->assertArrayHasKey('totalSize', $databases);
  106. $this->assertArrayHasKey('databases', $databases);
  107. foreach ($databases['databases'] as $database) {
  108. $this->assertArrayHasKey('name', $database);
  109. $this->assertArrayHasKey('empty', $database);
  110. $this->assertArrayHasKey('sizeOnDisk', $database);
  111. if ($database['name'] == 'mongo-php-adapter') {
  112. $this->assertFalse($database['empty']);
  113. return;
  114. }
  115. }
  116. $this->fail('Could not find mongo-php-adapter database in list');
  117. }
  118. public function testNoPrefixUri()
  119. {
  120. $client = $this->getClient(null, 'localhost');
  121. $this->assertNotNull($client);
  122. }
  123. /**
  124. * @dataProvider dataReadPreferenceOptionsAreInherited
  125. */
  126. public function testReadPreferenceOptionsAreInherited($options, $uri, $expectedTagsets)
  127. {
  128. $client = $this->getClient($options, $uri);
  129. $collection = $client->selectCollection('test', 'foo');
  130. $this->assertSame(
  131. [
  132. 'type' => \MongoClient::RP_SECONDARY_PREFERRED,
  133. 'tagsets' => $expectedTagsets
  134. ],
  135. $collection->getReadPreference()
  136. );
  137. }
  138. public static function dataReadPreferenceOptionsAreInherited()
  139. {
  140. $options = [
  141. 'readPreference' => \MongoClient::RP_SECONDARY_PREFERRED,
  142. 'readPreferenceTags' => 'a:b',
  143. ];
  144. $overriddenOptions = [
  145. 'readPreference' => \MongoClient::RP_NEAREST,
  146. 'readPreferenceTags' => 'c:d',
  147. ];
  148. $multipleTagsets = [
  149. 'readPreference' => \MongoClient::RP_SECONDARY_PREFERRED,
  150. 'readPreferenceTags' => 'a:b,c:d',
  151. ];
  152. return [
  153. 'optionsArray' => [
  154. 'options' => $options,
  155. 'uri' => 'mongodb://localhost',
  156. 'expectedTagsets' => [['a' => 'b']],
  157. ],
  158. 'queryString' => [
  159. 'options' => [],
  160. 'uri' => 'mongodb://localhost/?' . self::makeOptionString($options),
  161. 'expectedTagsets' => [['a' => 'b']],
  162. ],
  163. 'multipleInQueryString' => [
  164. 'options' => [],
  165. 'uri' => 'mongodb://localhost/?' . self::makeOptionString($options) . '&readPreferenceTags=c:d',
  166. 'expectedTagsets' => [['a' => 'b'], ['c' => 'd']],
  167. ],
  168. 'overridden' => [
  169. 'options' => $options,
  170. 'uri' => 'mongodb://localhost/?' . self::makeOptionString($overriddenOptions),
  171. 'expectedTagsets' => [['c' => 'd'], ['a' => 'b']],
  172. ],
  173. 'multipleTagsetsOptions' => [
  174. 'options' => $multipleTagsets,
  175. 'uri' => 'mongodb://localhost',
  176. 'expectedTagsets' => [['a' => 'b', 'c' => 'd']],
  177. ],
  178. 'multipleTagsetsQueryString' => [
  179. 'options' => null,
  180. 'uri' => 'mongodb://localhost/?' . self::makeOptionString($multipleTagsets),
  181. 'expectedTagsets' => [['a' => 'b', 'c' => 'd']],
  182. ],
  183. ];
  184. }
  185. /**
  186. * @dataProvider dataWriteConcernOptionsAreInherited
  187. */
  188. public function testWriteConcernOptionsAreInherited($options, $uri)
  189. {
  190. $client = $this->getClient($options, $uri);
  191. $collection = $client->selectCollection('test', 'foo');
  192. $this->assertSame(['w' => 'majority', 'wtimeout' => 666], $collection->getWriteConcern());
  193. }
  194. public static function dataWriteConcernOptionsAreInherited()
  195. {
  196. $options = [
  197. 'w' => 'majority',
  198. 'wTimeoutMs' => 666,
  199. ];
  200. $overriddenOptions = [
  201. 'w' => '2',
  202. 'wTimeoutMs' => 333,
  203. ];
  204. return [
  205. 'optionsArray' => [
  206. 'options' => $options,
  207. 'uri' => 'mongodb://localhost',
  208. ],
  209. 'queryString' => [
  210. 'options' => [],
  211. 'uri' => 'mongodb://localhost/?' . self::makeOptionString($options),
  212. ],
  213. 'overridden' => [
  214. 'options' => $options,
  215. 'uri' => 'mongodb://localhost/?' . self::makeOptionString($overriddenOptions),
  216. ]
  217. ];
  218. }
  219. public function testConnectWithUsernameAndPassword()
  220. {
  221. $this->expectException(\MongoConnectionException::class);
  222. $this->expectExceptionMessage('Authentication failed');
  223. $client = $this->getClient(['username' => 'alcaeus', 'password' => 'mySuperSecurePassword']);
  224. $collection = $client->selectCollection('test', 'foo');
  225. $document = ['foo' => 'bar'];
  226. $collection->insert($document);
  227. }
  228. public function testConnectWithUsernameAndPasswordInConnectionUrl()
  229. {
  230. $this->expectException(\MongoConnectionException::class);
  231. $this->expectExceptionMessage('Authentication failed');
  232. $client = $this->getClient([], 'mongodb://alcaeus:mySuperSecurePassword@localhost');
  233. $collection = $client->selectCollection('test', 'foo');
  234. $document = ['foo' => 'bar'];
  235. $collection->insert($document);
  236. }
  237. public function testConnectionUriOptionIntegerTypeCasting()
  238. {
  239. $client = new \MongoClient('mongodb://localhost/db?w=0&wtimeout=0', ['connect' => false]);
  240. $this->assertSame(['w' => 0, 'wtimeout' => 0], $client->getWriteConcern());
  241. }
  242. /**
  243. * @param array $options
  244. * @return string
  245. */
  246. private static function makeOptionString(array $options)
  247. {
  248. return implode('&', array_map(
  249. function ($key, $value) {
  250. return $key . '=' . $value;
  251. },
  252. array_keys($options),
  253. array_values($options)
  254. ));
  255. }
  256. }