Skip to content

Commit

Permalink
Merge pull request #2097 from jenssegers/l7
Browse files Browse the repository at this point in the history
Laravel 7 support
  • Loading branch information
Smolevich authored Sep 14, 2020
2 parents 32ff280 + 0804ab2 commit dd6f667
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 73 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/build-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
matrix:
php: ['7.2', '7.3', '7.4']
os: ['ubuntu-latest']
mongodb: ['3.6', '4.0', '4.2']
mongodb: ['3.6', '4.0', '4.2', '4.4']
services:
mongo:
image: mongo:${{ matrix.mongodb }}
Expand Down Expand Up @@ -63,10 +63,6 @@ jobs:
MONGO_HOST: 0.0.0.0
MYSQL_HOST: 0.0.0.0
MYSQL_PORT: 3307
- name: Send coveralls
run: vendor/bin/coveralls coverage.xml
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
Expand Down
17 changes: 13 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# Changelog
All notable changes to this project will be documented in this file.

## [Unreleased]
# Changelog
All notable changes to this project will be documented in this file.

## [Unreleased]

### Added
- Laravel 7 support by [@divine](https://github.com/divine).

### Changed
- Updated versions of all dependencies by [@divine](https://github.com/divine).

### Removed
- shouldUseCollections function by [@divine](https://github.com/divine).
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Make sure you have the MongoDB PHP driver installed. You can find installation i
5.7.x | 3.4.x
5.8.x | 3.5.x
6.x | 3.6.x
7.x | 3.7.x

Install the package via Composer:

Expand Down
20 changes: 9 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@
],
"license": "MIT",
"require": {
"illuminate/support": "^5.8|^6.0",
"illuminate/container": "^5.8|^6.0",
"illuminate/database": "^5.8|^6.0",
"illuminate/events": "^5.8|^6.0",
"mongodb/mongodb": "^1.4"
"illuminate/support": "^7.0",
"illuminate/container": "^7.0",
"illuminate/database": "^7.0",
"illuminate/events": "^7.0",
"mongodb/mongodb": "^1.6"
},
"require-dev": {
"phpunit/phpunit": "^6.0|^7.0|^8.0",
"orchestra/testbench": "^3.1|^4.0",
"mockery/mockery": "^1.0",
"doctrine/dbal": "^2.5",
"phpunit/phpcov": "^6.0",
"cedx/coveralls": "^11.2"
"phpunit/phpunit": "^8.4|^9.0",
"orchestra/testbench": "^5.0",
"mockery/mockery": "^1.3.1",
"doctrine/dbal": "^2.6"
},
"autoload": {
"psr-0": {
Expand Down
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
<testsuite name="seeder">
<file>tests/SeederTest.php</file>
</testsuite>
<testsuite name="cache">
<file>tests/CacheTest.php</file>
</testsuite>
<testsuite name="builder">
<file>tests/QueryBuilderTest.php</file>
<file>tests/QueryTest.php</file>
Expand Down
21 changes: 11 additions & 10 deletions src/Jenssegers/Mongodb/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,36 +234,37 @@ public function getCasts()
/**
* @inheritdoc
*/
public function originalIsEquivalent($key, $current)
public function originalIsEquivalent($key)
{
if (!array_key_exists($key, $this->original)) {
return false;
}

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

if ($current === $original) {
if ($attribute === $original) {
return true;
}

if (null === $current) {
if (null === $attribute) {
return false;
}

if ($this->isDateAttribute($key)) {
$current = $current instanceof UTCDateTime ? $this->asDateTime($current) : $current;
$attribute = $attribute instanceof UTCDateTime ? $this->asDateTime($attribute) : $attribute;
$original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original;

return $current == $original;
return $attribute == $original;
}

if ($this->hasCast($key)) {
return $this->castAttribute($key, $current) ===
if ($this->hasCast($key, static::$primitiveCastTypes)) {
return $this->castAttribute($key, $attribute) ===
$this->castAttribute($key, $original);
}

return is_numeric($current) && is_numeric($original)
&& strcmp((string) $current, (string) $original) === 0;
return is_numeric($attribute) && is_numeric($original)
&& strcmp((string) $attribute, (string) $original) === 0;
}

/**
Expand Down
32 changes: 5 additions & 27 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ class Builder extends BaseBuilder
'>=' => '$gte',
];

/**
* Check if we need to return Collections instead of plain arrays (laravel >= 5.3 )
* @var boolean
*/
protected $useCollections;

/**
* @inheritdoc
*/
Expand All @@ -134,22 +128,6 @@ public function __construct(Connection $connection, Processor $processor)
$this->grammar = new Grammar;
$this->connection = $connection;
$this->processor = $processor;
$this->useCollections = $this->shouldUseCollections();
}

/**
* Returns true if Laravel or Lumen >= 5.3
* @return bool
*/
protected function shouldUseCollections()
{
if (function_exists('app')) {
$version = app()->version();
$version = filter_var(explode(')', $version)[0], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); // lumen
return version_compare($version, '5.3', '>=');
}

return true;
}

/**
Expand Down Expand Up @@ -303,7 +281,7 @@ public function getFresh($columns = [], $returnLazy = false)
'aggregate' => $totalResults
]
];
return $this->useCollections ? new Collection($results) : $results;
return new Collection($results);
} elseif ($function == 'count') {
// Translate count into sum.
$group['aggregate'] = ['$sum' => 1];
Expand Down Expand Up @@ -360,7 +338,7 @@ public function getFresh($columns = [], $returnLazy = false)
$results = iterator_to_array($this->collection->aggregate($pipeline, $options));

// Return results
return $this->useCollections ? new Collection($results) : $results;
return new Collection($results);
} // Distinct query
elseif ($this->distinct) {
// Return distinct results directly
Expand All @@ -373,7 +351,7 @@ public function getFresh($columns = [], $returnLazy = false)
$result = $this->collection->distinct($column);
}

return $this->useCollections ? new Collection($result) : $result;
return new Collection($result);
} // Normal query
else {
$columns = [];
Expand Down Expand Up @@ -430,7 +408,7 @@ public function getFresh($columns = [], $returnLazy = false)

// Return results as an array with numeric keys
$results = iterator_to_array($cursor, false);
return $this->useCollections ? new Collection($results) : $results;
return new Collection($results);
}
}

Expand Down Expand Up @@ -685,7 +663,7 @@ public function pluck($column, $key = null)
}

$p = Arr::pluck($results, $column, $key);
return $this->useCollections ? new Collection($p) : $p;
return new Collection($p);
}

/**
Expand Down
22 changes: 11 additions & 11 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ public function testDates(): void

// test created_at
$item = Item::create(['name' => 'sword']);
$this->assertInstanceOf(UTCDateTime::class, $item->getOriginal('created_at'));
$this->assertEquals($item->getOriginal('created_at')
$this->assertInstanceOf(UTCDateTime::class, $item->getRawOriginal('created_at'));
$this->assertEquals($item->getRawOriginal('created_at')
->toDateTime()
->getTimestamp(), $item->created_at->getTimestamp());
$this->assertLessThan(2, abs(time() - $item->created_at->getTimestamp()));
Expand All @@ -420,7 +420,7 @@ public function testDates(): void
/** @var Item $item */
$item = Item::create(['name' => 'sword']);
$json = $item->toArray();
$this->assertEquals($item->created_at->format('Y-m-d H:i:s'), $json['created_at']);
$this->assertEquals($item->created_at->toISOString(), $json['created_at']);

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

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

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

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

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

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

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

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

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

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

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

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

$user->setAttribute('entry.date', '2005-08-08');
Expand Down
11 changes: 11 additions & 0 deletions tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
declare(strict_types=1);

use Carbon\Carbon;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider;

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

public function testQueueJobLifeCycle(): void
{
$uuid = Str::uuid();

Str::createUuidsUsing(function () use ($uuid) {
return $uuid;
});

$id = Queue::push('test', ['action' => 'QueueJobLifeCycle'], 'test');
$this->assertNotNull($id);

Expand All @@ -25,9 +32,11 @@ public function testQueueJobLifeCycle(): void
$this->assertInstanceOf(Jenssegers\Mongodb\Queue\MongoJob::class, $job);
$this->assertEquals(1, $job->isReserved());
$this->assertEquals(json_encode([
'uuid' => $uuid,
'displayName' => 'test',
'job' => 'test',
'maxTries' => null,
'maxExceptions' => null,
'delay' => null,
'timeout' => null,
'data' => ['action' => 'QueueJobLifeCycle'],
Expand All @@ -36,6 +45,8 @@ public function testQueueJobLifeCycle(): void
// Remove reserved job
$job->delete();
$this->assertEquals(0, Queue::getDatabase()->table(Config::get('queue.connections.database.table'))->count());

Str::createUuidsNormally();
}

public function testQueueJobExpired(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public function father()
return $this->embedsOne('User');
}

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

0 comments on commit dd6f667

Please sign in to comment.