Skip to content

Commit 731dfd0

Browse files
committed
Merge branch '10.x' into 11.x
# Conflicts: # CHANGELOG.md # src/Illuminate/Foundation/Application.php
2 parents f57756a + 8e9ab6d commit 731dfd0

File tree

6 files changed

+75
-7
lines changed

6 files changed

+75
-7
lines changed

src/Illuminate/Database/Eloquent/Relations/BelongsTo.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Database\Eloquent\Relations;
44

5+
use BackedEnum;
56
use Illuminate\Database\Eloquent\Builder;
67
use Illuminate\Database\Eloquent\Collection;
78
use Illuminate\Database\Eloquent\Model;
@@ -375,7 +376,9 @@ protected function getRelatedKeyFrom(Model $model)
375376
*/
376377
protected function getForeignKeyFrom(Model $model)
377378
{
378-
return $model->{$this->foreignKey};
379+
$foreignKey = $model->{$this->foreignKey};
380+
381+
return $foreignKey instanceof BackedEnum ? $foreignKey->value : $foreignKey;
379382
}
380383

381384
/**

src/Illuminate/Database/Query/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ public function whereIntegerInRaw($column, $values, $boolean = 'and', $not = fal
12221222
$values = Arr::flatten($values);
12231223

12241224
foreach ($values as &$value) {
1225-
$value = (int) $value;
1225+
$value = (int) ($value instanceof BackedEnum ? $value->value : $value);
12261226
}
12271227

12281228
$this->wheres[] = compact('type', 'column', 'values', 'boolean');

tests/Database/DatabaseEloquentBelongsToTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Database\Eloquent\Collection;
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9+
use Illuminate\Tests\Database\Fixtures\Enums\Bar;
910
use Mockery as m;
1011
use PHPUnit\Framework\TestCase;
1112

@@ -85,6 +86,16 @@ public function testIdsInEagerConstraintsCanBeZero()
8586
$relation->addEagerConstraints($models);
8687
}
8788

89+
public function testIdsInEagerConstraintsCanBeBackedEnum()
90+
{
91+
$relation = $this->getRelation();
92+
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
93+
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
94+
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('relation.id', [5, 'foreign.value']);
95+
$models = [new EloquentBelongsToModelStub, new EloquentBelongsToModelStubWithBackedEnumCast];
96+
$relation->addEagerConstraints($models);
97+
}
98+
8899
public function testRelationIsProperlyInitialized()
89100
{
90101
$relation = $this->getRelation();
@@ -119,6 +130,15 @@ public function __toString()
119130
}
120131
};
121132

133+
$result4 = new class extends Model
134+
{
135+
protected $casts = [
136+
'id' => Bar::class,
137+
];
138+
139+
protected $attributes = ['id' => 5];
140+
};
141+
122142
$model1 = new EloquentBelongsToModelStub;
123143
$model1->foreign_key = 1;
124144
$model2 = new EloquentBelongsToModelStub;
@@ -131,11 +151,18 @@ public function __toString()
131151
return '3';
132152
}
133153
};
134-
$models = $relation->match([$model1, $model2, $model3], new Collection([$result1, $result2, $result3]), 'foo');
154+
$model4 = new EloquentBelongsToModelStub;
155+
$model4->foreign_key = 5;
156+
$models = $relation->match(
157+
[$model1, $model2, $model3, $model4],
158+
new Collection([$result1, $result2, $result3, $result4]),
159+
'foo'
160+
);
135161

136162
$this->assertEquals(1, $models[0]->foo->getAttribute('id'));
137163
$this->assertEquals(2, $models[1]->foo->getAttribute('id'));
138164
$this->assertSame('3', (string) $models[2]->foo->getAttribute('id'));
165+
$this->assertEquals(5, $models[3]->foo->getAttribute('id')->value);
139166
}
140167

141168
public function testAssociateMethodSetsForeignKeyOnModel()
@@ -403,3 +430,14 @@ class MissingEloquentBelongsToModelStub extends Model
403430
{
404431
public $foreign_key;
405432
}
433+
434+
class EloquentBelongsToModelStubWithBackedEnumCast extends Model
435+
{
436+
protected $casts = [
437+
'foreign_key' => Bar::class,
438+
];
439+
440+
public $attributes = [
441+
'foreign_key' => 5,
442+
];
443+
}

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Illuminate\Pagination\Cursor;
2525
use Illuminate\Pagination\CursorPaginator;
2626
use Illuminate\Pagination\LengthAwarePaginator;
27+
use Illuminate\Tests\Database\Fixtures\Enums\Bar;
2728
use InvalidArgumentException;
2829
use Mockery as m;
2930
use PHPUnit\Framework\TestCase;
@@ -1042,17 +1043,20 @@ public function testEmptyWhereNotIns()
10421043
public function testWhereIntegerInRaw()
10431044
{
10441045
$builder = $this->getBuilder();
1045-
$builder->select('*')->from('users')->whereIntegerInRaw('id', ['1a', 2]);
1046-
$this->assertSame('select * from "users" where "id" in (1, 2)', $builder->toSql());
1046+
$builder->select('*')->from('users')->whereIntegerInRaw('id', [
1047+
'1a', 2, Bar::FOO,
1048+
]);
1049+
$this->assertSame('select * from "users" where "id" in (1, 2, 5)', $builder->toSql());
10471050
$this->assertEquals([], $builder->getBindings());
10481051

10491052
$builder = $this->getBuilder();
10501053
$builder->select('*')->from('users')->whereIntegerInRaw('id', [
10511054
['id' => '1a'],
10521055
['id' => 2],
10531056
['any' => '3'],
1057+
['id' => Bar::FOO],
10541058
]);
1055-
$this->assertSame('select * from "users" where "id" in (1, 2, 3)', $builder->toSql());
1059+
$this->assertSame('select * from "users" where "id" in (1, 2, 3, 5)', $builder->toSql());
10561060
$this->assertEquals([], $builder->getBindings());
10571061
}
10581062

tests/Database/Fixtures/Enums/Bar.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Database\Fixtures\Enums;
4+
5+
enum Bar: int
6+
{
7+
case FOO = 5;
8+
}

tests/Database/QueryDurationThresholdTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,24 @@
66
use Illuminate\Database\Connection;
77
use Illuminate\Events\Dispatcher;
88
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Carbon;
910
use PDO;
1011
use PHPUnit\Framework\TestCase;
1112

1213
class QueryDurationThresholdTest extends TestCase
1314
{
15+
/**
16+
* @var \Illuminate\Support\Carbon
17+
*/
18+
protected $now;
19+
20+
protected function tearDown(): void
21+
{
22+
Carbon::setTestNow(null);
23+
24+
parent::tearDown();
25+
}
26+
1427
public function testItCanHandleReachingADurationThresholdInTheDb()
1528
{
1629
$connection = new Connection(new PDO('sqlite::memory:'));
@@ -46,10 +59,12 @@ public function testItIsOnlyCalledOnce()
4659

4760
public function testItIsOnlyCalledOnceWhenGivenDateTime()
4861
{
62+
Carbon::setTestNow($this->now = Carbon::create(2017, 6, 27, 13, 14, 15, 'UTC'));
63+
4964
$connection = new Connection(new PDO('sqlite::memory:'));
5065
$connection->setEventDispatcher(new Dispatcher());
5166
$called = 0;
52-
$connection->whenQueryingForLongerThan(now()->addMilliseconds(1), function () use (&$called) {
67+
$connection->whenQueryingForLongerThan($this->now->addMilliseconds(1), function () use (&$called) {
5368
$called++;
5469
});
5570

0 commit comments

Comments
 (0)