Skip to content

Commit

Permalink
Merge branch '10.x' into 11.x
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	src/Illuminate/Foundation/Application.php
  • Loading branch information
driesvints committed Apr 16, 2024
2 parents f57756a + 8e9ab6d commit 731dfd0
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Eloquent\Relations;

use BackedEnum;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -375,7 +376,9 @@ protected function getRelatedKeyFrom(Model $model)
*/
protected function getForeignKeyFrom(Model $model)
{
return $model->{$this->foreignKey};
$foreignKey = $model->{$this->foreignKey};

return $foreignKey instanceof BackedEnum ? $foreignKey->value : $foreignKey;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ public function whereIntegerInRaw($column, $values, $boolean = 'and', $not = fal
$values = Arr::flatten($values);

foreach ($values as &$value) {
$value = (int) $value;
$value = (int) ($value instanceof BackedEnum ? $value->value : $value);
}

$this->wheres[] = compact('type', 'column', 'values', 'boolean');
Expand Down
40 changes: 39 additions & 1 deletion tests/Database/DatabaseEloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Tests\Database\Fixtures\Enums\Bar;
use Mockery as m;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -85,6 +86,16 @@ public function testIdsInEagerConstraintsCanBeZero()
$relation->addEagerConstraints($models);
}

public function testIdsInEagerConstraintsCanBeBackedEnum()
{
$relation = $this->getRelation();
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('relation.id', [5, 'foreign.value']);
$models = [new EloquentBelongsToModelStub, new EloquentBelongsToModelStubWithBackedEnumCast];
$relation->addEagerConstraints($models);
}

public function testRelationIsProperlyInitialized()
{
$relation = $this->getRelation();
Expand Down Expand Up @@ -119,6 +130,15 @@ public function __toString()
}
};

$result4 = new class extends Model
{
protected $casts = [
'id' => Bar::class,
];

protected $attributes = ['id' => 5];
};

$model1 = new EloquentBelongsToModelStub;
$model1->foreign_key = 1;
$model2 = new EloquentBelongsToModelStub;
Expand All @@ -131,11 +151,18 @@ public function __toString()
return '3';
}
};
$models = $relation->match([$model1, $model2, $model3], new Collection([$result1, $result2, $result3]), 'foo');
$model4 = new EloquentBelongsToModelStub;
$model4->foreign_key = 5;
$models = $relation->match(
[$model1, $model2, $model3, $model4],
new Collection([$result1, $result2, $result3, $result4]),
'foo'
);

$this->assertEquals(1, $models[0]->foo->getAttribute('id'));
$this->assertEquals(2, $models[1]->foo->getAttribute('id'));
$this->assertSame('3', (string) $models[2]->foo->getAttribute('id'));
$this->assertEquals(5, $models[3]->foo->getAttribute('id')->value);
}

public function testAssociateMethodSetsForeignKeyOnModel()
Expand Down Expand Up @@ -403,3 +430,14 @@ class MissingEloquentBelongsToModelStub extends Model
{
public $foreign_key;
}

class EloquentBelongsToModelStubWithBackedEnumCast extends Model
{
protected $casts = [
'foreign_key' => Bar::class,
];

public $attributes = [
'foreign_key' => 5,
];
}
10 changes: 7 additions & 3 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Illuminate\Pagination\Cursor;
use Illuminate\Pagination\CursorPaginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Tests\Database\Fixtures\Enums\Bar;
use InvalidArgumentException;
use Mockery as m;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -1042,17 +1043,20 @@ public function testEmptyWhereNotIns()
public function testWhereIntegerInRaw()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereIntegerInRaw('id', ['1a', 2]);
$this->assertSame('select * from "users" where "id" in (1, 2)', $builder->toSql());
$builder->select('*')->from('users')->whereIntegerInRaw('id', [
'1a', 2, Bar::FOO,
]);
$this->assertSame('select * from "users" where "id" in (1, 2, 5)', $builder->toSql());
$this->assertEquals([], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereIntegerInRaw('id', [
['id' => '1a'],
['id' => 2],
['any' => '3'],
['id' => Bar::FOO],
]);
$this->assertSame('select * from "users" where "id" in (1, 2, 3)', $builder->toSql());
$this->assertSame('select * from "users" where "id" in (1, 2, 3, 5)', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Database/Fixtures/Enums/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Tests\Database\Fixtures\Enums;

enum Bar: int
{
case FOO = 5;
}
17 changes: 16 additions & 1 deletion tests/Database/QueryDurationThresholdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@
use Illuminate\Database\Connection;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use PDO;
use PHPUnit\Framework\TestCase;

class QueryDurationThresholdTest extends TestCase
{
/**
* @var \Illuminate\Support\Carbon
*/
protected $now;

protected function tearDown(): void
{
Carbon::setTestNow(null);

parent::tearDown();
}

public function testItCanHandleReachingADurationThresholdInTheDb()
{
$connection = new Connection(new PDO('sqlite::memory:'));
Expand Down Expand Up @@ -46,10 +59,12 @@ public function testItIsOnlyCalledOnce()

public function testItIsOnlyCalledOnceWhenGivenDateTime()
{
Carbon::setTestNow($this->now = Carbon::create(2017, 6, 27, 13, 14, 15, 'UTC'));

$connection = new Connection(new PDO('sqlite::memory:'));
$connection->setEventDispatcher(new Dispatcher());
$called = 0;
$connection->whenQueryingForLongerThan(now()->addMilliseconds(1), function () use (&$called) {
$connection->whenQueryingForLongerThan($this->now->addMilliseconds(1), function () use (&$called) {
$called++;
});

Expand Down

0 comments on commit 731dfd0

Please sign in to comment.