Skip to content

PHPORM-150 Run CI on Laravel 11 #2735

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 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .github/workflows/build-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ jobs:
- php: "8.1"
mongodb: "5.0"
mode: "low-deps"

# Laravel 11
- php: "8.3"
mongodb: "7.0"
mode: "dev"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once Laravel 11 will be released, this will no longer be necessary. But only low-deps job will run Laravel 10.

steps:
- uses: "actions/checkout@v4"

Expand Down Expand Up @@ -70,8 +73,10 @@ jobs:
restore-keys: "${{ matrix.os }}-composer-"

- name: "Install dependencies"
run: composer update --no-interaction $([[ "${{ matrix.mode }}" == low-deps ]] && echo ' --prefer-lowest --prefer-stable')

run: |
composer update --no-interaction \
$([[ "${{ matrix.mode }}" == low-deps ]] && echo ' --prefer-lowest') \
$([[ "${{ matrix.mode }}" != dev ]] && echo ' --prefer-stable')
- name: "Run tests"
run: "./vendor/bin/phpunit --coverage-clover coverage.xml"
env:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.

## [unreleased]

* Add support for Laravel 11 by @GromNaN in [#2735](https://github.com/mongodb/laravel-mongodb/pull/2735)
* Fix `Query\Builder::dump` and `dd` methods to dump the MongoDB query by @GromNaN in [#2727](https://github.com/mongodb/laravel-mongodb/pull/2727) and [#2730](https://github.com/mongodb/laravel-mongodb/pull/2730)

## [4.1.2]
Expand Down
14 changes: 6 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@
"require": {
"php": "^8.1",
"ext-mongodb": "^1.15",
"illuminate/support": "^10.0",
"illuminate/container": "^10.0",
"illuminate/database": "^10.30",
"illuminate/events": "^10.0",
"illuminate/support": "^10.0|^11",
"illuminate/container": "^10.0|^11",
"illuminate/database": "^10.30|^11",
"illuminate/events": "^10.0|^11",
"mongodb/mongodb": "^1.15"
},
"require-dev": {
"phpunit/phpunit": "^10.3",
"orchestra/testbench": "^8.0",
"orchestra/testbench": "^8.0|^9.0",
"mockery/mockery": "^1.4.4",
"doctrine/coding-standard": "12.0.x-dev",
"spatie/laravel-query-builder": "^5.6",
"phpstan/phpstan": "^1.10"
},
"minimum-stability": "dev",
"replace": {
"jenssegers/mongodb": "self.version"
},
Expand Down Expand Up @@ -66,9 +67,6 @@
"cs:fix": "phpcbf"
},
"config": {
"platform": {
"php": "8.1"
},
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
Expand Down
23 changes: 22 additions & 1 deletion src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MongoDB\Laravel\Eloquent;

use BackedEnum;
use Carbon\CarbonInterface;
use DateTimeInterface;
use Illuminate\Contracts\Queue\QueueableCollection;
Expand All @@ -22,6 +23,7 @@
use MongoDB\BSON\UTCDateTime;
use MongoDB\Laravel\Query\Builder as QueryBuilder;
use Stringable;
use ValueError;

use function array_key_exists;
use function array_keys;
Expand All @@ -38,10 +40,12 @@
use function is_string;
use function ltrim;
use function method_exists;
use function sprintf;
use function str_contains;
use function str_starts_with;
use function strcmp;
use function uniqid;
use function var_export;

abstract class Model extends BaseModel
{
Expand Down Expand Up @@ -704,7 +708,7 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt
}

if ($this->isEnumCastable($key) && (! $castValue instanceof Arrayable)) {
$castValue = $castValue !== null ? $this->getStorableEnumValue($castValue) : null;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature of this method have changed in a non-backward compatible way. So I duplicated this simple method to keep compatibility with both Laravel 10 and 11.
laravel/framework@8647dcf#diff-59d24f1a8f0dd1e51ee7dffc8778133711634e928ecef4a91b63fc3851cb1579R1188

We must keep the function addCastAttributesToArray introduced for Laravel 10 compatibility, as it provides casting of nested fields. 2ea1a7c#diff-284e164f76aeb13f424d154f9208d91fdbb0112b0a50ad8b1dfa237176f03cf2R525-R530

$castValue = $castValue !== null ? $this->getStorableEnumValueFromLaravel11($this->getCasts()[$key], $castValue) : null;
}

if ($castValue instanceof Arrayable) {
Expand All @@ -717,6 +721,23 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt
return $attributes;
}

/**
* Duplicate of {@see HasAttributes::getStorableEnumValue()} for Laravel 11 as the signature of the method has
* changed in a non-backward compatible way.
*
* @todo Remove this method when support for Laravel 10 is dropped.
*/
private function getStorableEnumValueFromLaravel11($expectedEnum, $value)
{
if (! $value instanceof $expectedEnum) {
throw new ValueError(sprintf('Value [%s] is not of the expected enum type [%s].', var_export($value, true), $expectedEnum));
}

return $value instanceof BackedEnum
? $value->value
: $value->name;
}

/**
* Is a value a BSON type?
*
Expand Down
6 changes: 1 addition & 5 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,7 @@ public function orderBy($column, $direction = 'asc')
return $this;
}

/**
* @param list{mixed, mixed}|CarbonPeriod $values
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This signature was causing psalm error. Param type incompatible with the parent method.

*
* @inheritdoc
*/
/** @inheritdoc */
public function whereBetween($column, iterable $values, $boolean = 'and', $not = false)
{
$type = 'between';
Expand Down