MongoClientTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 testReadPreference()
  63. {
  64. $client = $this->getClient();
  65. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $client->getReadPreference());
  66. $this->assertTrue($client->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  67. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $client->getReadPreference());
  68. }
  69. public function testWriteConcern()
  70. {
  71. $client = $this->getClient();
  72. $this->assertTrue($client->setWriteConcern('majority', 100));
  73. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $client->getWriteConcern());
  74. }
  75. public function testListDBs()
  76. {
  77. $document = ['foo' => 'bar'];
  78. $this->getCollection()->insert($document);
  79. $databases = $this->getClient()->listDBs();
  80. $this->assertSame(1.0, $databases['ok']);
  81. $this->assertArrayHasKey('totalSize', $databases);
  82. $this->assertArrayHasKey('databases', $databases);
  83. foreach ($databases['databases'] as $database) {
  84. $this->assertArrayHasKey('name', $database);
  85. $this->assertArrayHasKey('empty', $database);
  86. $this->assertArrayHasKey('sizeOnDisk', $database);
  87. if ($database['name'] == 'mongo-php-adapter') {
  88. $this->assertFalse($database['empty']);
  89. return;
  90. }
  91. }
  92. $this->fail('Could not find mongo-php-adapter database in list');
  93. }
  94. public function testNoPrefixUri()
  95. {
  96. $client = $this->getClient(null, 'localhost');
  97. $this->assertNotNull($client);
  98. }
  99. /**
  100. * @dataProvider dataReadPreferenceOptionsAreInherited
  101. */
  102. public function testReadPreferenceOptionsAreInherited($options, $uri, $expectedTagsets)
  103. {
  104. $client = $this->getClient($options, $uri);
  105. $collection = $client->selectCollection('test', 'foo');
  106. $this->assertSame(
  107. [
  108. 'type' => \MongoClient::RP_SECONDARY_PREFERRED,
  109. 'tagsets' => $expectedTagsets
  110. ],
  111. $collection->getReadPreference()
  112. );
  113. }
  114. public static function dataReadPreferenceOptionsAreInherited()
  115. {
  116. $options = [
  117. 'readPreference' => \MongoClient::RP_SECONDARY_PREFERRED,
  118. 'readPreferenceTags' => 'a:b',
  119. ];
  120. $overriddenOptions = [
  121. 'readPreference' => \MongoClient::RP_NEAREST,
  122. 'readPreferenceTags' => 'c:d',
  123. ];
  124. $multipleTagsets = [
  125. 'readPreference' => \MongoClient::RP_SECONDARY_PREFERRED,
  126. 'readPreferenceTags' => 'a:b,c:d',
  127. ];
  128. return [
  129. 'optionsArray' => [
  130. 'options' => $options,
  131. 'uri' => 'mongodb://localhost',
  132. 'expectedTagsets' => [['a' => 'b']],
  133. ],
  134. 'queryString' => [
  135. 'options' => [],
  136. 'uri' => 'mongodb://localhost/?' . self::makeOptionString($options),
  137. 'expectedTagsets' => [['a' => 'b']],
  138. ],
  139. 'multipleInQueryString' => [
  140. 'options' => [],
  141. 'uri' => 'mongodb://localhost/?' . self::makeOptionString($options) . '&readPreferenceTags=c:d',
  142. 'expectedTagsets' => [['a' => 'b'], ['c' => 'd']],
  143. ],
  144. 'overridden' => [
  145. 'options' => $options,
  146. 'uri' => 'mongodb://localhost/?' . self::makeOptionString($overriddenOptions),
  147. 'expectedTagsets' => [['c' => 'd'], ['a' => 'b']],
  148. ],
  149. 'multipleTagsetsOptions' => [
  150. 'options' => $multipleTagsets,
  151. 'uri' => 'mongodb://localhost',
  152. 'expectedTagsets' => [['a' => 'b', 'c' => 'd']],
  153. ],
  154. 'multipleTagsetsQueryString' => [
  155. 'options' => null,
  156. 'uri' => 'mongodb://localhost/?' . self::makeOptionString($multipleTagsets),
  157. 'expectedTagsets' => [['a' => 'b', 'c' => 'd']],
  158. ],
  159. ];
  160. }
  161. /**
  162. * @dataProvider dataWriteConcernOptionsAreInherited
  163. */
  164. public function testWriteConcernOptionsAreInherited($options, $uri)
  165. {
  166. $client = $this->getClient($options, $uri);
  167. $collection = $client->selectCollection('test', 'foo');
  168. $this->assertSame(['w' => 'majority', 'wtimeout' => 666], $collection->getWriteConcern());
  169. }
  170. public static function dataWriteConcernOptionsAreInherited()
  171. {
  172. $options = [
  173. 'w' => 'majority',
  174. 'wTimeoutMs' => 666,
  175. ];
  176. $overriddenOptions = [
  177. 'w' => '2',
  178. 'wTimeoutMs' => 333,
  179. ];
  180. return [
  181. 'optionsArray' => [
  182. 'options' => $options,
  183. 'uri' => 'mongodb://localhost',
  184. ],
  185. 'queryString' => [
  186. 'options' => [],
  187. 'uri' => 'mongodb://localhost/?' . self::makeOptionString($options),
  188. ],
  189. 'overridden' => [
  190. 'options' => $options,
  191. 'uri' => 'mongodb://localhost/?' . self::makeOptionString($overriddenOptions),
  192. ]
  193. ];
  194. }
  195. public function testConnectWithUsernameAndPassword()
  196. {
  197. $this->expectException(\MongoConnectionException::class);
  198. $this->expectExceptionMessage('Authentication failed');
  199. $client = $this->getClient(['username' => 'alcaeus', 'password' => 'mySuperSecurePassword']);
  200. $collection = $client->selectCollection('test', 'foo');
  201. $document = ['foo' => 'bar'];
  202. $collection->insert($document);
  203. }
  204. public function testConnectWithUsernameAndPasswordInConnectionUrl()
  205. {
  206. $this->expectException(\MongoConnectionException::class);
  207. $this->expectExceptionMessage('Authentication failed');
  208. $client = $this->getClient([], 'mongodb://alcaeus:mySuperSecurePassword@localhost');
  209. $collection = $client->selectCollection('test', 'foo');
  210. $document = ['foo' => 'bar'];
  211. $collection->insert($document);
  212. }
  213. /**
  214. * @param array $options
  215. * @return string
  216. */
  217. private static function makeOptionString(array $options)
  218. {
  219. return implode('&', array_map(
  220. function ($key, $value) {
  221. return $key . '=' . $value;
  222. },
  223. array_keys($options),
  224. array_values($options)
  225. ));
  226. }
  227. }