MongoCollectionTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. /**
  4. * @author alcaeus <alcaeus@alcaeus.org>
  5. */
  6. class MongoCollectionTest extends TestCase
  7. {
  8. public function testGetNestedCollections()
  9. {
  10. $collection = $this->getCollection()->foo->bar;
  11. $this->assertSame('mongo-php-adapter.test.foo.bar', (string) $collection);
  12. }
  13. public function testCreateRecord()
  14. {
  15. $id = '54203e08d51d4a1f868b456e';
  16. $collection = $this->getCollection();
  17. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  18. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  19. $this->assertSame(1, $newCollection->count());
  20. $object = $newCollection->findOne();
  21. $this->assertNotNull($object);
  22. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  23. $this->assertSame($id, (string) $object->_id);
  24. $this->assertObjectHasAttribute('foo', $object);
  25. $this->assertAttributeSame('bar', 'foo', $object);
  26. }
  27. public function testFindReturnsCursor()
  28. {
  29. $collection = $this->getCollection();
  30. $collection->insert(['sorter' => 1]);
  31. $this->assertInstanceOf('MongoCursor', $collection->find());
  32. }
  33. public function testCount()
  34. {
  35. $collection = $this->getCollection();
  36. $collection->insert(['foo' => 'bar']);
  37. $collection->insert(['foo' => 'foo']);
  38. $this->assertSame(2, $collection->count());
  39. $this->assertSame(1, $collection->count(['foo' => 'bar']));
  40. }
  41. /**
  42. * @return \MongoCollection
  43. */
  44. protected function getCollection($name = 'test')
  45. {
  46. $client = new \MongoClient();
  47. return $client->selectCollection('mongo-php-adapter', $name);
  48. }
  49. }