Skip to content

Commit 296fac2

Browse files
authored
Merge pull request #930 from pi0/UnitTests
5.3 UnitTests
2 parents 8e69e05 + fb81b2e commit 296fac2

File tree

12 files changed

+119
-78
lines changed

12 files changed

+119
-78
lines changed

.travis.yml

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
language: php
22

33
php:
4-
- 5.5
54
- 5.6
65
- 7
7-
- hhvm
86

97
matrix:
108
fast_finish: true
11-
allow_failures:
12-
- php: hhvm
139

1410
sudo: false
1511

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

3733
after_success:
38-
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php vendor/bin/coveralls -v; fi;'
34+
- sh -c 'php vendor/bin/coveralls -v'

src/Jenssegers/Mongodb/Eloquent/Builder.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
44
use Illuminate\Database\Eloquent\Relations\Relation;
5+
use Illuminate\Support\Collection;
56
use MongoDB\Driver\Cursor;
67
use MongoDB\Model\BSONDocument;
78

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

169170
// Get the number of related objects for each possible parent.
171+
$relations = $query->pluck($relation->getHasCompareKey());
170172
$relationCount = array_count_values(array_map(function ($id) {
171173
return (string) $id; // Convert Back ObjectIds to Strings
172-
}, $query->pluck($relation->getHasCompareKey())));
174+
}, is_array($relations) ? $relations : $relations->toArray()));
173175

174176
// Remove unwanted related objects based on the operator and count.
175177
$relationCount = array_filter($relationCount, function ($counted) use ($count, $operator) {

src/Jenssegers/Mongodb/Eloquent/Model.php

+12
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,18 @@ public function getAttribute($key)
244244
// Get the relation results.
245245
return $this->getRelationshipFromMethod($key, $camelKey);
246246
}
247+
248+
if ($relations instanceof Relation) {
249+
// If the key already exists in the relationships array, it just means the
250+
// relationship has already been loaded, so we'll just return it out of
251+
// here because there is no need to query within the relations twice.
252+
if (array_key_exists($key, $this->relations) && $this->relations[$key] != null) {
253+
return $this->relations[$key];
254+
}
255+
256+
// Get the relation results.
257+
return $this->getRelationshipFromMethod($key, $camelKey);
258+
}
247259
}
248260
}
249261

src/Jenssegers/Mongodb/Query/Builder.php

+11-15
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,16 @@ public function pluck($column, $key = null)
577577
{
578578
$results = $this->get(is_null($key) ? [$column] : [$column, $key]);
579579

580-
return $this->useCollections ? $results->pluck($column, $key) : Arr::pluck($results, $column, $key);
580+
// Convert ObjectID's to strings
581+
if ($key == '_id') {
582+
$results = $results->map(function ($item) {
583+
$item['_id'] = (string) $item['_id'];
584+
return $item;
585+
});
586+
}
587+
588+
$p = Arr::pluck($results, $column, $key);
589+
return $this->useCollections ? new Collection($p) : $p;
581590
}
582591

583592
/**
@@ -632,20 +641,7 @@ public function truncate()
632641
*/
633642
public function lists($column, $key = null)
634643
{
635-
if ($key == '_id') {
636-
$results = new Collection($this->get([$column, $key]));
637-
638-
// Convert ObjectID's to strings so that lists can do its work.
639-
$results = $results->map(function ($item) {
640-
$item['_id'] = (string) $item['_id'];
641-
642-
return $item;
643-
});
644-
645-
return $results->pluck($column, $key)->all();
646-
}
647-
648-
return parent::pluck($column, $key);
644+
return $this->pluck($column, $key);
649645
}
650646

651647
/**

src/Jenssegers/Mongodb/Queue/MongoQueue.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,21 @@ protected function getNextAvailableJobAndReserve($queue)
7979
*/
8080
protected function releaseJobsThatHaveBeenReservedTooLong($queue)
8181
{
82-
$expired = Carbon::now()->subSeconds($this->expire)->getTimestamp();
82+
$expiration = Carbon::now()->subSeconds($this->expire)->getTimestamp();
83+
$now = time();
8384

8485
$reserved = $this->database->collection($this->table)
8586
->where('queue', $this->getQueue($queue))
86-
->where('reserved', 1)
87-
->where('reserved_at', '<=', $expired)->get();
87+
->where(function ($query) use ($expiration, $now) {
88+
// Check for available jobs
89+
$query->where(function ($query) use ($now) {
90+
$query->whereNull('reserved_at');
91+
$query->where('available_at', '<=', $now);
92+
});
93+
94+
// Check for jobs that are reserved but have expired
95+
$query->orWhere('reserved_at', '<=', $expiration);
96+
})->get();
8897

8998
foreach ($reserved as $job) {
9099
$attempts = $job['attempts'] + 1;

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

+20
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,24 @@ public function getForeignKey()
286286
{
287287
return $this->foreignKey;
288288
}
289+
290+
/**
291+
* Format the sync list so that it is keyed by ID. (Legacy Support)
292+
* The original function has been renamed to formatRecordsList since Laravel 5.3
293+
*
294+
* @deprecated
295+
* @param array $records
296+
* @return array
297+
*/
298+
protected function formatSyncList(array $records)
299+
{
300+
$results = [];
301+
foreach ($records as $id => $attributes) {
302+
if (! is_array($attributes)) {
303+
list($id, $attributes) = [$attributes, []];
304+
}
305+
$results[$id] = $attributes;
306+
}
307+
return $results;
308+
}
289309
}

src/Jenssegers/Mongodb/Schema/Blueprint.php

+13-7
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ public function __construct(Connection $connection, $collection)
4444
*
4545
* @param string|array $columns
4646
* @param array $options
47+
* @param string $name
48+
* @param string|null $algorithm
4749
* @return Blueprint
4850
*/
49-
public function index($columns = null, $options = [])
51+
public function index($columns = null, $name = null, $algorithm = null, $options = [])
5052
{
5153
$columns = $this->fluent($columns);
5254

@@ -71,10 +73,12 @@ public function index($columns = null, $options = [])
7173
* Specify the primary key(s) for the table.
7274
*
7375
* @param string|array $columns
76+
* @param string $name
77+
* @param string|null $algorithm
7478
* @param array $options
7579
* @return \Illuminate\Support\Fluent
7680
*/
77-
public function primary($columns = null, $options = [])
81+
public function primary($columns = null, $name = null, $algorithm = null, $options = [])
7882
{
7983
return $this->unique($columns, $options);
8084
}
@@ -112,16 +116,18 @@ public function dropIndex($columns = null)
112116
* Specify a unique index for the collection.
113117
*
114118
* @param string|array $columns
119+
* @param string $name
120+
* @param string|null $algorithm
115121
* @param array $options
116122
* @return Blueprint
117123
*/
118-
public function unique($columns = null, $options = [])
124+
public function unique($columns = null, $name = null, $algorithm = null, $options = [])
119125
{
120126
$columns = $this->fluent($columns);
121127

122128
$options['unique'] = true;
123129

124-
$this->index($columns, $options);
130+
$this->index($columns, null, null, $options);
125131

126132
return $this;
127133
}
@@ -136,7 +142,7 @@ public function background($columns = null)
136142
{
137143
$columns = $this->fluent($columns);
138144

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

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

155161
$options['sparse'] = true;
156162

157-
$this->index($columns, $options);
163+
$this->index($columns, null, null, $options);
158164

159165
return $this;
160166
}
@@ -171,7 +177,7 @@ public function expire($columns, $seconds)
171177
{
172178
$columns = $this->fluent($columns);
173179

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

176182
return $this;
177183
}

0 commit comments

Comments
 (0)