MongoClientTest.php 8.6 KB

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