| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace Alcaeus\MongoDbAdapter\Tests;
- use MongoDB\Client;
- abstract class TestCase extends \PHPUnit_Framework_TestCase
- {
- protected function tearDown()
- {
- $this->getCheckDatabase()->drop();
- }
- /**
- * @return \MongoDB\Database
- */
- protected function getCheckDatabase()
- {
- $client = new Client('mongodb://localhost', ['connect' => true]);
- return $client->selectDatabase('mongo-php-adapter');
- }
- /**
- * @param array|null $options
- * @return \MongoClient
- */
- protected function getClient($options = null)
- {
- $args = ['mongodb://localhost'];
- if ($options !== null) {
- $args[] = $options;
- }
- $reflection = new \ReflectionClass('MongoClient');
- return $reflection->newInstanceArgs($args);
- }
- /**
- * @param \MongoClient|null $client
- * @return \MongoDB
- */
- protected function getDatabase(\MongoClient $client = null)
- {
- if ($client === null) {
- $client = $this->getClient();
- }
- return $client->selectDB('mongo-php-adapter');
- }
- /**
- * @param string $name
- * @param \MongoDB|null $database
- * @return \MongoCollection
- */
- protected function getCollection($name = 'test', \MongoDB $database = null)
- {
- if ($database === null) {
- $database = $this->getDatabase();
- }
- return $database->selectCollection($name);
- }
- /**
- * @param string $prefix
- * @param \MongoDB|null $database
- * @return \MongoGridFS
- */
- protected function getGridFS($prefix = 'fs', \MongoDB $database = null)
- {
- if ($database === null) {
- $database = $this->getDatabase();
- }
- return $database->getGridFS($prefix);
- }
- /**
- * @return \MongoCollection
- */
- protected function prepareData()
- {
- $collection = $this->getCollection();
- $document = ['foo' => 'bar'];
- $collection->insert($document);
- unset($document['_id']);
- $collection->insert($document);
- $document = ['foo' => 'foo'];
- $collection->insert($document);
- return $collection;
- }
- }
|