瀏覽代碼

Extract methods to abstract testcase

Andreas Braun 10 年之前
父節點
當前提交
9676848b27

+ 0 - 16
tests/Alcaeus/MongoDbAdapter/MongoClientTest.php

@@ -79,20 +79,4 @@ class MongoClientTest extends TestCase
         $this->assertTrue($client->setWriteConcern('majority', 100));
         $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $client->getWriteConcern());
     }
-
-    /**
-     * @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);
-    }
 }

+ 0 - 18
tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php

@@ -482,24 +482,6 @@ class MongoCollectionTest extends TestCase
     /**
      * @return \MongoCollection
      */
-    protected function getCollection($name = 'test')
-    {
-        return $this->getDatabase()->selectCollection($name);
-    }
-
-    /**
-     * @return \MongoDB
-     */
-    protected function getDatabase()
-    {
-        $client = new \MongoClient();
-
-        return $client->selectDB('mongo-php-adapter');
-    }
-
-    /**
-     * @return \MongoCollection
-     */
     protected function prepareData()
     {
         $collection = $this->getCollection();

+ 0 - 11
tests/Alcaeus/MongoDbAdapter/MongoCommandCursorTest.php

@@ -52,17 +52,6 @@ class MongoCommandCursorTest extends TestCase
     }
 
     /**
-     * @param string $name
-     * @return \MongoCollection
-     */
-    protected function getCollection($name = 'test')
-    {
-        $client = new \MongoClient();
-
-        return $client->selectCollection('mongo-php-adapter', $name);
-    }
-
-    /**
      * @return \MongoCollection
      */
     protected function prepareData()

+ 0 - 11
tests/Alcaeus/MongoDbAdapter/MongoCursorTest.php

@@ -248,17 +248,6 @@ class MongoCursorTest extends TestCase
     }
 
     /**
-     * @param string $name
-     * @return \MongoCollection
-     */
-    protected function getCollection($name = 'test')
-    {
-        $client = new \MongoClient();
-
-        return $client->selectCollection('mongo-php-adapter', $name);
-    }
-
-    /**
      * @return \PHPUnit_Framework_MockObject_MockObject
      */
     protected function getCollectionMock()

+ 3 - 6
tests/Alcaeus/MongoDbAdapter/MongoDBRefTest.php

@@ -49,8 +49,7 @@ class MongoDBRefTest extends TestCase
     {
         $id = new \MongoId();
 
-        $client = new \MongoClient();
-        $db = $client->selectDB('mongo-php-adapter');
+        $db = $this->getDatabase();
 
         $document = ['_id' => $id, 'foo' => 'bar'];
         $db->selectCollection('test')->insert($document);
@@ -62,16 +61,14 @@ class MongoDBRefTest extends TestCase
 
     public function testGetWithNonExistingDocument()
     {
-        $client = new \MongoClient();
-        $db = $client->selectDB('mongo-php-adapter');
+        $db = $this->getDatabase();
 
         $this->assertNull(\MongoDBRef::get($db, ['$ref' => 'test', '$id' => 'foo']));
     }
 
     public function testGetWithInvalidRef()
     {
-        $client = new \MongoClient();
-        $db = $client->selectDB('mongo-php-adapter');
+        $db = $this->getDatabase();
 
         $this->assertNull(\MongoDBRef::get($db, []));
     }

+ 0 - 18
tests/Alcaeus/MongoDbAdapter/MongoDBTest.php

@@ -115,22 +115,4 @@ class MongoDBTest extends TestCase
         $this->assertSame(\MongoDB::PROFILING_SLOW, $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_ON));
         $this->assertSame(\MongoDB::PROFILING_ON, $this->getDatabase()->getProfilingLevel());
     }
-
-    /**
-     * @return \MongoDB
-     */
-    protected function getDatabase()
-    {
-        $client = $this->getClient();
-
-        return $client->selectDB('mongo-php-adapter');
-    }
-
-    /**
-     * @return \MongoClient
-     */
-    protected function getClient()
-    {
-        return new \MongoClient();
-    }
 }

+ 43 - 0
tests/Alcaeus/MongoDbAdapter/TestCase.php

@@ -19,4 +19,47 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
         $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);
+    }
 }