Skip to content

Commit 14218fa

Browse files
Merge pull request #9 from kaskus/development
Development php8.1 compatibility (#8)
2 parents e8f41a7 + 7bd4207 commit 14218fa

12 files changed

+89
-29
lines changed

lib/Alcaeus/MongoDbAdapter/TypeConverter.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ private static function convertBSONObjectToLegacy(BSON\Type $value)
178178
case $value instanceof Model\BSONDocument:
179179
case $value instanceof Model\BSONArray:
180180
return array_map(
181-
['self', 'toLegacy'],
181+
function ($value) {
182+
return self::toLegacy($value);
183+
},
182184
$value->getArrayCopy()
183185
);
184186
default:

lib/Mongo/MongoCollection.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -1047,9 +1047,13 @@ private function checkCollectionName($name)
10471047
*/
10481048
private function generateIndexName($keys)
10491049
{
1050-
$indexInput = new IndexInput(['key' => $keys]);
1050+
$name = '';
10511051

1052-
return (string) $indexInput;
1052+
foreach ($keys as $field => $type) {
1053+
$name .= ($name !== '' ? '_' : '') . $field . '_' . $type;
1054+
}
1055+
1056+
return $name;
10531057
}
10541058

10551059
/**

lib/Mongo/MongoCursor.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public function immortal($liveForever = true)
295295
public function limit($num)
296296
{
297297
$this->errorIfOpened();
298-
$this->limit = intval($num);
298+
$this->limit = $num;
299299

300300
return $this;
301301
}
@@ -356,7 +356,7 @@ public function setFlag($flag, $set = true)
356356
public function skip($num)
357357
{
358358
$this->errorIfOpened();
359-
$this->skip = intval($num);
359+
$this->skip = $num;
360360

361361
return $this;
362362
}

lib/Mongo/MongoId.php

+22
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,28 @@ public function unserialize($serialized)
142142
$this->createObjectID($serialized);
143143
}
144144

145+
/**
146+
* @return array
147+
*/
148+
public function __serialize()
149+
{
150+
return [
151+
'objectID' => (string) $this->objectID
152+
];
153+
}
154+
155+
/**
156+
* @param array $data
157+
* @return void
158+
* @throws MongoException
159+
*/
160+
public function __unserialize(array $data)
161+
{
162+
if (isset($data['objectID'])) {
163+
$this->createObjectID($data['objectID']);
164+
}
165+
}
166+
145167
/**
146168
* Gets the incremented value to create this id
147169
* @link http://php.net/manual/en/mongoid.getinc.php

tests/Alcaeus/MongoDbAdapter/Constraint/Matches.php

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class Matches extends Constraint
5151
/** @var ComparisonFailure|null */
5252
private $lastFailure;
5353

54+
/** @var Factory */
55+
private $comparatorFactory;
56+
5457
public function __construct($value, $allowExtraRootKeys = true, $allowExtraKeys = false, $allowOperators = true)
5558
{
5659
$this->value = self::prepare($value);

tests/Alcaeus/MongoDbAdapter/Mongo/MongoClientTest.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,23 @@ public function testGetHosts()
8989

9090
public function testGetHostsExceptionHandling()
9191
{
92+
// Workaround for PHPUnit 10
93+
set_error_handler(
94+
static function ($errno, $errstr) {
95+
throw new \Exception($errstr, $errno);
96+
},
97+
E_ALL
98+
);
99+
92100
$this->expectException(\MongoConnectionException::class);
93-
$this->expectErrorMessageMatches('/fake_host/');
101+
$this->expectExceptionMessageMatches('/fake_host/');
94102

95-
$client = $this->getClient(null, 'mongodb://fake_host');
96-
$client->getHosts();
103+
try {
104+
$client = $this->getClient(null, 'mongodb://fake_host');
105+
$client->getHosts();
106+
} finally {
107+
restore_error_handler();
108+
}
97109
}
98110

99111
public function testReadPreference()

tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

+16-9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use MongoDB\BSON\Regex;
77
use MongoDB\Driver\ReadPreference;
88
use Alcaeus\MongoDbAdapter\Tests\TestCase;
9-
use MongoId;
10-
use PHPUnit\Framework\Error\Warning;
119
use function extension_loaded;
1210
use function strcasecmp;
1311

@@ -56,12 +54,21 @@ public function testCreateRecord()
5654

5755
public function testInsertInvalidData()
5856
{
57+
// Workaround for PHPUnit 10
58+
set_error_handler(static function ($errno, $errstr) {
59+
throw new \Exception($errstr, $errno);
60+
}, E_ALL);
61+
5962
// Dirty hack to support both PHPUnit 5.x and 6.x
60-
$this->expectWarning();
61-
$this->expectWarningMessage('MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
63+
$this->expectException(\Exception::class);
64+
$this->expectExceptionMessage('MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
6265

63-
$document = 8;
64-
$this->getCollection()->insert($document);
66+
try {
67+
$document = 8;
68+
$this->getCollection()->insert($document);
69+
} finally {
70+
restore_error_handler();
71+
}
6572
}
6673

6774
public function testInsertEmptyArray()
@@ -195,7 +202,7 @@ public function testInsertDuplicate()
195202
unset($document['_id']);
196203

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

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

309316
$this->getCollection()->batchInsert($documents);
@@ -415,7 +422,7 @@ public function testUpdateReplaceOne()
415422
public function testUpdateReplaceMultiple()
416423
{
417424
$this->expectException(\MongoWriteConcernException::class);
418-
$this->expectErrorMessageMatches('/multi update only works with \$ operators/', 9);
425+
$this->expectExceptionMessageMatches('/multi update only works with \$ operators/', 9);
419426
$this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo'], ['multiple' => true]);
420427
}
421428

tests/Alcaeus/MongoDbAdapter/Mongo/MongoDeleteBatchTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function testDeleteManyWithoutAck()
7676
];
7777

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

8081
$newCollection = $this->getCheckDatabase()->selectCollection('test');
8182
$this->assertSame(0, $newCollection->count());

tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public function testStoreByteExceptionWhileInsertingRecord()
327327
$collection->insert($document);
328328

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

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

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

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

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

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

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

378378
$collection->storeFile(__FILE__);
@@ -387,7 +387,7 @@ public function testStoreFileExceptionWhileUpdatingFileRecord()
387387
$collection->insert($document);
388388

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

393393
$collection->storeFile(fopen(__FILE__, 'r'));

tests/Alcaeus/MongoDbAdapter/Mongo/MongoIdTest.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ public function testCreateWithoutParameter()
2020
$this->assertSame($stringId, $id->{'$id'});
2121

2222
$serialized = serialize($id);
23-
$this->assertSame(sprintf('C:7:"MongoId":24:{%s}', $stringId), $serialized);
23+
24+
if (PHP_VERSION_ID >= 70400) {
25+
$serializedStr = 'O:7:"MongoId":1:{s:8:"objectID";s:24:"%s";}';
26+
} else {
27+
$serializedStr = 'C:7:"MongoId":24:{%s}';
28+
}
29+
30+
$this->assertSame(sprintf($serializedStr, $stringId), $serialized);
2431

2532
$unserialized = unserialize($serialized);
2633
$this->assertInstanceOf('MongoId', $unserialized);

tests/Alcaeus/MongoDbAdapter/Mongo/MongoInsertBatchTest.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ class MongoInsertBatchTest extends TestCase
88
{
99
public function testSerialize()
1010
{
11-
$batch = new \MongoInsertBatch($this->getCollection());
11+
$batch = new \MongoInsertBatch($this->getCollection('MongoInsertBatchTest_testSerialize'));
1212
$this->assertIsString(serialize($batch));
1313
}
1414

1515
public function testInsertBatch()
1616
{
17-
$batch = new \MongoInsertBatch($this->getCollection());
17+
$batch = new \MongoInsertBatch($this->getCollection('MongoInsertBatchTest_testInsertBatch'));
1818

1919
$this->assertTrue($batch->add(['foo' => 'bar']));
2020
$this->assertTrue($batch->add(['bar' => 'foo']));
@@ -26,7 +26,7 @@ public function testInsertBatch()
2626

2727
$this->assertSame($expected, $batch->execute());
2828

29-
$newCollection = $this->getCheckDatabase()->selectCollection('test');
29+
$newCollection = $this->getCheckDatabase()->selectCollection('MongoInsertBatchTest_testInsertBatch');
3030
$this->assertSame(2, $newCollection->count());
3131
$record = $newCollection->findOne();
3232
$this->assertNotNull($record);
@@ -35,7 +35,7 @@ public function testInsertBatch()
3535

3636
public function testInsertBatchWithoutAck()
3737
{
38-
$batch = new \MongoInsertBatch($this->getCollection());
38+
$batch = new \MongoInsertBatch($this->getCollection('MongoInsertBatchTest_testInsertBatchWithoutAck'));
3939

4040
$this->assertTrue($batch->add(['foo' => 'bar']));
4141
$this->assertTrue($batch->add(['bar' => 'foo']));
@@ -46,8 +46,9 @@ public function testInsertBatchWithoutAck()
4646
];
4747

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

50-
$newCollection = $this->getCheckDatabase()->selectCollection('test');
51+
$newCollection = $this->getCheckDatabase()->selectCollection('MongoInsertBatchTest_testInsertBatchWithoutAck');
5152
$this->assertSame(2, $newCollection->count());
5253
$record = $newCollection->findOne();
5354
$this->assertNotNull($record);
@@ -56,7 +57,7 @@ public function testInsertBatchWithoutAck()
5657

5758
public function testInsertBatchError()
5859
{
59-
$collection = $this->getCollection();
60+
$collection = $this->getCollection('MongoInsertBatchTest_testInsertBatchError');
6061
$batch = new \MongoInsertBatch($collection);
6162
$collection->createIndex(['foo' => 1], ['unique' => true]);
6263

tests/Alcaeus/MongoDbAdapter/Mongo/MongoUpdateBatchTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public function testUpdateManyWithoutAck()
122122
];
123123

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

126127
$newCollection = $this->getCheckDatabase()->selectCollection('test');
127128
$this->assertSame(2, $newCollection->count());

0 commit comments

Comments
 (0)