Skip to content

5.3 UnitTests #930

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 9 commits into from
Aug 30, 2016
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
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
language: php

php:
- 5.5
- 5.6
- 7
- hhvm

matrix:
fast_finish: true
allow_failures:
- php: hhvm

sudo: false

Expand All @@ -35,4 +31,4 @@ script:
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml

after_success:
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php vendor/bin/coveralls -v; fi;'
- sh -c 'php vendor/bin/coveralls -v'
4 changes: 3 additions & 1 deletion src/Jenssegers/Mongodb/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use MongoDB\Driver\Cursor;
use MongoDB\Model\BSONDocument;

Expand Down Expand Up @@ -167,9 +168,10 @@ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $o
$query = $hasQuery->getQuery();

// Get the number of related objects for each possible parent.
$relations = $query->pluck($relation->getHasCompareKey());
$relationCount = array_count_values(array_map(function ($id) {
return (string) $id; // Convert Back ObjectIds to Strings
}, $query->pluck($relation->getHasCompareKey())));
}, is_array($relations) ? $relations : $relations->toArray()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This tests fail without this check:

  • RelationsTest.php:384
  • RelationsTest.php:433

Error: array_map(): Argument #2 should be an array

Laravel has returns arrays instead of Collections!


// Remove unwanted related objects based on the operator and count.
$relationCount = array_filter($relationCount, function ($counted) use ($count, $operator) {
Expand Down
12 changes: 12 additions & 0 deletions src/Jenssegers/Mongodb/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ public function getAttribute($key)
// Get the relation results.
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);
}
}
}

Expand Down
26 changes: 11 additions & 15 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,16 @@ public function pluck($column, $key = null)
{
$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;
}

/**
Expand Down Expand Up @@ -632,20 +641,7 @@ public function truncate()
*/
public function lists($column, $key = null)
{
if ($key == '_id') {
$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);
return $this->pluck($column, $key);
}

/**
Expand Down
15 changes: 12 additions & 3 deletions src/Jenssegers/Mongodb/Queue/MongoQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,21 @@ protected function getNextAvailableJobAndReserve($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)
->where('queue', $this->getQueue($queue))
->where('reserved', 1)
->where('reserved_at', '<=', $expired)->get();
->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();

foreach ($reserved as $job) {
$attempts = $job['attempts'] + 1;
Expand Down
20 changes: 20 additions & 0 deletions src/Jenssegers/Mongodb/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,24 @@ public function getForeignKey()
{
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;
}
}
20 changes: 13 additions & 7 deletions src/Jenssegers/Mongodb/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ public function __construct(Connection $connection, $collection)
*
* @param string|array $columns
* @param array $options
* @param string $name
* @param string|null $algorithm
* @return Blueprint
*/
public function index($columns = null, $options = [])
public function index($columns = null, $name = null, $algorithm = null, $options = [])
{
$columns = $this->fluent($columns);

Expand All @@ -71,10 +73,12 @@ public function index($columns = null, $options = [])
* Specify the primary key(s) for the table.
*
* @param string|array $columns
* @param string $name
* @param string|null $algorithm
* @param array $options
* @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);
}
Expand Down Expand Up @@ -112,16 +116,18 @@ public function dropIndex($columns = null)
* Specify a unique index for the collection.
*
* @param string|array $columns
* @param string $name
* @param string|null $algorithm
* @param array $options
* @return Blueprint
*/
public function unique($columns = null, $options = [])
public function unique($columns = null, $name = null, $algorithm = null, $options = [])
{
$columns = $this->fluent($columns);

$options['unique'] = true;

$this->index($columns, $options);
$this->index($columns, null, null, $options);

return $this;
}
Expand All @@ -136,7 +142,7 @@ public function background($columns = null)
{
$columns = $this->fluent($columns);

$this->index($columns, ['background' => true]);
$this->index($columns, null, null, ['background' => true]);

return $this;
}
Expand All @@ -154,7 +160,7 @@ public function sparse($columns = null, $options = [])

$options['sparse'] = true;

$this->index($columns, $options);
$this->index($columns, null, null, $options);

return $this;
}
Expand All @@ -171,7 +177,7 @@ public function expire($columns, $seconds)
{
$columns = $this->fluent($columns);

$this->index($columns, ['expireAfterSeconds' => $seconds]);
$this->index($columns, null, null, ['expireAfterSeconds' => $seconds]);

return $this;
}
Expand Down
Loading