MongoClientTest.php 9.4 KB

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