Skip to content

Commit 6ffda75

Browse files
committed
Merge remote-tracking branch 'upstream/master' into patch-1
2 parents 3f58809 + 01ac069 commit 6ffda75

File tree

12 files changed

+108
-26
lines changed

12 files changed

+108
-26
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ User::where('name', 'Jaques')->decrement('weight', 50);
484484
The number of updated objects is returned:
485485

486486
```php
487-
$count = User->increment('age');
487+
$count = User::increment('age');
488488
```
489489

490490
You may also specify additional columns to update:

phpunit.xml.dist

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,40 @@
1515
<directory>tests/</directory>
1616
</testsuite>
1717
<testsuite name="schema">
18-
<directory>tests/SchemaTest.php</directory>
18+
<file>tests/SchemaTest.php</file>
1919
</testsuite>
2020
<testsuite name="seeder">
21-
<directory>tests/SeederTest.php</directory>
21+
<file>tests/SeederTest.php</file>
2222
</testsuite>
2323
<testsuite name="cache">
24-
<directory>tests/CacheTest.php</directory>
24+
<file>tests/CacheTest.php</file>
2525
</testsuite>
2626
<testsuite name="builder">
27-
<directory>tests/QueryBuilderTest.php</directory>
28-
<directory>tests/QueryTest.php</directory>
27+
<file>tests/QueryBuilderTest.php</file>
28+
<file>tests/QueryTest.php</file>
2929
</testsuite>
3030
<testsuite name="model">
31-
<directory>tests/ModelTest.php</directory>
32-
<directory>tests/RelationsTest.php</directory>
31+
<file>tests/ModelTest.php</file>
32+
<file>tests/RelationsTest.php</file>
3333
</testsuite>
3434
<testsuite name="relations">
35-
<directory>tests/RelationsTest.php</directory>
36-
<directory>tests/EmbeddedRelationsTest.php</directory>
35+
<file>tests/RelationsTest.php</file>
36+
<file>tests/EmbeddedRelationsTest.php</file>
3737
</testsuite>
3838
<testsuite name="mysqlrelations">
39-
<directory>tests/RelationsTest.php</directory>
39+
<file>tests/RelationsTest.php</file>
4040
</testsuite>
4141
<testsuite name="validation">
42-
<directory>tests/ValidationTest.php</directory>
42+
<file>tests/ValidationTest.php</file>
4343
</testsuite>
4444
</testsuites>
45+
<php>
46+
<env name="MONGO_HOST" value="mongodb"/>
47+
<env name="MONGO_DATABASE" value="unittest"/>
48+
<env name="MONGO_PORT" value="27017"/>
49+
<env name="MYSQL_HOST" value="mysql"/>
50+
<env name="MYSQL_DATABASE" value="unittest"/>
51+
<env name="MYSQL_USERNAME" value="root"/>
52+
<env name="QUEUE_CONNECTION" value="database"/>
53+
</php>
4554
</phpunit>

src/Jenssegers/Mongodb/Eloquent/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function update(array $values, array $options = [])
4444
return 1;
4545
}
4646

47-
return $this->query->update($this->addUpdatedAtColumn($values), $options);
47+
return $this->toBase()->update($this->addUpdatedAtColumn($values), $options);
4848
}
4949

5050
/**

src/Jenssegers/Mongodb/Eloquent/Model.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ abstract class Model extends BaseModel
3232
* @var string
3333
*/
3434
protected $primaryKey = '_id';
35-
35+
3636
/**
3737
* The primary key type.
3838
*
@@ -175,7 +175,7 @@ protected function getAttributeFromArray($key)
175175
public function setAttribute($key, $value)
176176
{
177177
// Convert _id to ObjectID.
178-
if (($key == '_id' || Str::endsWith($key, '_id')) && is_string($value)) {
178+
if ($key == '_id' && is_string($value)) {
179179
$builder = $this->newBaseQueryBuilder();
180180

181181
$value = $builder->convertKey($value);

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,16 @@ public function find($id, $columns = [])
204204
return $this->where('_id', '=', $this->convertKey($id))->first($columns);
205205
}
206206

207+
/**
208+
* @inheritdoc
209+
*/
210+
public function value($column)
211+
{
212+
$result = (array) $this->first([$column]);
213+
214+
return Arr::get($result, $column);
215+
}
216+
207217
/**
208218
* @inheritdoc
209219
*/
@@ -844,7 +854,7 @@ public function convertKey($id)
844854
{
845855
if (is_string($id) && strlen($id) === 24 && ctype_xdigit($id)) {
846856
return new ObjectID($id);
847-
} elseif (strlen($id) === 16 && preg_match('~[^\x20-\x7E\t\r\n]~', $id) > 0) {
857+
} elseif (is_string($id) && strlen($id) === 16 && preg_match('~[^\x20-\x7E\t\r\n]~', $id) > 0) {
848858
return new Binary($id, Binary::TYPE_UUID);
849859
}
850860

@@ -906,7 +916,7 @@ protected function compileWheres()
906916
}
907917

908918
// Convert id's.
909-
if (isset($where['column']) && ($where['column'] == '_id' || Str::endsWith($where['column'], '_id'))) {
919+
if (isset($where['column']) && ($where['column'] == '_id' || Str::endsWith($where['column'], '._id'))) {
910920
// Multiple values.
911921
if (isset($where['values'])) {
912922
foreach ($where['values'] as &$value) {

tests/QueryBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,4 +701,16 @@ public function testProjections()
701701
$this->assertEquals(1, count($result['tags']));
702702
}
703703
}
704+
705+
public function testValue()
706+
{
707+
DB::collection('books')->insert([
708+
['title' => 'Moby-Dick', 'author' => ['first_name' => 'Herman', 'last_name' => 'Melville']]
709+
]);
710+
711+
$this->assertEquals('Moby-Dick', DB::collection('books')->value('title'));
712+
$this->assertEquals(['first_name' => 'Herman', 'last_name' => 'Melville'], DB::collection('books')->value('author'));
713+
$this->assertEquals('Herman', DB::collection('books')->value('author.first_name'));
714+
$this->assertEquals('Melville', DB::collection('books')->value('author.last_name'));
715+
}
704716
}

tests/QueryTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function setUp(): void
2121
public function tearDown(): void
2222
{
2323
User::truncate();
24+
Scoped::truncate();
2425
parent::tearDown();
2526
}
2627

@@ -309,4 +310,21 @@ public function testPaginate()
309310
$this->assertEquals(9, $results->total());
310311
$this->assertEquals(1, $results->currentPage());
311312
}
313+
314+
public function testUpdate()
315+
{
316+
$this->assertEquals(1, User::where(['name' => 'John Doe'])->update(['name' => 'Jim Morrison']));
317+
$this->assertEquals(1, User::where(['name' => 'Jim Morrison'])->count());
318+
319+
Scoped::create(['favorite' => true]);
320+
Scoped::create(['favorite' => false]);
321+
322+
$this->assertCount(1, Scoped::get());
323+
$this->assertEquals(1, Scoped::query()->update(['name' => 'Johnny']));
324+
$this->assertCount(1, Scoped::withoutGlobalScopes()->where(['name' => 'Johnny'])->get());
325+
326+
$this->assertCount(2, Scoped::withoutGlobalScopes()->get());
327+
$this->assertEquals(2, Scoped::withoutGlobalScopes()->update(['name' => 'Jimmy']));
328+
$this->assertCount(2, Scoped::withoutGlobalScopes()->where(['name' => 'Jimmy'])->get());
329+
}
312330
}

tests/QueueTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function testQueueJobLifeCycle()
2424
'displayName' => 'test',
2525
'job' => 'test',
2626
'maxTries' => null,
27+
'delay' => null,
2728
'timeout' => null,
2829
'data' => ['action' => 'QueueJobLifeCycle'],
2930
]), $job->getRawBody());

tests/config/database.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
<?php
22

3+
$mongoHost = env('MONGO_HOST', 'mongodb');
4+
$mongoPort = env('MONGO_PORT') ? (int) env('MONGO_PORT') : 27017;
5+
36
return [
47

58
'connections' => [
69

710
'mongodb' => [
811
'name' => 'mongodb',
912
'driver' => 'mongodb',
10-
'host' => 'mongodb',
11-
'database' => 'unittest',
13+
'host' => $mongoHost,
14+
'database' => env('MONGO_DATABASE', 'unittest'),
1215
],
1316

1417
'dsn_mongodb' => [
1518
'driver' => 'mongodb',
16-
'dsn' => 'mongodb://mongodb:27017',
17-
'database' => 'unittest',
19+
'dsn' => "mongodb://$mongoHost:$mongoPort",
20+
'database' => env('MONGO_DATABASE', 'unittest'),
1821
],
1922

2023
'mysql' => [
2124
'driver' => 'mysql',
22-
'host' => 'mysql',
23-
'database' => 'unittest',
24-
'username' => 'root',
25+
'host' => env('MYSQL_HOST', 'mysql'),
26+
'database' => env('MYSQL_DATABASE', 'unittest'),
27+
'username' => env('MYSQL_USERNAME', 'root'),
2528
'password' => '',
2629
'charset' => 'utf8',
2730
'collation' => 'utf8_unicode_ci',

tests/config/queue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
return [
44

5-
'default' => 'database',
5+
'default' => env('QUEUE_CONNECTION'),
66

77
'connections' => [
88

@@ -16,7 +16,7 @@
1616
],
1717

1818
'failed' => [
19-
'database' => 'mongodb',
19+
'database' => env('MONGO_DATABASE'),
2020
'table' => 'failed_jobs',
2121
],
2222

tests/models/Scoped.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
4+
use Jenssegers\Mongodb\Eloquent\Builder;
5+
6+
class Scoped extends Eloquent
7+
{
8+
protected $connection = 'mongodb';
9+
protected $collection = 'scoped';
10+
protected $fillable = ['name', 'favorite'];
11+
12+
protected static function boot()
13+
{
14+
parent::boot();
15+
16+
static::addGlobalScope('favorite', function (Builder $builder) {
17+
$builder->where('favorite', true);
18+
});
19+
}
20+
}

0 commit comments

Comments
 (0)