Commit 296fac28 authored by Jens Segers's avatar Jens Segers Committed by GitHub

Merge pull request #930 from pi0/UnitTests

5.3 UnitTests
parents 8e69e056 fb81b2e8
language: php language: php
php: php:
- 5.5
- 5.6 - 5.6
- 7 - 7
- hhvm
matrix: matrix:
fast_finish: true fast_finish: true
allow_failures:
- php: hhvm
sudo: false sudo: false
...@@ -35,4 +31,4 @@ script: ...@@ -35,4 +31,4 @@ script:
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml - vendor/bin/phpunit --coverage-clover build/logs/clover.xml
after_success: after_success:
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php vendor/bin/coveralls -v; fi;' - sh -c 'php vendor/bin/coveralls -v'
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use MongoDB\Driver\Cursor; use MongoDB\Driver\Cursor;
use MongoDB\Model\BSONDocument; use MongoDB\Model\BSONDocument;
...@@ -167,9 +168,10 @@ class Builder extends EloquentBuilder ...@@ -167,9 +168,10 @@ class Builder extends EloquentBuilder
$query = $hasQuery->getQuery(); $query = $hasQuery->getQuery();
// Get the number of related objects for each possible parent. // Get the number of related objects for each possible parent.
$relations = $query->pluck($relation->getHasCompareKey());
$relationCount = array_count_values(array_map(function ($id) { $relationCount = array_count_values(array_map(function ($id) {
return (string) $id; // Convert Back ObjectIds to Strings return (string) $id; // Convert Back ObjectIds to Strings
}, $query->pluck($relation->getHasCompareKey()))); }, is_array($relations) ? $relations : $relations->toArray()));
// Remove unwanted related objects based on the operator and count. // Remove unwanted related objects based on the operator and count.
$relationCount = array_filter($relationCount, function ($counted) use ($count, $operator) { $relationCount = array_filter($relationCount, function ($counted) use ($count, $operator) {
......
...@@ -244,6 +244,18 @@ abstract class Model extends BaseModel ...@@ -244,6 +244,18 @@ abstract class Model extends BaseModel
// Get the relation results. // Get the relation results.
return $this->getRelationshipFromMethod($key, $camelKey); return $this->getRelationshipFromMethod($key, $camelKey);
} }
if ($relations instanceof Relation) {
// If the key already exists in the relationships array, it just means the
// relationship has already been loaded, so we'll just return it out of
// here because there is no need to query within the relations twice.
if (array_key_exists($key, $this->relations) && $this->relations[$key] != null) {
return $this->relations[$key];
}
// Get the relation results.
return $this->getRelationshipFromMethod($key, $camelKey);
}
} }
} }
......
...@@ -577,7 +577,16 @@ class Builder extends BaseBuilder ...@@ -577,7 +577,16 @@ class Builder extends BaseBuilder
{ {
$results = $this->get(is_null($key) ? [$column] : [$column, $key]); $results = $this->get(is_null($key) ? [$column] : [$column, $key]);
return $this->useCollections ? $results->pluck($column, $key) : Arr::pluck($results, $column, $key); // Convert ObjectID's to strings
if ($key == '_id') {
$results = $results->map(function ($item) {
$item['_id'] = (string) $item['_id'];
return $item;
});
}
$p = Arr::pluck($results, $column, $key);
return $this->useCollections ? new Collection($p) : $p;
} }
/** /**
...@@ -632,20 +641,7 @@ class Builder extends BaseBuilder ...@@ -632,20 +641,7 @@ class Builder extends BaseBuilder
*/ */
public function lists($column, $key = null) public function lists($column, $key = null)
{ {
if ($key == '_id') { return $this->pluck($column, $key);
$results = new Collection($this->get([$column, $key]));
// Convert ObjectID's to strings so that lists can do its work.
$results = $results->map(function ($item) {
$item['_id'] = (string) $item['_id'];
return $item;
});
return $results->pluck($column, $key)->all();
}
return parent::pluck($column, $key);
} }
/** /**
......
...@@ -79,12 +79,21 @@ class MongoQueue extends DatabaseQueue ...@@ -79,12 +79,21 @@ class MongoQueue extends DatabaseQueue
*/ */
protected function releaseJobsThatHaveBeenReservedTooLong($queue) protected function releaseJobsThatHaveBeenReservedTooLong($queue)
{ {
$expired = Carbon::now()->subSeconds($this->expire)->getTimestamp(); $expiration = Carbon::now()->subSeconds($this->expire)->getTimestamp();
$now = time();
$reserved = $this->database->collection($this->table) $reserved = $this->database->collection($this->table)
->where('queue', $this->getQueue($queue)) ->where('queue', $this->getQueue($queue))
->where('reserved', 1) ->where(function ($query) use ($expiration, $now) {
->where('reserved_at', '<=', $expired)->get(); // 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();
foreach ($reserved as $job) { foreach ($reserved as $job) {
$attempts = $job['attempts'] + 1; $attempts = $job['attempts'] + 1;
......
...@@ -286,4 +286,24 @@ class BelongsToMany extends EloquentBelongsToMany ...@@ -286,4 +286,24 @@ class BelongsToMany extends EloquentBelongsToMany
{ {
return $this->foreignKey; return $this->foreignKey;
} }
/**
* Format the sync list so that it is keyed by ID. (Legacy Support)
* The original function has been renamed to formatRecordsList since Laravel 5.3
*
* @deprecated
* @param array $records
* @return array
*/
protected function formatSyncList(array $records)
{
$results = [];
foreach ($records as $id => $attributes) {
if (! is_array($attributes)) {
list($id, $attributes) = [$attributes, []];
}
$results[$id] = $attributes;
}
return $results;
}
} }
...@@ -44,9 +44,11 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint ...@@ -44,9 +44,11 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
* *
* @param string|array $columns * @param string|array $columns
* @param array $options * @param array $options
* @param string $name
* @param string|null $algorithm
* @return Blueprint * @return Blueprint
*/ */
public function index($columns = null, $options = []) public function index($columns = null, $name = null, $algorithm = null, $options = [])
{ {
$columns = $this->fluent($columns); $columns = $this->fluent($columns);
...@@ -71,10 +73,12 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint ...@@ -71,10 +73,12 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
* Specify the primary key(s) for the table. * Specify the primary key(s) for the table.
* *
* @param string|array $columns * @param string|array $columns
* @param string $name
* @param string|null $algorithm
* @param array $options * @param array $options
* @return \Illuminate\Support\Fluent * @return \Illuminate\Support\Fluent
*/ */
public function primary($columns = null, $options = []) public function primary($columns = null, $name = null, $algorithm = null, $options = [])
{ {
return $this->unique($columns, $options); return $this->unique($columns, $options);
} }
...@@ -112,16 +116,18 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint ...@@ -112,16 +116,18 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
* Specify a unique index for the collection. * Specify a unique index for the collection.
* *
* @param string|array $columns * @param string|array $columns
* @param string $name
* @param string|null $algorithm
* @param array $options * @param array $options
* @return Blueprint * @return Blueprint
*/ */
public function unique($columns = null, $options = []) public function unique($columns = null, $name = null, $algorithm = null, $options = [])
{ {
$columns = $this->fluent($columns); $columns = $this->fluent($columns);
$options['unique'] = true; $options['unique'] = true;
$this->index($columns, $options); $this->index($columns, null, null, $options);
return $this; return $this;
} }
...@@ -136,7 +142,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint ...@@ -136,7 +142,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
{ {
$columns = $this->fluent($columns); $columns = $this->fluent($columns);
$this->index($columns, ['background' => true]); $this->index($columns, null, null, ['background' => true]);
return $this; return $this;
} }
...@@ -154,7 +160,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint ...@@ -154,7 +160,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
$options['sparse'] = true; $options['sparse'] = true;
$this->index($columns, $options); $this->index($columns, null, null, $options);
return $this; return $this;
} }
...@@ -171,7 +177,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint ...@@ -171,7 +177,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
{ {
$columns = $this->fluent($columns); $columns = $this->fluent($columns);
$this->index($columns, ['expireAfterSeconds' => $seconds]); $this->index($columns, null, null, ['expireAfterSeconds' => $seconds]);
return $this; return $this;
} }
......
This diff is collapsed.
...@@ -156,8 +156,8 @@ class ModelTest extends TestCase ...@@ -156,8 +156,8 @@ class ModelTest extends TestCase
$all = User::all(); $all = User::all();
$this->assertEquals(2, count($all)); $this->assertEquals(2, count($all));
$this->assertContains('John Doe', $all->lists('name')); $this->assertContains('John Doe', $all->pluck('name'));
$this->assertContains('Jane Doe', $all->lists('name')); $this->assertContains('Jane Doe', $all->pluck('name'));
} }
public function testFind() public function testFind()
......
...@@ -29,7 +29,7 @@ class QueryBuilderTest extends TestCase ...@@ -29,7 +29,7 @@ class QueryBuilderTest extends TestCase
public function testNoDocument() public function testNoDocument()
{ {
$items = DB::collection('items')->where('name', 'nothing')->get(); $items = DB::collection('items')->where('name', 'nothing')->get()->toArray();
$this->assertEquals([], $items); $this->assertEquals([], $items);
$item = DB::collection('items')->where('name', 'nothing')->first(); $item = DB::collection('items')->where('name', 'nothing')->first();
...@@ -288,12 +288,12 @@ class QueryBuilderTest extends TestCase ...@@ -288,12 +288,12 @@ class QueryBuilderTest extends TestCase
['name' => 'spoon', 'type' => 'round'], ['name' => 'spoon', 'type' => 'round'],
]); ]);
$items = DB::collection('items')->distinct('name')->get(); $items = DB::collection('items')->distinct('name')->get()->toArray();
sort($items); sort($items);
$this->assertEquals(3, count($items)); $this->assertEquals(3, count($items));
$this->assertEquals(['fork', 'knife', 'spoon'], $items); $this->assertEquals(['fork', 'knife', 'spoon'], $items);
$types = DB::collection('items')->distinct('type')->get(); $types = DB::collection('items')->distinct('type')->get()->toArray();
sort($types); sort($types);
$this->assertEquals(2, count($types)); $this->assertEquals(2, count($types));
$this->assertEquals(['round', 'sharp'], $types); $this->assertEquals(['round', 'sharp'], $types);
...@@ -357,7 +357,7 @@ class QueryBuilderTest extends TestCase ...@@ -357,7 +357,7 @@ class QueryBuilderTest extends TestCase
['name' => 'John Doe', 'age' => 25], ['name' => 'John Doe', 'age' => 25],
]); ]);
$age = DB::collection('users')->where('name', 'John Doe')->pluck('age'); $age = DB::collection('users')->where('name', 'John Doe')->pluck('age')->toArray();
$this->assertEquals([25], $age); $this->assertEquals([25], $age);
} }
...@@ -370,16 +370,16 @@ class QueryBuilderTest extends TestCase ...@@ -370,16 +370,16 @@ class QueryBuilderTest extends TestCase
['name' => 'spoon', 'type' => 'round', 'amount' => 14], ['name' => 'spoon', 'type' => 'round', 'amount' => 14],
]); ]);
$list = DB::collection('items')->lists('name'); $list = DB::collection('items')->pluck('name')->toArray();
sort($list); sort($list);
$this->assertEquals(4, count($list)); $this->assertEquals(4, count($list));
$this->assertEquals(['fork', 'knife', 'spoon', 'spoon'], $list); $this->assertEquals(['fork', 'knife', 'spoon', 'spoon'], $list);
$list = DB::collection('items')->lists('type', 'name'); $list = DB::collection('items')->pluck('type', 'name')->toArray();
$this->assertEquals(3, count($list)); $this->assertEquals(3, count($list));
$this->assertEquals(['knife' => 'sharp', 'fork' => 'sharp', 'spoon' => 'round'], $list); $this->assertEquals(['knife' => 'sharp', 'fork' => 'sharp', 'spoon' => 'round'], $list);
$list = DB::collection('items')->lists('name', '_id'); $list = DB::collection('items')->pluck('name', '_id')->toArray();
$this->assertEquals(4, count($list)); $this->assertEquals(4, count($list));
$this->assertEquals(24, strlen(key($list))); $this->assertEquals(24, strlen(key($list)));
} }
......
...@@ -38,7 +38,7 @@ class QueueTest extends TestCase ...@@ -38,7 +38,7 @@ class QueueTest extends TestCase
// Expect an attempted older job in the queue // Expect an attempted older job in the queue
$job = Queue::pop('test'); $job = Queue::pop('test');
$this->assertEquals(2, $job->getDatabaseJob()->attempts); $this->assertEquals(1, $job->getDatabaseJob()->attempts);
$this->assertGreaterThan($expiry, $job->getDatabaseJob()->reserved_at); $this->assertGreaterThan($expiry, $job->getDatabaseJob()->reserved_at);
$job->delete(); $job->delete();
......
...@@ -322,8 +322,8 @@ class RelationsTest extends TestCase ...@@ -322,8 +322,8 @@ class RelationsTest extends TestCase
$this->assertTrue(array_key_exists('groups', $user->getAttributes())); $this->assertTrue(array_key_exists('groups', $user->getAttributes()));
// Assert they are attached // Assert they are attached
$this->assertTrue(in_array($group->_id, $user->groups)); $this->assertTrue(in_array($group->_id, $user->groups->pluck('_id')->toArray()));
$this->assertTrue(in_array($user->_id, $group->users)); $this->assertTrue(in_array($user->_id, $group->users->pluck('_id')->toArray()));
$this->assertEquals($group->_id, $user->groups()->first()->_id); $this->assertEquals($group->_id, $user->groups()->first()->_id);
$this->assertEquals($user->_id, $group->users()->first()->_id); $this->assertEquals($user->_id, $group->users()->first()->_id);
} }
......
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