Skip to content

Commit 7bcf724

Browse files
Merge branch '8.x' into 9.x
2 parents fef0260 + 30e341b commit 7bcf724

File tree

7 files changed

+130
-9
lines changed

7 files changed

+130
-9
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,10 @@ public function getRelationValue($key)
500500
*/
501501
public function isRelation($key)
502502
{
503+
if ($this->hasAttributeMutator($key)) {
504+
return false;
505+
}
506+
503507
return method_exists($this, $key) ||
504508
(static::$relationResolvers[get_class($this)][$key] ?? null);
505509
}

src/Illuminate/Foundation/Bootstrap/HandleExceptions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ public function bootstrap(Application $app)
6767
*/
6868
public function handleError($level, $message, $file = '', $line = 0, $context = [])
6969
{
70-
if (error_reporting() & $level) {
71-
if ($this->isDeprecation($level)) {
72-
return $this->handleDeprecation($message, $file, $line);
73-
}
70+
if ($this->isDeprecation($level)) {
71+
return $this->handleDeprecation($message, $file, $line);
72+
}
7473

74+
if (error_reporting() & $level) {
7575
throw new ErrorException($message, 0, $level, $file, $line);
7676
}
7777
}

src/Illuminate/Foundation/Testing/Concerns/InteractsWithDeprecationHandling.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function withoutDeprecationHandling()
3838
{
3939
if ($this->originalDeprecationHandler == null) {
4040
$this->originalDeprecationHandler = set_error_handler(function ($level, $message, $file = '', $line = 0) {
41-
if (error_reporting() & $level) {
41+
if (in_array($level, [E_DEPRECATED, E_USER_DEPRECATED]) || (error_reporting() & $level)) {
4242
throw new ErrorException($message, 0, $level, $file, $line);
4343
}
4444
});

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Http\Client\Events\ConnectionFailed;
1212
use Illuminate\Http\Client\Events\RequestSending;
1313
use Illuminate\Http\Client\Events\ResponseReceived;
14+
use Illuminate\Support\Arr;
1415
use Illuminate\Support\Collection;
1516
use Illuminate\Support\Str;
1617
use Illuminate\Support\Traits\Conditionable;
@@ -155,6 +156,20 @@ class PendingRequest
155156
*/
156157
protected $request;
157158

159+
/**
160+
* The Guzzle request options that are mergable via array_merge_recursive.
161+
*
162+
* @var array
163+
*/
164+
protected $mergableOptions = [
165+
'cookies',
166+
'form_params',
167+
'headers',
168+
'json',
169+
'multipart',
170+
'query',
171+
];
172+
158173
/**
159174
* Create a new HTTP Client instance.
160175
*
@@ -488,15 +503,18 @@ public function retry(int $times, int $sleep = 0, ?callable $when = null, bool $
488503
}
489504

490505
/**
491-
* Merge new options into the client.
506+
* Replace the specified options on the request.
492507
*
493508
* @param array $options
494509
* @return $this
495510
*/
496511
public function withOptions(array $options)
497512
{
498513
return tap($this, function ($request) use ($options) {
499-
return $this->options = array_merge_recursive($this->options, $options);
514+
return $this->options = array_replace_recursive(
515+
array_merge_recursive($this->options, Arr::only($options, $this->mergableOptions)),
516+
$options
517+
);
500518
});
501519
}
502520

@@ -1018,14 +1036,17 @@ public function runBeforeSendingCallbacks($request, array $options)
10181036
}
10191037

10201038
/**
1021-
* Merge the given options with the current request options.
1039+
* Replace the given options with the current request options.
10221040
*
10231041
* @param array $options
10241042
* @return array
10251043
*/
10261044
public function mergeOptions(...$options)
10271045
{
1028-
return array_merge_recursive($this->options, ...$options);
1046+
return array_replace_recursive(
1047+
array_merge_recursive($this->options, Arr::only($options, $this->mergableOptions)),
1048+
...$options
1049+
);
10291050
}
10301051

10311052
/**
@@ -1131,4 +1152,14 @@ public function setHandler($handler)
11311152

11321153
return $this;
11331154
}
1155+
1156+
/**
1157+
* Get the pending request options.
1158+
*
1159+
* @return array
1160+
*/
1161+
public function getOptions()
1162+
{
1163+
return $this->options;
1164+
}
11341165
}

tests/Database/DatabaseEloquentRelationTest.php

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

55
use Exception;
66
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Database\Eloquent\Casts\Attribute;
78
use Illuminate\Database\Eloquent\Collection;
89
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Database\Eloquent\Relations\HasOne;
@@ -281,6 +282,14 @@ public function testRelationResolvers()
281282
$this->assertInstanceOf(EloquentResolverRelationStub::class, $model->customer());
282283
$this->assertSame(['key' => 'value'], $model->customer);
283284
}
285+
286+
public function testIsRelationIgnoresAttribute()
287+
{
288+
$model = new EloquentRelationAndAtrributeModelStub;
289+
290+
$this->assertTrue($model->isRelation('parent'));
291+
$this->assertFalse($model->isRelation('field'));
292+
}
284293
}
285294

286295
class EloquentRelationResetModelStub extends Model
@@ -351,3 +360,25 @@ public function getResults()
351360
return ['key' => 'value'];
352361
}
353362
}
363+
364+
class EloquentRelationAndAtrributeModelStub extends Model
365+
{
366+
protected $table = 'one_more_table';
367+
368+
public function field(): Attribute
369+
{
370+
return new Attribute(
371+
function ($value) {
372+
return $value;
373+
},
374+
function ($value) {
375+
return $value;
376+
},
377+
);
378+
}
379+
380+
public function parent()
381+
{
382+
return $this->belongsTo(self::class);
383+
}
384+
}

tests/Http/HttpClientTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,19 @@ public function testClientCanBeSet()
971971
$this->assertSame($client, $request->buildClient());
972972
}
973973

974+
public function testRequestsCanReplaceOptions()
975+
{
976+
$request = new PendingRequest($this->factory);
977+
978+
$request = $request->withOptions(['http_errors' => true, 'connect_timeout' => 10]);
979+
980+
$this->assertSame(['http_errors' => true, 'connect_timeout' => 10], $request->getOptions());
981+
982+
$request = $request->withOptions(['connect_timeout' => 20]);
983+
984+
$this->assertSame(['http_errors' => true, 'connect_timeout' => 20], $request->getOptions());
985+
}
986+
974987
public function testMultipleRequestsAreSentInThePool()
975988
{
976989
$this->factory->fake([

tests/Integration/Database/EloquentBelongsToManyTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
4646
});
4747

4848
Schema::create('users_posts', function (Blueprint $table) {
49+
$table->increments('id');
4950
$table->string('user_uuid');
5051
$table->string('post_uuid');
5152
$table->tinyInteger('is_draft')->default(1);
@@ -1014,6 +1015,33 @@ public function testOrderByPivotMethod()
10141015
$relationTag2 = $post->tagsWithCustomExtraPivot()->orderByPivot('flag', 'desc')->first();
10151016
$this->assertEquals($relationTag2->getAttributes(), $tag3->getAttributes());
10161017
}
1018+
1019+
public function testFirstOrMethod()
1020+
{
1021+
$user1 = User::create(['name' => Str::random()]);
1022+
$user2 = User::create(['name' => Str::random()]);
1023+
$user3 = User::create(['name' => Str::random()]);
1024+
$post1 = Post::create(['title' => Str::random()]);
1025+
$post2 = Post::create(['title' => Str::random()]);
1026+
$post3 = Post::create(['title' => Str::random()]);
1027+
1028+
$user1->posts()->sync([$post1->uuid, $post2->uuid]);
1029+
$user2->posts()->sync([$post1->uuid, $post2->uuid]);
1030+
1031+
$this->assertEquals(
1032+
$post1->id,
1033+
$user2->posts()->firstOr(function () {
1034+
return Post::create(['title' => Str::random()]);
1035+
})->id
1036+
);
1037+
1038+
$this->assertEquals(
1039+
$post3->id,
1040+
$user3->posts()->firstOr(function () use ($post3) {
1041+
return $post3;
1042+
})->id
1043+
);
1044+
}
10171045
}
10181046

10191047
class User extends Model
@@ -1031,6 +1059,13 @@ protected static function boot()
10311059
});
10321060
}
10331061

1062+
public function posts()
1063+
{
1064+
return $this->belongsToMany(Post::class, 'users_posts', 'user_uuid', 'post_uuid', 'uuid', 'uuid')
1065+
->withPivot('is_draft')
1066+
->withTimestamps();
1067+
}
1068+
10341069
public function postsWithCustomPivot()
10351070
{
10361071
return $this->belongsToMany(Post::class, 'users_posts', 'user_uuid', 'post_uuid', 'uuid', 'uuid')
@@ -1056,6 +1091,13 @@ protected static function boot()
10561091
});
10571092
}
10581093

1094+
public function users()
1095+
{
1096+
return $this->belongsToMany(User::class, 'users_posts', 'post_uuid', 'user_uuid', 'uuid', 'uuid')
1097+
->withPivot('is_draft')
1098+
->withTimestamps();
1099+
}
1100+
10591101
public function tags()
10601102
{
10611103
return $this->belongsToMany(Tag::class, 'posts_tags', 'post_id', 'tag_id')

0 commit comments

Comments
 (0)