Skip to content

Commit d1f5dd0

Browse files
author
Dennis Heller
committed
Merge remote-tracking branch 'original/master'
# Conflicts: # src/Jenssegers/Mongodb/Auth/User.php
2 parents 2789238 + 1bb259a commit d1f5dd0

File tree

13 files changed

+126
-35
lines changed

13 files changed

+126
-35
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ Embedded relations now return an `Illuminate\Database\Eloquent\Collection` rathe
103103
$books = $user->books()->sortBy('title');
104104
```
105105

106+
Testing
107+
-------
108+
109+
To run the test for this package, run:
110+
111+
```
112+
docker-compose up
113+
```
114+
106115
Configuration
107116
-------------
108117

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"require-dev": {
2121
"phpunit/phpunit": "^6.0",
2222
"orchestra/testbench": "^3.1",
23-
"mockery/mockery": "^0.9",
24-
"satooshi/php-coveralls": "^1.0",
23+
"mockery/mockery": "^1.0",
24+
"satooshi/php-coveralls": "^2.0",
2525
"doctrine/dbal": "^2.5"
2626
},
2727
"autoload": {

docker-compose.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: '3'
2+
3+
services:
4+
5+
php:
6+
build:
7+
context: .
8+
dockerfile: docker/Dockerfile
9+
volumes:
10+
- .:/code
11+
working_dir: /code
12+
command: docker/entrypoint.sh
13+
depends_on:
14+
- mysql
15+
- mongodb
16+
17+
mysql:
18+
image: mysql
19+
environment:
20+
MYSQL_ROOT_PASSWORD:
21+
MYSQL_DATABASE: unittest
22+
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
23+
logging:
24+
driver: none
25+
26+
mongodb:
27+
image: mongo
28+
logging:
29+
driver: none

docker/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM php:7.1-cli
2+
3+
RUN apt-get update && \
4+
apt-get install -y autoconf pkg-config libssl-dev && \
5+
pecl install mongodb && docker-php-ext-enable mongodb && \
6+
docker-php-ext-install -j$(nproc) pdo pdo_mysql

docker/entrypoint.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
sleep 3 && php ./vendor/bin/phpunit

src/Jenssegers/Mongodb/Auth/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
99
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
1010
use Illuminate\Foundation\Auth\Access\Authorizable;
11-
use Jenssegers\Mongodb\Eloquent\Model as Model;
11+
use Jenssegers\Mongodb\Eloquent\Model;
1212
use Jenssegers\Mongodb\Notifications\Notifiable;
1313

1414
class User extends Model implements

src/Jenssegers/Mongodb/Connection.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Connection as BaseConnection;
66
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Str;
78
use MongoDB\Client;
89

910
class Connection extends BaseConnection
@@ -150,18 +151,43 @@ public function disconnect()
150151
}
151152

152153
/**
153-
* Create a DSN string from a configuration.
154+
* Determine if the given configuration array has a UNIX socket value.
154155
*
155-
* @param array $config
156+
* @param array $config
157+
* @return bool
158+
*/
159+
protected function hasDsnString(array $config)
160+
{
161+
return isset($config['dsn']) && ! empty($config['dsn']);
162+
}
163+
164+
/**
165+
* Get the DSN string for a socket configuration.
166+
*
167+
* @param array $config
156168
* @return string
157169
*/
158-
protected function getDsn(array $config)
170+
protected function getDsnString(array $config)
159171
{
160-
// Check if the user passed a complete dsn to the configuration.
161-
if (!empty($config['dsn'])) {
162-
return $config['dsn'];
172+
$dsn_string = $config['dsn'];
173+
174+
if (Str::contains($dsn_string, 'mongodb://')) {
175+
$dsn_string = Str::replaceFirst('mongodb://', '', $dsn_string);
163176
}
164177

178+
$dsn_string = rawurlencode($dsn_string);
179+
180+
return "mongodb://{$dsn_string}";
181+
}
182+
183+
/**
184+
* Get the DSN string for a host / port configuration.
185+
*
186+
* @param array $config
187+
* @return string
188+
*/
189+
protected function getHostDsn(array $config)
190+
{
165191
// Treat host option as array of hosts
166192
$hosts = is_array($config['host']) ? $config['host'] : [$config['host']];
167193

@@ -178,6 +204,19 @@ protected function getDsn(array $config)
178204
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
179205
}
180206

207+
/**
208+
* Create a DSN string from a configuration.
209+
*
210+
* @param array $config
211+
* @return string
212+
*/
213+
protected function getDsn(array $config)
214+
{
215+
return $this->hasDsnString($config)
216+
? $this->getDsnString($config)
217+
: $this->getHostDsn($config);
218+
}
219+
181220
/**
182221
* @inheritdoc
183222
*/

src/Jenssegers/Mongodb/Eloquent/HybridRelations.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ protected function guessBelongsToManyRelation()
300300
*/
301301
public function newEloquentBuilder($query)
302302
{
303-
return new EloquentBuilder($query);
303+
if (is_subclass_of($this, \Jenssegers\Mongodb\Eloquent\Model::class)) {
304+
return new Builder($query);
305+
} else {
306+
return new EloquentBuilder($query);
307+
}
304308
}
305309
}

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public function hint($index)
198198
*/
199199
public function find($id, $columns = [])
200200
{
201-
return $this->where($this->getKeyName(), '=', $this->convertKey($id))->first($columns);
201+
return $this->where('_id', '=', $this->convertKey($id))->first($columns);
202202
}
203203

204204
/**
@@ -920,13 +920,6 @@ protected function compileWheres()
920920
}
921921

922922
// Convert DateTime values to UTCDateTime.
923-
if (isset($where['values'])) {
924-
array_walk_recursive($where['values'], function (&$item, $key) {
925-
if ($item instanceof DateTime) {
926-
$item = new UTCDateTime($item->getTimestamp() * 1000);
927-
}
928-
});
929-
}
930923
if (isset($where['value'])) {
931924
if (is_array($where['value'])) {
932925
array_walk_recursive($where['value'], function (&$item, $key) {
@@ -939,6 +932,12 @@ protected function compileWheres()
939932
$where['value'] = new UTCDateTime($where['value']->getTimestamp() * 1000);
940933
}
941934
}
935+
} elseif (isset($where['values'])) {
936+
array_walk_recursive($where['values'], function (&$item, $key) {
937+
if ($item instanceof DateTime) {
938+
$item = new UTCDateTime($item->getTimestamp() * 1000);
939+
}
940+
});
942941
}
943942

944943
// The next item in a "chain" of wheres devices the boolean of the

tests/ConnectionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,13 @@ public function testDriverName()
9898

9999
public function testAuth()
100100
{
101+
$host = Config::get('database.connections.mongodb.host');
101102
Config::set('database.connections.mongodb.username', 'foo');
102103
Config::set('database.connections.mongodb.password', 'bar');
103104
Config::set('database.connections.mongodb.options.database', 'custom');
104105

105106
$connection = DB::connection('mongodb');
106-
$this->assertEquals('mongodb://127.0.0.1/custom', (string) $connection->getMongoClient());
107+
$this->assertEquals('mongodb://' . $host . '/custom', (string) $connection->getMongoClient());
107108
}
108109

109110
public function testCustomHostAndPort()

tests/EmbeddedRelationsTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testEmbedsManySave()
2121
$address = new Address(['city' => 'London']);
2222

2323
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
24-
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything());
24+
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any());
2525
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true);
2626
$events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(true);
2727
$events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($address), $address);
@@ -47,7 +47,7 @@ public function testEmbedsManySave()
4747
$this->assertEquals(['London', 'Paris'], $user->addresses->pluck('city')->all());
4848

4949
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
50-
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything());
50+
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any());
5151
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true);
5252
$events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(true);
5353
$events->shouldReceive('fire')->once()->with('eloquent.updated: ' . get_class($address), $address);
@@ -213,7 +213,7 @@ public function testEmbedsManyDestroy()
213213
$address = $user->addresses->first();
214214

215215
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
216-
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything());
216+
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any());
217217
$events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true);
218218
$events->shouldReceive('fire')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address'));
219219

@@ -252,7 +252,7 @@ public function testEmbedsManyDelete()
252252
$address = $user->addresses->first();
253253

254254
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
255-
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything());
255+
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any());
256256
$events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true);
257257
$events->shouldReceive('fire')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address'));
258258

@@ -301,7 +301,7 @@ public function testEmbedsManyCreatingEventReturnsFalse()
301301
$address = new Address(['city' => 'London']);
302302

303303
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
304-
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything());
304+
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any());
305305
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true);
306306
$events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(false);
307307

@@ -316,7 +316,7 @@ public function testEmbedsManySavingEventReturnsFalse()
316316
$address->exists = true;
317317

318318
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
319-
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything());
319+
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any());
320320
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(false);
321321

322322
$this->assertFalse($user->addresses()->save($address));
@@ -330,7 +330,7 @@ public function testEmbedsManyUpdatingEventReturnsFalse()
330330
$user->addresses()->save($address);
331331

332332
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
333-
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything());
333+
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any());
334334
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true);
335335
$events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(false);
336336

@@ -348,7 +348,7 @@ public function testEmbedsManyDeletingEventReturnsFalse()
348348
$address = $user->addresses->first();
349349

350350
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
351-
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything());
351+
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any());
352352
$events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::mustBe($address))->andReturn(false);
353353

354354
$this->assertEquals(0, $user->addresses()->destroy($address));
@@ -452,7 +452,7 @@ public function testEmbedsOne()
452452
$father = new User(['name' => 'Mark Doe']);
453453

454454
$father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
455-
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), anything());
455+
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), Mockery::any());
456456
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true);
457457
$events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true);
458458
$events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($father), $father);
@@ -472,7 +472,7 @@ public function testEmbedsOne()
472472
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']);
473473

474474
$father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
475-
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), anything());
475+
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), Mockery::any());
476476
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true);
477477
$events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($father), $father)->andReturn(true);
478478
$events->shouldReceive('fire')->once()->with('eloquent.updated: ' . get_class($father), $father);
@@ -488,7 +488,7 @@ public function testEmbedsOne()
488488
$father = new User(['name' => 'Jim Doe']);
489489

490490
$father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
491-
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), anything());
491+
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), Mockery::any());
492492
$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true);
493493
$events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true);
494494
$events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($father), $father);
@@ -507,7 +507,7 @@ public function testEmbedsOneAssociate()
507507
$father = new User(['name' => 'Mark Doe']);
508508

509509
$father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
510-
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), anything());
510+
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), Mockery::any());
511511
$events->shouldReceive('until')->times(0)->with('eloquent.saving: ' . get_class($father), $father);
512512

513513
$father = $user->father()->associate($father);

tests/config/database.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
'mongodb' => [
88
'name' => 'mongodb',
99
'driver' => 'mongodb',
10-
'host' => '127.0.0.1',
10+
'host' => 'mongodb',
1111
'database' => 'unittest',
1212
],
1313

1414
'mysql' => [
1515
'driver' => 'mysql',
16-
'host' => '127.0.0.1',
16+
'host' => 'mysql',
1717
'database' => 'unittest',
18-
'username' => 'travis',
18+
'username' => 'root',
1919
'password' => '',
2020
'charset' => 'utf8',
2121
'collation' => 'utf8_unicode_ci',

tests/models/User.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
22

33
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
4+
use Jenssegers\Mongodb\Eloquent\HybridRelations;
45
use Illuminate\Auth\Authenticatable;
56
use Illuminate\Auth\Passwords\CanResetPassword;
67
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
78
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
89

910
class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract
1011
{
11-
use Authenticatable, CanResetPassword;
12+
use Authenticatable, CanResetPassword, HybridRelations;
1213

1314
protected $connection = 'mongodb';
1415
protected $dates = ['birthday', 'entry.date'];

0 commit comments

Comments
 (0)