Skip to content

Commit dd6f667

Browse files
authored
Merge pull request #2097 from jenssegers/l7
Laravel 7 support
2 parents 32ff280 + 0804ab2 commit dd6f667

File tree

10 files changed

+64
-73
lines changed

10 files changed

+64
-73
lines changed

.github/workflows/build-ci.yml

+1-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
matrix:
1414
php: ['7.2', '7.3', '7.4']
1515
os: ['ubuntu-latest']
16-
mongodb: ['3.6', '4.0', '4.2']
16+
mongodb: ['3.6', '4.0', '4.2', '4.4']
1717
services:
1818
mongo:
1919
image: mongo:${{ matrix.mongodb }}
@@ -63,10 +63,6 @@ jobs:
6363
MONGO_HOST: 0.0.0.0
6464
MYSQL_HOST: 0.0.0.0
6565
MYSQL_PORT: 3307
66-
- name: Send coveralls
67-
run: vendor/bin/coveralls coverage.xml
68-
env:
69-
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7066
- uses: codecov/codecov-action@v1
7167
with:
7268
token: ${{ secrets.CODECOV_TOKEN }}

CHANGELOG.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
# Changelog
2-
All notable changes to this project will be documented in this file.
3-
4-
## [Unreleased]
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
## [Unreleased]
5+
6+
### Added
7+
- Laravel 7 support by [@divine](https://github.com/divine).
8+
9+
### Changed
10+
- Updated versions of all dependencies by [@divine](https://github.com/divine).
11+
12+
### Removed
13+
- shouldUseCollections function by [@divine](https://github.com/divine).

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Make sure you have the MongoDB PHP driver installed. You can find installation i
6767
5.7.x | 3.4.x
6868
5.8.x | 3.5.x
6969
6.x | 3.6.x
70+
7.x | 3.7.x
7071

7172
Install the package via Composer:
7273

composer.json

+9-11
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@
1919
],
2020
"license": "MIT",
2121
"require": {
22-
"illuminate/support": "^5.8|^6.0",
23-
"illuminate/container": "^5.8|^6.0",
24-
"illuminate/database": "^5.8|^6.0",
25-
"illuminate/events": "^5.8|^6.0",
26-
"mongodb/mongodb": "^1.4"
22+
"illuminate/support": "^7.0",
23+
"illuminate/container": "^7.0",
24+
"illuminate/database": "^7.0",
25+
"illuminate/events": "^7.0",
26+
"mongodb/mongodb": "^1.6"
2727
},
2828
"require-dev": {
29-
"phpunit/phpunit": "^6.0|^7.0|^8.0",
30-
"orchestra/testbench": "^3.1|^4.0",
31-
"mockery/mockery": "^1.0",
32-
"doctrine/dbal": "^2.5",
33-
"phpunit/phpcov": "^6.0",
34-
"cedx/coveralls": "^11.2"
29+
"phpunit/phpunit": "^8.4|^9.0",
30+
"orchestra/testbench": "^5.0",
31+
"mockery/mockery": "^1.3.1",
32+
"doctrine/dbal": "^2.6"
3533
},
3634
"autoload": {
3735
"psr-0": {

phpunit.xml.dist

-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
<testsuite name="seeder">
1919
<file>tests/SeederTest.php</file>
2020
</testsuite>
21-
<testsuite name="cache">
22-
<file>tests/CacheTest.php</file>
23-
</testsuite>
2421
<testsuite name="builder">
2522
<file>tests/QueryBuilderTest.php</file>
2623
<file>tests/QueryTest.php</file>

src/Jenssegers/Mongodb/Eloquent/Model.php

+11-10
Original file line numberDiff line numberDiff line change
@@ -234,36 +234,37 @@ public function getCasts()
234234
/**
235235
* @inheritdoc
236236
*/
237-
public function originalIsEquivalent($key, $current)
237+
public function originalIsEquivalent($key)
238238
{
239239
if (!array_key_exists($key, $this->original)) {
240240
return false;
241241
}
242242

243-
$original = $this->getOriginal($key);
243+
$attribute = Arr::get($this->attributes, $key);
244+
$original = Arr::get($this->original, $key);
244245

245-
if ($current === $original) {
246+
if ($attribute === $original) {
246247
return true;
247248
}
248249

249-
if (null === $current) {
250+
if (null === $attribute) {
250251
return false;
251252
}
252253

253254
if ($this->isDateAttribute($key)) {
254-
$current = $current instanceof UTCDateTime ? $this->asDateTime($current) : $current;
255+
$attribute = $attribute instanceof UTCDateTime ? $this->asDateTime($attribute) : $attribute;
255256
$original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original;
256257

257-
return $current == $original;
258+
return $attribute == $original;
258259
}
259260

260-
if ($this->hasCast($key)) {
261-
return $this->castAttribute($key, $current) ===
261+
if ($this->hasCast($key, static::$primitiveCastTypes)) {
262+
return $this->castAttribute($key, $attribute) ===
262263
$this->castAttribute($key, $original);
263264
}
264265

265-
return is_numeric($current) && is_numeric($original)
266-
&& strcmp((string) $current, (string) $original) === 0;
266+
return is_numeric($attribute) && is_numeric($original)
267+
&& strcmp((string) $attribute, (string) $original) === 0;
267268
}
268269

269270
/**

src/Jenssegers/Mongodb/Query/Builder.php

+5-27
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,6 @@ class Builder extends BaseBuilder
120120
'>=' => '$gte',
121121
];
122122

123-
/**
124-
* Check if we need to return Collections instead of plain arrays (laravel >= 5.3 )
125-
* @var boolean
126-
*/
127-
protected $useCollections;
128-
129123
/**
130124
* @inheritdoc
131125
*/
@@ -134,22 +128,6 @@ public function __construct(Connection $connection, Processor $processor)
134128
$this->grammar = new Grammar;
135129
$this->connection = $connection;
136130
$this->processor = $processor;
137-
$this->useCollections = $this->shouldUseCollections();
138-
}
139-
140-
/**
141-
* Returns true if Laravel or Lumen >= 5.3
142-
* @return bool
143-
*/
144-
protected function shouldUseCollections()
145-
{
146-
if (function_exists('app')) {
147-
$version = app()->version();
148-
$version = filter_var(explode(')', $version)[0], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); // lumen
149-
return version_compare($version, '5.3', '>=');
150-
}
151-
152-
return true;
153131
}
154132

155133
/**
@@ -303,7 +281,7 @@ public function getFresh($columns = [], $returnLazy = false)
303281
'aggregate' => $totalResults
304282
]
305283
];
306-
return $this->useCollections ? new Collection($results) : $results;
284+
return new Collection($results);
307285
} elseif ($function == 'count') {
308286
// Translate count into sum.
309287
$group['aggregate'] = ['$sum' => 1];
@@ -360,7 +338,7 @@ public function getFresh($columns = [], $returnLazy = false)
360338
$results = iterator_to_array($this->collection->aggregate($pipeline, $options));
361339

362340
// Return results
363-
return $this->useCollections ? new Collection($results) : $results;
341+
return new Collection($results);
364342
} // Distinct query
365343
elseif ($this->distinct) {
366344
// Return distinct results directly
@@ -373,7 +351,7 @@ public function getFresh($columns = [], $returnLazy = false)
373351
$result = $this->collection->distinct($column);
374352
}
375353

376-
return $this->useCollections ? new Collection($result) : $result;
354+
return new Collection($result);
377355
} // Normal query
378356
else {
379357
$columns = [];
@@ -430,7 +408,7 @@ public function getFresh($columns = [], $returnLazy = false)
430408

431409
// Return results as an array with numeric keys
432410
$results = iterator_to_array($cursor, false);
433-
return $this->useCollections ? new Collection($results) : $results;
411+
return new Collection($results);
434412
}
435413
}
436414

@@ -685,7 +663,7 @@ public function pluck($column, $key = null)
685663
}
686664

687665
$p = Arr::pluck($results, $column, $key);
688-
return $this->useCollections ? new Collection($p) : $p;
666+
return new Collection($p);
689667
}
690668

691669
/**

tests/ModelTest.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,8 @@ public function testDates(): void
410410

411411
// test created_at
412412
$item = Item::create(['name' => 'sword']);
413-
$this->assertInstanceOf(UTCDateTime::class, $item->getOriginal('created_at'));
414-
$this->assertEquals($item->getOriginal('created_at')
413+
$this->assertInstanceOf(UTCDateTime::class, $item->getRawOriginal('created_at'));
414+
$this->assertEquals($item->getRawOriginal('created_at')
415415
->toDateTime()
416416
->getTimestamp(), $item->created_at->getTimestamp());
417417
$this->assertLessThan(2, abs(time() - $item->created_at->getTimestamp()));
@@ -420,7 +420,7 @@ public function testDates(): void
420420
/** @var Item $item */
421421
$item = Item::create(['name' => 'sword']);
422422
$json = $item->toArray();
423-
$this->assertEquals($item->created_at->format('Y-m-d H:i:s'), $json['created_at']);
423+
$this->assertEquals($item->created_at->toISOString(), $json['created_at']);
424424

425425
/** @var User $user */
426426
//Test with create and standard property
@@ -430,10 +430,10 @@ public function testDates(): void
430430
$user = User::create(['name' => 'Jane Doe', 'birthday' => Date::now()]);
431431
$this->assertInstanceOf(Carbon::class, $user->birthday);
432432

433-
$user = User::create(['name' => 'Jane Doe', 'birthday' => 'Monday 8th of August 2005 03:12:46 PM']);
433+
$user = User::create(['name' => 'Jane Doe', 'birthday' => 'Monday 8th August 2005 03:12:46 PM']);
434434
$this->assertInstanceOf(Carbon::class, $user->birthday);
435435

436-
$user = User::create(['name' => 'Jane Doe', 'birthday' => 'Monday 8th of August 1960 03:12:46 PM']);
436+
$user = User::create(['name' => 'Jane Doe', 'birthday' => 'Monday 8th August 1960 03:12:46 PM']);
437437
$this->assertInstanceOf(Carbon::class, $user->birthday);
438438

439439
$user = User::create(['name' => 'Jane Doe', 'birthday' => '2005-08-08']);
@@ -467,10 +467,10 @@ public function testDates(): void
467467
$user->setAttribute('birthday', Date::now());
468468
$this->assertInstanceOf(Carbon::class, $user->birthday);
469469

470-
$user->setAttribute('birthday', 'Monday 8th of August 2005 03:12:46 PM');
470+
$user->setAttribute('birthday', 'Monday 8th August 2005 03:12:46 PM');
471471
$this->assertInstanceOf(Carbon::class, $user->birthday);
472472

473-
$user->setAttribute('birthday', 'Monday 8th of August 1960 03:12:46 PM');
473+
$user->setAttribute('birthday', 'Monday 8th August 1960 03:12:46 PM');
474474
$this->assertInstanceOf(Carbon::class, $user->birthday);
475475

476476
$user->setAttribute('birthday', '2005-08-08');
@@ -504,10 +504,10 @@ public function testDates(): void
504504
$user = User::create(['name' => 'Jane Doe', 'entry' => ['date' => Date::now()]]);
505505
$this->assertInstanceOf(Carbon::class, $user->getAttribute('entry.date'));
506506

507-
$user = User::create(['name' => 'Jane Doe', 'entry' => ['date' => 'Monday 8th of August 2005 03:12:46 PM']]);
507+
$user = User::create(['name' => 'Jane Doe', 'entry' => ['date' => 'Monday 8th August 2005 03:12:46 PM']]);
508508
$this->assertInstanceOf(Carbon::class, $user->getAttribute('entry.date'));
509509

510-
$user = User::create(['name' => 'Jane Doe', 'entry' => ['date' => 'Monday 8th of August 1960 03:12:46 PM']]);
510+
$user = User::create(['name' => 'Jane Doe', 'entry' => ['date' => 'Monday 8th August 1960 03:12:46 PM']]);
511511
$this->assertInstanceOf(Carbon::class, $user->getAttribute('entry.date'));
512512

513513
$user = User::create(['name' => 'Jane Doe', 'entry' => ['date' => '2005-08-08']]);
@@ -541,10 +541,10 @@ public function testDates(): void
541541
$user->setAttribute('entry.date', Date::now());
542542
$this->assertInstanceOf(Carbon::class, $user->getAttribute('entry.date'));
543543

544-
$user->setAttribute('entry.date', 'Monday 8th of August 2005 03:12:46 PM');
544+
$user->setAttribute('entry.date', 'Monday 8th August 2005 03:12:46 PM');
545545
$this->assertInstanceOf(Carbon::class, $user->getAttribute('entry.date'));
546546

547-
$user->setAttribute('entry.date', 'Monday 8th of August 1960 03:12:46 PM');
547+
$user->setAttribute('entry.date', 'Monday 8th August 1960 03:12:46 PM');
548548
$this->assertInstanceOf(Carbon::class, $user->getAttribute('entry.date'));
549549

550550
$user->setAttribute('entry.date', '2005-08-08');

tests/QueueTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
declare(strict_types=1);
33

44
use Carbon\Carbon;
5+
use Illuminate\Support\Str;
56
use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider;
67

78
class QueueTest extends TestCase
@@ -17,6 +18,12 @@ public function setUp(): void
1718

1819
public function testQueueJobLifeCycle(): void
1920
{
21+
$uuid = Str::uuid();
22+
23+
Str::createUuidsUsing(function () use ($uuid) {
24+
return $uuid;
25+
});
26+
2027
$id = Queue::push('test', ['action' => 'QueueJobLifeCycle'], 'test');
2128
$this->assertNotNull($id);
2229

@@ -25,9 +32,11 @@ public function testQueueJobLifeCycle(): void
2532
$this->assertInstanceOf(Jenssegers\Mongodb\Queue\MongoJob::class, $job);
2633
$this->assertEquals(1, $job->isReserved());
2734
$this->assertEquals(json_encode([
35+
'uuid' => $uuid,
2836
'displayName' => 'test',
2937
'job' => 'test',
3038
'maxTries' => null,
39+
'maxExceptions' => null,
3140
'delay' => null,
3241
'timeout' => null,
3342
'data' => ['action' => 'QueueJobLifeCycle'],
@@ -36,6 +45,8 @@ public function testQueueJobLifeCycle(): void
3645
// Remove reserved job
3746
$job->delete();
3847
$this->assertEquals(0, Queue::getDatabase()->table(Config::get('queue.connections.database.table'))->count());
48+
49+
Str::createUuidsNormally();
3950
}
4051

4152
public function testQueueJobExpired(): void

tests/models/User.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public function father()
7777
return $this->embedsOne('User');
7878
}
7979

80-
public function getDateFormat()
80+
protected function serializeDate(DateTimeInterface $date)
8181
{
82-
return 'l jS \of F Y h:i:s A';
82+
return $date->format('l jS \of F Y h:i:s A');
8383
}
8484
}

0 commit comments

Comments
 (0)