Unverified Commit 343e7bbf authored by Stephan de Souza's avatar Stephan de Souza Committed by GitHub

Merge branch 'master' into fix-hasmany

parents bf4fe793 20fd7b02
name: CI
on:
push:
branches:
tags:
pull_request:
jobs:
build:
runs-on: ${{matrix.os}}
strategy:
matrix:
php: ['7.1', '7.2', '7.3', '7.4']
os: ['ubuntu-latest']
mongodb: ['3.6', '4.0', '4.2']
services:
mongo:
image: mongo:${{ matrix.mongodb }}
ports:
- 27017:27017
mysql:
image: mysql:5.7
ports:
- 3307:3306
env:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
MYSQL_DATABASE: 'unittest'
MYSQL_ROOT_PASSWORD:
name: PHP v${{ matrix.php }} with Mongo v${{ matrix.mongodb }}
steps:
- uses: actions/checkout@v1
- name: Show PHP version
run: php${{ matrix.php }} -v && composer -V
- name: Show Docker version
run: if [[ "$DEBUG" == "true" ]]; then docker version && env; fi
env:
DEBUG: ${{secrets.DEBUG}}
- name: Download Composer cache dependencies from cache
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache Composer dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ matrix.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ matrix.os }}-composer-
- name: Install dependencies
run: |
composer install --no-interaction
- name: Run tests
run: |
./vendor/bin/phpunit --coverage-clover coverage.xml
env:
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 }}
fail_ci_if_error: false
preset: laravel
language: minimal
matrix:
include:
- name: "7.1"
env: PHP_VERSION=7.1
- name: "7.2"
env: PHP_VERSION=7.2
- name: "7.3"
env: PHP_VERSION=7.3
services:
- docker
cache:
directories:
- $HOME/.composer/cache
install:
- docker version
- sudo pip install docker-compose
- docker-compose version
- docker-compose build --build-arg PHP_VERSION=${PHP_VERSION}
- docker-compose run --rm tests composer install --no-interaction
script:
- docker-compose run --rm tests ./vendor/bin/phpunit --coverage-clover ./clover.xml
This diff is collapsed.
......@@ -23,14 +23,15 @@
"illuminate/container": "^5.8|^6.0",
"illuminate/database": "^5.8|^6.0",
"illuminate/events": "^5.8|^6.0",
"mongodb/mongodb": "^1.4"
"mongodb/mongodb": "^1.4",
"cedx/coveralls": "^11.2"
},
"require-dev": {
"phpunit/phpunit": "^6.0|^7.0|^8.0",
"orchestra/testbench": "^3.1|^4.0",
"mockery/mockery": "^1.0",
"satooshi/php-coveralls": "^2.0",
"doctrine/dbal": "^2.5"
"doctrine/dbal": "^2.5",
"phpunit/phpcov": "^6.0"
},
"autoload": {
"psr-0": {
......
......@@ -40,11 +40,17 @@
<file>tests/ValidationTest.php</file>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<php>
<env name="MONGO_HOST" value="mongodb"/>
<env name="MONGO_DATABASE" value="unittest"/>
<env name="MONGO_PORT" value="27017"/>
<env name="MYSQL_HOST" value="mysql"/>
<env name="MYSQL_PORT" value="3306"/>
<env name="MYSQL_DATABASE" value="unittest"/>
<env name="MYSQL_USERNAME" value="root"/>
<env name="QUEUE_CONNECTION" value="database"/>
......
......@@ -25,6 +25,23 @@ class DatabaseTokenRepository extends BaseDatabaseTokenRepository
* @inheritdoc
*/
protected function tokenExpired($createdAt)
{
$createdAt = $this->convertDateTime($createdAt);
return parent::tokenExpired($createdAt);
}
/**
* @inheritdoc
*/
protected function tokenRecentlyCreated($createdAt)
{
$createdAt = $this->convertDateTime($createdAt);
return parent::tokenRecentlyCreated($createdAt);
}
private function convertDateTime($createdAt)
{
// Convert UTCDateTime to a date string.
if ($createdAt instanceof UTCDateTime) {
......@@ -37,6 +54,6 @@ class DatabaseTokenRepository extends BaseDatabaseTokenRepository
$createdAt = $date->format('Y-m-d H:i:s');
}
return parent::tokenExpired($createdAt);
return $createdAt;
}
}
......@@ -2,7 +2,6 @@
namespace Jenssegers\Mongodb\Eloquent;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Helpers\EloquentBuilder;
......@@ -11,6 +10,7 @@ use Jenssegers\Mongodb\Relations\BelongsToMany;
use Jenssegers\Mongodb\Relations\HasMany;
use Jenssegers\Mongodb\Relations\HasOne;
use Jenssegers\Mongodb\Relations\MorphTo;
use Jenssegers\Mongodb\Relations\MorphMany;
trait HybridRelations
{
......@@ -184,7 +184,7 @@ trait HybridRelations
// there are multiple types in the morph and we can't use single queries.
if (($class = $this->$type) === null) {
return new MorphTo(
$this->newQuery(), $this, $id, null, $type, $name
$this->newQuery(), $this, $id, $ownerKey, $type, $name
);
}
......@@ -195,8 +195,10 @@ trait HybridRelations
$instance = new $class;
$ownerKey = $ownerKey ?? $instance->getKeyName();
return new MorphTo(
$instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
$instance->newQuery(), $this, $id, $ownerKey, $type, $name
);
}
......
......@@ -118,7 +118,7 @@ abstract class Model extends BaseModel
*/
public function freshTimestamp()
{
return new UTCDateTime(time() * 1000);
return new UTCDateTime(Carbon::now());
}
/**
......
......@@ -2,6 +2,7 @@
namespace Jenssegers\Mongodb;
use DB;
use Illuminate\Queue\QueueServiceProvider;
use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider;
......@@ -13,7 +14,7 @@ class MongodbQueueServiceProvider extends QueueServiceProvider
protected function registerFailedJobServices()
{
// Add compatible queue failer if mongodb is configured.
if (config('queue.failed.database') == 'mongodb') {
if (DB::connection(config('queue.failed.database'))->getDriverName() == 'mongodb') {
$this->app->singleton('queue.failer', function ($app) {
return new MongoFailedJobProvider($app['db'], config('queue.failed.database'), config('queue.failed.table'));
});
......
......@@ -232,7 +232,7 @@ class Builder extends BaseBuilder
$wheres = $this->compileWheres();
// Use MongoDB's aggregation framework when using grouping or aggregation functions.
if ($this->groups || $this->aggregate || $this->paginating) {
if ($this->groups || $this->aggregate) {
$group = [];
$unwinds = [];
......@@ -267,24 +267,34 @@ class Builder extends BaseBuilder
$column = implode('.', $splitColumns);
}
// Null coalense only > 7.2
$aggregations = blank($this->aggregate['columns']) ? [] : $this->aggregate['columns'];
if (in_array('*', $aggregations) && $function == 'count') {
// When ORM is paginating, count doesnt need a aggregation, just a cursor operation
// elseif added to use this only in pagination
// https://docs.mongodb.com/manual/reference/method/cursor.count/
// count method returns int
$totalResults = $this->collection->count($wheres);
// Preserving format expected by framework
$results = [
[
'_id' => null,
'aggregate' => $totalResults
]
];
return $this->useCollections ? new Collection($results) : $results;
} elseif ($function == 'count') {
// Translate count into sum.
if ($function == 'count') {
$group['aggregate'] = ['$sum' => 1];
} // Pass other functions directly.
else {
} else {
$group['aggregate'] = ['$' . $function => '$' . $column];
}
}
}
// When using pagination, we limit the number of returned columns
// by adding a projection.
if ($this->paginating) {
foreach ($this->columns as $column) {
$this->projections[$column] = 1;
}
}
// The _id field is mandatory when using grouping.
if ($group && empty($group['_id'])) {
$group['_id'] = null;
......
......@@ -46,6 +46,10 @@ class MongoFailedJobProvider extends DatabaseFailedJobProvider
{
$job = $this->getTable()->find($id);
if (!$job) {
return;
}
$job['id'] = (string) $job['_id'];
return (object) $job;
......
......@@ -64,7 +64,7 @@ class MongoQueue extends DatabaseQueue
$job = $this->database->getCollection($this->table)->findOneAndUpdate(
[
'queue' => $this->getQueue($queue),
'reserved' => 0,
'reserved' => ['$ne' => 1],
'available_at' => ['$lte' => Carbon::now()->getTimestamp()],
],
[
......@@ -72,6 +72,9 @@ class MongoQueue extends DatabaseQueue
'reserved' => 1,
'reserved_at' => Carbon::now()->getTimestamp(),
],
'$inc' => [
'attempts' => 1,
],
],
[
'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
......@@ -94,24 +97,15 @@ class MongoQueue extends DatabaseQueue
protected function releaseJobsThatHaveBeenReservedTooLong($queue)
{
$expiration = Carbon::now()->subSeconds($this->retryAfter)->getTimestamp();
$now = time();
$reserved = $this->database->collection($this->table)
->where('queue', $this->getQueue($queue))
->where(function ($query) use ($expiration, $now) {
// Check for available jobs
$query->where(function ($query) use ($now) {
$query->whereNull('reserved_at');
$query->where('available_at', '<=', $now);
});
// Check for jobs that are reserved but have expired
$query->orWhere('reserved_at', '<=', $expiration);
})->get();
->whereNotNull('reserved_at')
->where('reserved_at', '<=', $expiration)
->get();
foreach ($reserved as $job) {
$attempts = $job['attempts'] + 1;
$this->releaseJob($job['_id'], $attempts);
$this->releaseJob($job['_id'], $job['attempts']);
}
}
......
<?php
namespace Jenssegers\Mongodb\Relations;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Eloquent\Relations\MorphMany as EloquentMorphMany;
class MorphMany extends EloquentMorphMany
{
/**
* Get the name of the "where in" method for eager loading.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
*
* @return string
*/
protected function whereInMethod(EloquentModel $model, $key)
{
return 'whereIn';
}
}
......@@ -74,6 +74,54 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
* @inheritdoc
*/
public function dropIndex($indexOrColumns = null)
{
$indexOrColumns = $this->transformColumns($indexOrColumns);
$this->collection->dropIndex($indexOrColumns);
return $this;
}
/**
* Indicate that the given index should be dropped, but do not fail if it didn't exist.
*
* @param string|array $indexOrColumns
* @return Blueprint
*/
public function dropIndexIfExists($indexOrColumns = null)
{
if ($this->hasIndex($indexOrColumns)) {
$this->dropIndex($indexOrColumns);
}
return $this;
}
/**
* Check whether the given index exists.
*
* @param string|array $indexOrColumns
* @return bool
*/
public function hasIndex($indexOrColumns = null)
{
$indexOrColumns = $this->transformColumns($indexOrColumns);
foreach ($this->collection->listIndexes() as $index) {
if (is_array($indexOrColumns) && in_array($index->getName(), $indexOrColumns)) {
return true;
}
if (is_string($indexOrColumns) && $index->getName() == $indexOrColumns) {
return true;
}
}
return false;
}
/**
* @param string|array $indexOrColumns
* @return string
*/
protected function transformColumns($indexOrColumns)
{
if (is_array($indexOrColumns)) {
$indexOrColumns = $this->fluent($indexOrColumns);
......@@ -85,12 +133,9 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
$transform[$column] = $column . '_1';
}
$indexOrColumns = join('_', $transform);
$indexOrColumns = implode('_', $transform);
}
$this->collection->dropIndex($indexOrColumns);
return $this;
return $indexOrColumns;
}
/**
......
......@@ -28,6 +28,7 @@ class AuthTest extends TestCase
public function testRemindOld()
{
if (Application::VERSION >= '5.2') {
$this->expectNotToPerformAssertions();
return;
}
......
......@@ -434,6 +434,16 @@ class ModelTest extends TestCase
$this->assertEquals((string) $user->getAttribute('entry.date')->format('Y-m-d H:i:s'), $data['entry']['date']);
}
public function testCarbonDateMockingWorks()
{
$fakeDate = \Carbon\Carbon::createFromDate(2000, 01, 01);
Carbon::setTestNow($fakeDate);
$item = Item::create(['name' => 'sword']);
$this->assertLessThan(1, $fakeDate->diffInSeconds($item->created_at));
}
public function testIdAttribute(): void
{
/** @var User $user */
......
<?php
declare(strict_types=1);
use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider;
class QueueTest extends TestCase
{
public function setUp(): void
......@@ -55,4 +57,38 @@ class QueueTest extends TestCase
$job->delete();
$this->assertEquals(0, Queue::getDatabase()->table(Config::get('queue.connections.database.table'))->count());
}
public function testFailQueueJob(): void
{
$provider = app('queue.failer');
$this->assertInstanceOf(MongoFailedJobProvider::class, $provider);
}
public function testFindFailJobNull(): void
{
Config::set('queue.failed.database', 'mongodb');
$provider = app('queue.failer');
$this->assertNull($provider->find(1));
}
public function testIncrementAttempts(): void
{
$job_id = Queue::push('test1', ['action' => 'QueueJobExpired'], 'test');
$this->assertNotNull($job_id);
$job_id = Queue::push('test2', ['action' => 'QueueJobExpired'], 'test');
$this->assertNotNull($job_id);
$job = Queue::pop('test');
$this->assertEquals(1, $job->attempts());
$job->delete();
$others_jobs = Queue::getDatabase()
->table(Config::get('queue.connections.database.table'))
->get();
$this->assertCount(1, $others_jobs);
$this->assertEquals(0, $others_jobs[0]['attempts']);
}
}
......@@ -147,6 +147,76 @@ class SchemaTest extends TestCase
$this->assertFalse($index);
}
public function testDropIndexIfExists(): void
{
Schema::collection('newcollection', function (Blueprint $collection) {
$collection->unique('uniquekey');
$collection->dropIndexIfExists('uniquekey_1');
});
$index = $this->getIndex('newcollection', 'uniquekey');
$this->assertEquals(null, $index);
Schema::collection('newcollection', function (Blueprint $collection) {
$collection->unique('uniquekey');
$collection->dropIndexIfExists(['uniquekey']);
});
$index = $this->getIndex('newcollection', 'uniquekey');
$this->assertEquals(null, $index);
Schema::collection('newcollection', function (Blueprint $collection) {
$collection->index(['field_a', 'field_b']);
});
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
$this->assertNotNull($index);
Schema::collection('newcollection', function (Blueprint $collection) {
$collection->dropIndexIfExists(['field_a', 'field_b']);
});
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
$this->assertFalse($index);
Schema::collection('newcollection', function (Blueprint $collection) {
$collection->index(['field_a', 'field_b'], 'custom_index_name');
});
$index = $this->getIndex('newcollection', 'custom_index_name');
$this->assertNotNull($index);
Schema::collection('newcollection', function (Blueprint $collection) {
$collection->dropIndexIfExists('custom_index_name');
});
$index = $this->getIndex('newcollection', 'custom_index_name');
$this->assertFalse($index);
}
public function testHasIndex(): void
{
$instance = $this;
Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
$collection->index('myhaskey1');
$instance->assertTrue($collection->hasIndex('myhaskey1_1'));
$instance->assertFalse($collection->hasIndex('myhaskey1'));
});
Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
$collection->index('myhaskey2');
$instance->assertTrue($collection->hasIndex(['myhaskey2']));
$instance->assertFalse($collection->hasIndex(['myhaskey2_1']));
});
Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
$collection->index(['field_a', 'field_b']);
$instance->assertTrue($collection->hasIndex(['field_a_1_field_b']));
$instance->assertFalse($collection->hasIndex(['field_a_1_field_b_1']));
});
}
public function testBackground(): void
{
Schema::collection('newcollection', function ($collection) {
......@@ -230,6 +300,7 @@ class SchemaTest extends TestCase
$collection->boolean('activated')->default(0);
$collection->integer('user_id')->unsigned();
});
$this->expectNotToPerformAssertions();
}
public function testSparseUnique(): void
......
......@@ -28,6 +28,7 @@ class TestCase extends Orchestra\Testbench\TestCase
{
return [
Jenssegers\Mongodb\MongodbServiceProvider::class,
Jenssegers\Mongodb\MongodbQueueServiceProvider::class,
Jenssegers\Mongodb\Auth\PasswordResetServiceProvider::class,
Jenssegers\Mongodb\Validation\ValidationServiceProvider::class,
];
......@@ -50,6 +51,7 @@ class TestCase extends Orchestra\Testbench\TestCase
$app['config']->set('database.default', 'mongodb');
$app['config']->set('database.connections.mysql', $config['connections']['mysql']);
$app['config']->set('database.connections.mongodb', $config['connections']['mongodb']);
$app['config']->set('database.connections.mongodb2', $config['connections']['mongodb']);
$app['config']->set('database.connections.dsn_mongodb', $config['connections']['dsn_mongodb']);
$app['config']->set('auth.model', 'User');
......@@ -63,5 +65,6 @@ class TestCase extends Orchestra\Testbench\TestCase
'queue' => 'default',
'expire' => 60,
]);
$app['config']->set('queue.failed.database', 'mongodb2');
}
}
......@@ -2,6 +2,7 @@
$mongoHost = env('MONGO_HOST', 'mongodb');
$mongoPort = env('MONGO_PORT') ? (int) env('MONGO_PORT') : 27017;
$mysqlPort = env('MYSQL_PORT') ? (int) env('MYSQL_PORT') : 3306;
return [
......@@ -23,6 +24,7 @@ return [
'mysql' => [
'driver' => 'mysql',
'host' => env('MYSQL_HOST', 'mysql'),
'port' => $mysqlPort,
'database' => env('MYSQL_DATABASE', 'unittest'),
'username' => env('MYSQL_USERNAME', 'root'),
'password' => env('MYSQL_PASSWORD', ''),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment