MongoClientTest.php 7.4 KB

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