Kaynağa Gözat

Allow tests to run with ext-mongo installed

Andreas Braun 10 yıl önce
ebeveyn
işleme
5d4085c05d

+ 1 - 1
lib/Alcaeus/MongoDbAdapter/AbstractCursor.php

@@ -327,7 +327,7 @@ abstract class AbstractCursor
                 'id' => (string) $this->cursor->getId(),
                 'at' => null, // @todo Complete info for cursor that is iterating
                 'numReturned' => null, // @todo Complete info for cursor that is iterating
-                'server' => null, // @todo Complete info for cursor that is iterating
+                'server' => sprintf('%s:%d;-;.;%d', $this->cursor->getServer()->getHost(), $this->cursor->getServer()->getPort(), getmypid()),
                 'host' => $this->cursor->getServer()->getHost(),
                 'port' => $this->cursor->getServer()->getPort(),
                 'connection_type_desc' => $typeString,

+ 1 - 1
lib/Mongo/MongoClient.php

@@ -195,7 +195,7 @@ class MongoClient
         }
 
         foreach ($servers as $server) {
-            $key = sprintf('%s:%d', $server->getHost(), $server->getPort());
+            $key = sprintf('%s:%d;-;.;%d', $server->getHost(), $server->getPort(), getmypid());
             $info = $server->getInfo();
 
             switch ($server->getType()) {

+ 22 - 18
lib/Mongo/functions.php

@@ -15,24 +15,28 @@
 
 use Alcaeus\MongoDbAdapter\TypeConverter;
 
-/**
- * Deserializes a BSON object into a PHP array
- *
- * @param string $bson The BSON to be deserialized.
- * @return array Returns the deserialized BSON object.
- */
-function bson_decode($bson)
-{
-    return TypeConverter::toLegacy(\MongoDB\BSON\toPHP($bson));
+if (! function_exists('bson_decode')) {
+    /**
+     * Deserializes a BSON object into a PHP array
+     *
+     * @param string $bson The BSON to be deserialized.
+     * @return array Returns the deserialized BSON object.
+     */
+    function bson_decode($bson)
+    {
+        return TypeConverter::toLegacy(\MongoDB\BSON\toPHP($bson));
+    }
 }
 
-/**
- * Serializes a PHP variable into a BSON string
- *
- * @param mixed $anything The variable to be serialized.
- * @return string Returns the serialized string.
- */
-function bson_encode($anything)
-{
-    return \MongoDB\BSON\fromPHP(TypeConverter::fromLegacy($anything));
+if (! function_exists('bson_encode')) {
+    /**
+     * Serializes a PHP variable into a BSON string
+     *
+     * @param mixed $anything The variable to be serialized.
+     * @return string Returns the serialized string.
+     */
+    function bson_encode($anything)
+    {
+        return \MongoDB\BSON\fromPHP(TypeConverter::fromLegacy($anything));
+    }
 }

+ 14 - 1
tests/Alcaeus/MongoDbAdapter/MongoBinDataTest.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace Alcaeus\MongoDbAdapter\Tests;
+use Alcaeus\MongoDbAdapter\TypeInterface;
 
 /**
  * @author alcaeus <alcaeus@alcaeus.org>
@@ -13,7 +14,17 @@ class MongoBinDataTest extends TestCase
         $this->assertAttributeSame('foo', 'bin', $bin);
         $this->assertAttributeSame(\MongoBinData::FUNC, 'type', $bin);
 
-        $this->assertSame('<Mongo Binary Data>', (string) $bin);
+        $this->assertSame('<Mongo Binary Data>', (string)$bin);
+
+        return $bin;
+    }
+
+    /**
+     * @depends testCreate
+     */
+    public function testConvertToBson(\MongoBinData $bin)
+    {
+        $this->skipTestUnless($bin instanceof TypeInterface);
 
         $bsonBinary = $bin->toBSONType();
         $this->assertInstanceOf('MongoDB\BSON\Binary', $bsonBinary);
@@ -24,6 +35,8 @@ class MongoBinDataTest extends TestCase
 
     public function testCreateWithBsonBinary()
     {
+        $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoBinData')));
+
         $bsonBinary = new \MongoDB\BSON\Binary('foo', \MongoDB\BSON\Binary::TYPE_UUID);
         $bin = new \MongoBinData($bsonBinary);
 

+ 3 - 17
tests/Alcaeus/MongoDbAdapter/MongoClientTest.php

@@ -7,21 +7,6 @@ namespace Alcaeus\MongoDbAdapter\Tests;
  */
 class MongoClientTest extends TestCase
 {
-    public function testConnectAndDisconnect()
-    {
-        $client = $this->getClient();
-        $this->assertTrue($client->connected);
-
-        $client->close();
-        $this->assertFalse($client->connected);
-    }
-
-    public function testClientWithoutAutomaticConnect()
-    {
-        $client = $this->getClient([]);
-        $this->assertFalse($client->connected);
-    }
-
     public function testGetDb()
     {
         $client = $this->getClient();
@@ -63,16 +48,17 @@ class MongoClientTest extends TestCase
     public function testGetHosts()
     {
         $client = $this->getClient();
+        $hosts = $client->getHosts();
         $this->assertArraySubset(
             [
-                'localhost:27017' => [
+                'localhost:27017;-;.;' . getmypid() => [
                     'host' => 'localhost',
                     'port' => 27017,
                     'health' => 1,
                     'state' => 0,
                 ],
             ],
-            $client->getHosts()
+            $hosts
         );
     }
 

+ 14 - 1
tests/Alcaeus/MongoDbAdapter/MongoCodeTest.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace Alcaeus\MongoDbAdapter\Tests;
+use Alcaeus\MongoDbAdapter\TypeInterface;
 
 /**
  * @author alcaeus <alcaeus@alcaeus.org>
@@ -13,7 +14,17 @@ class MongoCodeTest extends TestCase
         $this->assertAttributeSame('code', 'code', $code);
         $this->assertAttributeSame(['scope' => 'bleh'], 'scope', $code);
 
-        $this->assertSame('code', (string) $code);
+        $this->assertSame('code', (string)$code);
+
+        return $code;
+    }
+
+    /**
+     * @depends testCreate
+     */
+    public function testConvertToBson(\MongoCode $code)
+    {
+        $this->skipTestUnless($code instanceof TypeInterface);
 
         $bsonCode = $code->toBSONType();
         $this->assertInstanceOf('MongoDB\BSON\Javascript', $bsonCode);
@@ -21,6 +32,8 @@ class MongoCodeTest extends TestCase
 
     public function testCreateWithBsonObject()
     {
+        $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoCode')));
+
         $bsonCode = new \MongoDB\BSON\Javascript('code', ['scope' => 'bleh']);
         $code = new \MongoCode($bsonCode);
 

+ 15 - 0
tests/Alcaeus/MongoDbAdapter/MongoDateTest.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace Alcaeus\MongoDbAdapter\Tests;
+use Alcaeus\MongoDbAdapter\TypeInterface;
 
 /**
  * @author alcaeus <alcaeus@alcaeus.org>
@@ -19,6 +20,18 @@ class MongoDateTest extends TestCase
         $this->assertSame(1234567890, $dateTime->getTimestamp());
         $this->assertSame('123000', $dateTime->format('u'));
 
+        return $date;
+    }
+
+    /**
+     * @depends testCreate
+     */
+    public function testConvertToBson(\MongoDate $date)
+    {
+        $this->skipTestUnless($date instanceof TypeInterface);
+
+        $dateTime = $date->toDateTime();
+
         $bsonDate = $date->toBSONType();
         $this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $bsonDate);
         $this->assertSame('1234567890123', (string) $bsonDate);
@@ -28,6 +41,8 @@ class MongoDateTest extends TestCase
 
     public function testCreateWithBsonDate()
     {
+        $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoDate')));
+
         $bsonDate = new \MongoDB\BSON\UTCDateTime(1234567890123);
         $date = new \MongoDate($bsonDate);
 

+ 4 - 2
tests/Alcaeus/MongoDbAdapter/MongoMaxKeyTest.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace Alcaeus\MongoDbAdapter\Tests;
+use Alcaeus\MongoDbAdapter\TypeInterface;
 
 /**
  * @author alcaeus <alcaeus@alcaeus.org>
@@ -9,7 +10,8 @@ class MongoMaxKeyTest extends TestCase
 {
     public function testConvert()
     {
-        $MaxKey = new \MongoMaxKey();
-        $this->assertInstanceOf('MongoDB\BSON\MaxKey', $MaxKey->toBSONType());
+        $maxKey = new \MongoMaxKey();
+        $this->skipTestUnless($maxKey instanceof TypeInterface);
+        $this->assertInstanceOf('MongoDB\BSON\MaxKey', $maxKey->toBSONType());
     }
 }

+ 2 - 0
tests/Alcaeus/MongoDbAdapter/MongoMinKeyTest.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace Alcaeus\MongoDbAdapter\Tests;
+use Alcaeus\MongoDbAdapter\TypeInterface;
 
 /**
  * @author alcaeus <alcaeus@alcaeus.org>
@@ -10,6 +11,7 @@ class MongoMinKeyTest extends TestCase
     public function testConvert()
     {
         $minKey = new \MongoMinKey();
+        $this->skipTestUnless($minKey instanceof TypeInterface);
         $this->assertInstanceOf('MongoDB\BSON\MinKey', $minKey->toBSONType());
     }
 }

+ 13 - 0
tests/Alcaeus/MongoDbAdapter/MongoRegexTest.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace Alcaeus\MongoDbAdapter\Tests;
+use Alcaeus\MongoDbAdapter\TypeInterface;
 
 /**
  * @author alcaeus <alcaeus@alcaeus.org>
@@ -15,6 +16,16 @@ class MongoRegexTest extends TestCase
 
         $this->assertSame('/abc/i', (string) $regex);
 
+        return $regex;
+    }
+
+    /**
+     * @depends testCreate
+     */
+    public function testConvertToBson(\MongoRegex $regex)
+    {
+        $this->skipTestUnless($regex instanceof TypeInterface);
+
         $bsonRegex = $regex->toBSONType();
         $this->assertInstanceOf('MongoDB\BSON\Regex', $bsonRegex);
         $this->assertSame('abc', $bsonRegex->getPattern());
@@ -23,6 +34,8 @@ class MongoRegexTest extends TestCase
 
     public function testCreateWithBsonType()
     {
+        $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoRegex')));
+
         $bsonRegex = new \MongoDB\BSON\Regex('abc', 'i');
         $regex = new \MongoRegex($bsonRegex);
 

+ 13 - 0
tests/Alcaeus/MongoDbAdapter/MongoTimestampTest.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace Alcaeus\MongoDbAdapter\Tests;
+use Alcaeus\MongoDbAdapter\TypeInterface;
 
 /**
  * @author alcaeus <alcaeus@alcaeus.org>
@@ -15,6 +16,16 @@ class MongoTimestampTest extends TestCase
 
         $this->assertSame('1234567890', (string) $timestamp);
 
+        return $timestamp;
+    }
+
+    /**
+     * @depends testCreate
+     */
+    public function testConvertToBson(\MongoTimestamp $timestamp)
+    {
+        $this->skipTestUnless($timestamp instanceof TypeInterface);
+
         $bsonTimestamp = $timestamp->toBSONType();
         $this->assertInstanceOf('MongoDB\BSON\Timestamp', $bsonTimestamp);
         $this->assertSame('[1234567890:987654321]', (string) $bsonTimestamp);
@@ -31,6 +42,8 @@ class MongoTimestampTest extends TestCase
 
     public function testCreateWithBsonTimestamp()
     {
+        $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoTimestamp')));
+
         $bsonTimestamp = new \MongoDB\BSON\Timestamp(1234567890, 987654321);
         $timestamp = new \MongoTimestamp($bsonTimestamp);
 

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

@@ -145,4 +145,14 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
     {
         return $this->configureFailPoint("maxTimeAlwaysTimeOut", array("times" => 1));
     }
+
+    /**
+     * @param $condition
+     */
+    protected function skipTestUnless($condition)
+    {
+        if (!$condition) {
+            $this->markTestSkipped('Test only applies when running against mongo-php-adapter');
+        }
+    }
 }