Skip to content

Commit

Permalink
Merge pull request #164 from leroy-merlin-br/feat/upgrade-mongo-driver
Browse files Browse the repository at this point in the history
Feat(MongoDB): Upgrade Driver
  • Loading branch information
diegofelix committed Feb 15, 2022
2 parents 62492ff + 951afb0 commit 50c637d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/.coverage
/build
/site
.idea
coverage
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"require": {
"php": ">=7.1",
"mongodb/mongodb": "^1.3",
"mongodb/mongodb": "1.8.0",
"illuminate/container": "^5.8 || ^6.0 || ^7.0 || ^8.0"
},
"require-dev": {
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ services:
- .:/var/www/html

db:
image: mongo:4.0
command: mongod --smallfiles
image: mongo:4.2
command: mongod
volumes:
- db:/data/db
- .:/var/www/html
Expand Down
11 changes: 8 additions & 3 deletions src/Mongolid/Util/SequenceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@ public function getNextValue(string $sequenceName): int
['upsert' => true]
);

if ($sequenceValue) {
$_id = $sequenceValue->seq + 1;
if (!$sequenceValue) {
return 1;
}

return $_id ?? 1;
// On MongoDB 4.0 or below, this value will be an
// object. From 4.1 it is returned as an array.
// so we cast it to object to make sure.
$sequenceValue = (object) $sequenceValue;

return $sequenceValue->seq + 1;
}

/**
Expand Down
19 changes: 10 additions & 9 deletions tests/Mongolid/Util/SequenceServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public function testShouldGetNextValue($sequenceName, $currentValue, $expectatio
['_id' => $sequenceName],
['$inc' => ['seq' => 1]],
['upsert' => true]
)->andReturn(
$currentValue ? (object) ['seq' => $currentValue] : null
);
)->andReturn($currentValue);

// Assertion
$this->assertEquals(
Expand Down Expand Up @@ -80,19 +78,22 @@ public function sequenceScenarios()
return [
'New sequence in collection "products"' => [
'sequenceName' => 'products',
'currentValue' => 0,
'currentValue' => null,
'expectation' => 1,
],
// -----------------------
'Existing sequence in collection "unicorns"' => [
'sequenceName' => 'unicorns',
'currentValue' => 7,
'currentValue' => (object) ['seq' => 7],
'expectation' => 8,
],
// -----------------------
'Existing sequence in collection "unicorns"' => [
'Existing one more sequence in collection "unicorns"' => [
'sequenceName' => 'unicorns',
'currentValue' => (object) ['seq' => 3],
'expectation' => 4,
],
'Returned as an array instead of object' => [
'sequenceName' => 'unicorns',
'currentValue' => 3,
'currentValue' => ['seq' => 3],
'expectation' => 4,
],
];
Expand Down

0 comments on commit 50c637d

Please sign in to comment.