Skip to content

Development php8.1 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 13, 2024
3 changes: 3 additions & 0 deletions .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
push:
branches:
- "*.x"
schedule:
# Run workflow on every Sunday
- cron: '25 5 * * 0'

jobs:
coding-standards:
Expand Down
13 changes: 8 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ on:
push:
branches:
- "*.x"
schedule:
# Run workflow on every Sunday
- cron: '25 5 * * 0'

jobs:
verification:
name: "Verification tests"
runs-on: "ubuntu-18.04"
runs-on: "ubuntu-20.04"

steps:
- name: "Checkout"
Expand Down Expand Up @@ -78,7 +81,7 @@ jobs:
fail-fast: true
matrix:
os:
- "ubuntu-18.04"
- "ubuntu-20.04"
php-version:
- "7.2"
- "7.3"
Expand All @@ -93,17 +96,17 @@ jobs:
- "normal"
include:
- deps: "low"
os: "ubuntu-18.04"
os: "ubuntu-20.04"
php-version: "5.6"
mongodb-version: "3.0"
driver-version: "1.2.0"
- deps: "normal"
os: "ubuntu-18.04"
os: "ubuntu-20.04"
php-version: "7.0"
mongodb-version: "4.4"
driver-version: "1.9.2"
- deps: "normal"
os: "ubuntu-18.04"
os: "ubuntu-20.04"
php-version: "7.1"
mongodb-version: "4.4"
driver-version: "1.11.1"
Expand Down
4 changes: 3 additions & 1 deletion lib/Alcaeus/MongoDbAdapter/TypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ private static function convertBSONObjectToLegacy(BSON\Type $value)
case $value instanceof Model\BSONDocument:
case $value instanceof Model\BSONArray:
return array_map(
['self', 'toLegacy'],
function ($value) {
return self::toLegacy($value);
},
$value->getArrayCopy()
);
default:
Expand Down
2 changes: 1 addition & 1 deletion lib/Mongo/MongoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function __construct($server = 'default', array $options = ['connect' =>
if (false === strpos($this->server, '://')) {
$this->server = 'mongodb://' . $this->server;
}
$this->client = new Client($this->server, $options, $driverOptions);
$this->client = new Client($this->server, $options, $driverOptions + ['driver' => ['name' => 'mongo-php-adapter']]);
$info = $this->client->__debugInfo();
$this->manager = $info['manager'];

Expand Down
21 changes: 19 additions & 2 deletions lib/Mongo/MongoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Alcaeus\MongoDbAdapter\TypeConverter;
use Alcaeus\MongoDbAdapter\ExceptionConverter;
use MongoDB\Driver\Exception\CommandException;
use MongoDB\Model\IndexInput;

/**
* Represents a database collection.
Expand Down Expand Up @@ -597,7 +598,7 @@ public function createIndex($keys, array $options = [])
}

if (! isset($options['name'])) {
$options['name'] = \MongoDB\generate_index_name($keys);
$options['name'] = $this->generateIndexName($keys);
}

$indexes = iterator_to_array($this->collection->listIndexes());
Expand Down Expand Up @@ -677,7 +678,7 @@ public function deleteIndex($keys)
$indexName .= '_1';
}
} elseif (is_array($keys)) {
$indexName = \MongoDB\generate_index_name($keys);
$indexName = $this->generateIndexName($keys);
} else {
throw new \InvalidArgumentException();
}
Expand Down Expand Up @@ -1039,6 +1040,22 @@ private function checkCollectionName($name)
}
}


/**
* @param array $keys Field or fields to use as index.
* @return string
*/
private function generateIndexName($keys)
{
$name = '';

foreach ($keys as $field => $type) {
$name .= ($name !== '' ? '_' : '') . $field . '_' . $type;
}

return $name;
}

/**
* @return array
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/Mongo/MongoCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public function immortal($liveForever = true)
public function limit($num)
{
$this->errorIfOpened();
$this->limit = intval($num);
$this->limit = $num;

return $this;
}
Expand Down Expand Up @@ -356,7 +356,7 @@ public function setFlag($flag, $set = true)
public function skip($num)
{
$this->errorIfOpened();
$this->skip = intval($num);
$this->skip = $num;

return $this;
}
Expand Down
22 changes: 22 additions & 0 deletions lib/Mongo/MongoId.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ public function unserialize($serialized)
$this->createObjectID($serialized);
}

/**
* @return array
*/
public function __serialize()
{
return [
'objectID' => (string) $this->objectID
];
}

/**
* @param array $data
* @return void
* @throws MongoException
*/
public function __unserialize(array $data)
{
if (isset($data['objectID'])) {
$this->createObjectID($data['objectID']);
}
}

/**
* Gets the incremented value to create this id
* @link http://php.net/manual/en/mongoid.getinc.php
Expand Down
3 changes: 3 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Constraint/Matches.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class Matches extends Constraint
/** @var ComparisonFailure|null */
private $lastFailure;

/** @var Factory */
private $comparatorFactory;

public function __construct($value, $allowExtraRootKeys = true, $allowExtraKeys = false, $allowOperators = true)
{
$this->value = self::prepare($value);
Expand Down
21 changes: 15 additions & 6 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ public function provideConnectionUri()
yield ['default', sprintf('mongodb://%s:%d', \MongoClient::DEFAULT_HOST, \MongoClient::DEFAULT_PORT)];
yield ['localhost', 'mongodb://localhost'];
yield ['mongodb://localhost', 'mongodb://localhost'];
if (version_compare(phpversion('mongodb'), '1.4.0', '>=')) {
yield ['mongodb+srv://foo.example.com', 'mongodb+srv://foo.example.com'];
}
}

public function testSerialize()
Expand Down Expand Up @@ -92,11 +89,23 @@ public function testGetHosts()

public function testGetHostsExceptionHandling()
{
// Workaround for PHPUnit 10
set_error_handler(
static function ($errno, $errstr) {
throw new \Exception($errstr, $errno);
},
E_ALL
);

$this->expectException(\MongoConnectionException::class);
$this->expectErrorMessageMatches('/fake_host/');
$this->expectExceptionMessageMatches('/fake_host/');

$client = $this->getClient(null, 'mongodb://fake_host');
$client->getHosts();
try {
$client = $this->getClient(null, 'mongodb://fake_host');
$client->getHosts();
} finally {
restore_error_handler();
}
}

public function testReadPreference()
Expand Down
25 changes: 16 additions & 9 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use MongoDB\BSON\Regex;
use MongoDB\Driver\ReadPreference;
use Alcaeus\MongoDbAdapter\Tests\TestCase;
use MongoId;
use PHPUnit\Framework\Error\Warning;
use function extension_loaded;
use function strcasecmp;

Expand Down Expand Up @@ -56,12 +54,21 @@ public function testCreateRecord()

public function testInsertInvalidData()
{
// Workaround for PHPUnit 10
set_error_handler(static function ($errno, $errstr) {
throw new \Exception($errstr, $errno);
}, E_ALL);

// Dirty hack to support both PHPUnit 5.x and 6.x
$this->expectWarning();
$this->expectWarningMessage('MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
$this->expectException(\Exception::class);
$this->expectExceptionMessage('MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');

$document = 8;
$this->getCollection()->insert($document);
try {
$document = 8;
$this->getCollection()->insert($document);
} finally {
restore_error_handler();
}
}

public function testInsertEmptyArray()
Expand Down Expand Up @@ -195,7 +202,7 @@ public function testInsertDuplicate()
unset($document['_id']);

$this->expectException(\MongoDuplicateKeyException::class);
$this->expectErrorMessageMatches('/E11000 duplicate key error .* mongo-php-adapter\.test/');
$this->expectExceptionMessageMatches('/E11000 duplicate key error .* mongo-php-adapter\.test/');
$this->expectExceptionCode(11000);
$collection->insert($document);
}
Expand Down Expand Up @@ -303,7 +310,7 @@ public function testBatchInsertException()
$documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];

$this->expectException(\MongoDuplicateKeyException::class);
$this->expectErrorMessageMatches('/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
$this->expectExceptionMessageMatches('/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
$this->expectExceptionCode(11000);

$this->getCollection()->batchInsert($documents);
Expand Down Expand Up @@ -415,7 +422,7 @@ public function testUpdateReplaceOne()
public function testUpdateReplaceMultiple()
{
$this->expectException(\MongoWriteConcernException::class);
$this->expectErrorMessageMatches('/multi update only works with \$ operators/', 9);
$this->expectExceptionMessageMatches('/multi update only works with \$ operators/', 9);
$this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo'], ['multiple' => true]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function testDeleteManyWithoutAck()
];

$this->assertSame($expected, $batch->execute(['w' => 0]));
usleep(250000); // 250 ms

$newCollection = $this->getCheckDatabase()->selectCollection('test');
$this->assertSame(0, $newCollection->count());
Expand Down
10 changes: 5 additions & 5 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public function testStoreByteExceptionWhileInsertingRecord()
$collection->insert($document);

$this->expectException(\MongoGridFSException::class);
$this->expectErrorMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
$this->expectExceptionMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
$this->expectExceptionCode(11000);

$collection->storeBytes('foo', ['_id' => $id]);
Expand All @@ -342,7 +342,7 @@ public function testStoreByteExceptionWhileInsertingChunks()
$collection->chunks->insert($document);

$this->expectException(\MongoGridFSException::class);
$this->expectErrorMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
$this->expectExceptionMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
$this->expectExceptionCode(11000);

$collection->storeBytes('foo');
Expand All @@ -357,7 +357,7 @@ public function testStoreFileExceptionWhileInsertingRecord()
$collection->insert($document);

$this->expectException(\MongoGridFSException::class);
$this->expectErrorMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
$this->expectExceptionMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
$this->expectExceptionCode(11000);

$collection->storeFile(__FILE__, ['_id' => $id]);
Expand All @@ -372,7 +372,7 @@ public function testStoreFileExceptionWhileInsertingChunks()
$collection->chunks->insert($document);

$this->expectException(\MongoGridFSException::class);
$this->expectErrorMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
$this->expectExceptionMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
$this->expectExceptionCode(11000);

$collection->storeFile(__FILE__);
Expand All @@ -387,7 +387,7 @@ public function testStoreFileExceptionWhileUpdatingFileRecord()
$collection->insert($document);

$this->expectException(\MongoGridFSException::class);
$this->expectErrorMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
$this->expectExceptionMessageMatches('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
$this->expectExceptionCode(11000);

$collection->storeFile(fopen(__FILE__, 'r'));
Expand Down
9 changes: 8 additions & 1 deletion tests/Alcaeus/MongoDbAdapter/Mongo/MongoIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ public function testCreateWithoutParameter()
$this->assertSame($stringId, $id->{'$id'});

$serialized = serialize($id);
$this->assertSame(sprintf('C:7:"MongoId":24:{%s}', $stringId), $serialized);

if (PHP_VERSION_ID >= 70400) {
$serializedStr = 'O:7:"MongoId":1:{s:8:"objectID";s:24:"%s";}';
} else {
$serializedStr = 'C:7:"MongoId":24:{%s}';
}

$this->assertSame(sprintf($serializedStr, $stringId), $serialized);

$unserialized = unserialize($serialized);
$this->assertInstanceOf('MongoId', $unserialized);
Expand Down
Loading