Skip to content

Commit d635d2f

Browse files
authored
Merge branch 'laravel:8.x' into 8.x
2 parents ff9d29e + fc2cd95 commit d635d2f

File tree

16 files changed

+277
-23
lines changed

16 files changed

+277
-23
lines changed

src/Illuminate/Events/Dispatcher.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,13 @@ public function subscribe($subscriber)
182182

183183
if (is_array($events)) {
184184
foreach ($events as $event => $listeners) {
185-
foreach ($listeners as $listener) {
185+
foreach (Arr::wrap($listeners) as $listener) {
186+
if (is_string($listener) && method_exists($subscriber, $listener)) {
187+
$this->listen($event, [get_class($subscriber), $listener]);
188+
189+
continue;
190+
}
191+
186192
$this->listen($event, $listener);
187193
}
188194
}

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -790,20 +790,42 @@ protected function populateResponse(Response $response)
790790
*/
791791
public function buildClient()
792792
{
793-
return $this->client = $this->client ?: new Client([
794-
'handler' => $this->buildHandlerStack(),
793+
return $this->client = $this->client ?: $this->createClient($this->buildHandlerStack());
794+
}
795+
796+
/**
797+
* Create new Guzzle client.
798+
*
799+
* @param \GuzzleHttp\HandlerStack $handlerStack
800+
* @return \GuzzleHttp\Client
801+
*/
802+
public function createClient($handlerStack)
803+
{
804+
return new Client([
805+
'handler' => $handlerStack,
795806
'cookies' => true,
796807
]);
797808
}
798809

799810
/**
800-
* Build the before sending handler stack.
811+
* Build the Guzzle client handler stack.
801812
*
802813
* @return \GuzzleHttp\HandlerStack
803814
*/
804815
public function buildHandlerStack()
805816
{
806-
return tap(HandlerStack::create(), function ($stack) {
817+
return $this->pushHandlers(HandlerStack::create());
818+
}
819+
820+
/**
821+
* Add the necessary handlers to the given handler stack.
822+
*
823+
* @param \GuzzleHttp\HandlerStack $handlerStack
824+
* @return \GuzzleHttp\HandlerStack
825+
*/
826+
public function pushHandlers($handlerStack)
827+
{
828+
return tap($handlerStack, function ($stack) {
807829
$stack->push($this->buildBeforeSendingHandler());
808830
$stack->push($this->buildRecorderHandler());
809831
$stack->push($this->buildStubHandler());
@@ -1022,4 +1044,19 @@ public function setClient(Client $client)
10221044

10231045
return $this;
10241046
}
1047+
1048+
/**
1049+
* Create a new client instance using the given handler.
1050+
*
1051+
* @param callable $handler
1052+
* @return $this
1053+
*/
1054+
public function setHandler($handler)
1055+
{
1056+
$this->client = $this->createClient(
1057+
$this->pushHandlers(HandlerStack::create($handler))
1058+
);
1059+
1060+
return $this;
1061+
}
10251062
}

src/Illuminate/Http/Client/Pool.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Illuminate\Http\Client;
44

5+
use GuzzleHttp\Utils;
6+
57
/**
68
* @mixin \Illuminate\Http\Client\Factory
79
*/
@@ -15,11 +17,11 @@ class Pool
1517
protected $factory;
1618

1719
/**
18-
* The client instance.
20+
* The handler function for the Guzzle client.
1921
*
20-
* @var \GuzzleHttp\Client
22+
* @var callable
2123
*/
22-
protected $client;
24+
protected $handler;
2325

2426
/**
2527
* The pool of requests.
@@ -38,7 +40,11 @@ public function __construct(Factory $factory = null)
3840
{
3941
$this->factory = $factory ?: new Factory();
4042

41-
$this->client = $this->factory->buildClient();
43+
if (method_exists(Utils::class, 'chooseHandler')) {
44+
$this->handler = Utils::chooseHandler();
45+
} else {
46+
$this->handler = \GuzzleHttp\choose_handler();
47+
}
4248
}
4349

4450
/**
@@ -59,7 +65,7 @@ public function as(string $key)
5965
*/
6066
protected function asyncRequest()
6167
{
62-
return $this->factory->setClient($this->client)->async();
68+
return $this->factory->setHandler($this->handler)->async();
6369
}
6470

6571
/**

src/Illuminate/Http/Middleware/TrustProxies.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TrustProxies
1010
/**
1111
* The trusted proxies for the application.
1212
*
13-
* @var string|array|null
13+
* @var array|string|null
1414
*/
1515
protected $proxies;
1616

@@ -47,7 +47,7 @@ public function handle(Request $request, Closure $next)
4747
*/
4848
protected function setTrustedProxyIpAddresses(Request $request)
4949
{
50-
$trustedIps = $this->proxies;
50+
$trustedIps = $this->proxies();
5151

5252
if ($trustedIps === '*' || $trustedIps === '**') {
5353
return $this->setTrustedProxyIpAddressesToTheCallingIp($request);
@@ -123,4 +123,14 @@ protected function getTrustedHeaderNames()
123123

124124
return $this->headers;
125125
}
126+
127+
/**
128+
* Get the trusted proxies.
129+
*
130+
* @return array|string|null
131+
*/
132+
protected function proxies()
133+
{
134+
return $this->proxies;
135+
}
126136
}

src/Illuminate/Support/Facades/Queue.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
* @method static void assertPushed(string|\Closure $job, callable|int $callback = null)
2222
* @method static void assertPushedOn(string $queue, string|\Closure $job, callable|int $callback = null)
2323
* @method static void assertPushedWithChain(string $job, array $expectedChain = [], callable $callback = null)
24-
* @method static void popUsing(string $workerName, callable $callback)
2524
*
2625
* @see \Illuminate\Queue\QueueManager
2726
* @see \Illuminate\Queue\Queue

src/Illuminate/Testing/TestResponse.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,105 @@ protected function responseHasView()
10221022
return isset($this->original) && $this->original instanceof View;
10231023
}
10241024

1025+
/**
1026+
* Assert that the given keys do not have validation errors.
1027+
*
1028+
* @param array|null $keys
1029+
* @param string $errorBag
1030+
* @param string $responseKey
1031+
* @return $this
1032+
*/
1033+
public function assertValid($keys = null, $errorBag = 'default', $responseKey = 'errors')
1034+
{
1035+
if ($this->baseResponse->headers->get('Content-Type') === 'application/json') {
1036+
return $this->assertJsonMissingValidationErrors($keys, $responseKey);
1037+
}
1038+
1039+
if ($this->session()->get('errors')) {
1040+
$errors = $this->session()->get('errors')->getBag($errorBag)->getMessages();
1041+
} else {
1042+
$errors = [];
1043+
}
1044+
1045+
if (empty($errors)) {
1046+
PHPUnit::assertTrue(true);
1047+
1048+
return $this;
1049+
}
1050+
1051+
if (is_null($keys) && count($errors) > 0) {
1052+
PHPUnit::fail(
1053+
'Response has unexpected validation errors: '.PHP_EOL.PHP_EOL.
1054+
json_encode($errors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
1055+
);
1056+
}
1057+
1058+
foreach (Arr::wrap($keys) as $key) {
1059+
PHPUnit::assertFalse(
1060+
isset($errors[$key]),
1061+
"Found unexpected validation error for key: '{$key}'"
1062+
);
1063+
}
1064+
1065+
return $this;
1066+
}
1067+
1068+
/**
1069+
* Assert that the response has the given validation errors.
1070+
*
1071+
* @param array $errors
1072+
* @param string $errorBag
1073+
* @param string $responseKey
1074+
* @return $this
1075+
*/
1076+
public function assertInvalid($errors,
1077+
$errorBag = 'default',
1078+
$responseKey = 'errors')
1079+
{
1080+
if ($this->baseResponse->headers->get('Content-Type') === 'application/json') {
1081+
return $this->assertJsonValidationErrors($errors, $responseKey);
1082+
}
1083+
1084+
$this->assertSessionHas('errors');
1085+
1086+
$keys = (array) $errors;
1087+
1088+
$sessionErrors = $this->session()->get('errors')->getBag($errorBag)->getMessages();
1089+
1090+
$errorMessage = $sessionErrors
1091+
? 'Response has the following validation errors in the session:'.
1092+
PHP_EOL.PHP_EOL.json_encode($sessionErrors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL
1093+
: 'Response does not have validation errors in the session.';
1094+
1095+
foreach ($errors as $key => $value) {
1096+
PHPUnit::assertArrayHasKey(
1097+
(is_int($key)) ? $value : $key,
1098+
$sessionErrors,
1099+
"Failed to find a validation error in session for key: '{$value}'".PHP_EOL.PHP_EOL.$errorMessage
1100+
);
1101+
1102+
if (! is_int($key)) {
1103+
$hasError = false;
1104+
1105+
foreach (Arr::wrap($sessionErrors[$key]) as $sessionErrorMessage) {
1106+
if (Str::contains($sessionErrorMessage, $value)) {
1107+
$hasError = true;
1108+
1109+
break;
1110+
}
1111+
}
1112+
1113+
if (! $hasError) {
1114+
PHPUnit::fail(
1115+
"Failed to find a validation error for key and message: '$key' => '$value'".PHP_EOL.PHP_EOL.$errorMessage
1116+
);
1117+
}
1118+
}
1119+
}
1120+
1121+
return $this;
1122+
}
1123+
10251124
/**
10261125
* Assert that the session has a given value.
10271126
*

src/Illuminate/Validation/ConditionalRules.php

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

33
namespace Illuminate\Validation;
44

5+
use Illuminate\Support\Fluent;
6+
57
class ConditionalRules
68
{
79
/**
@@ -40,7 +42,7 @@ public function __construct($condition, $rules)
4042
public function passes(array $data = [])
4143
{
4244
return is_callable($this->condition)
43-
? call_user_func($this->condition, $data)
45+
? call_user_func($this->condition, new Fluent($data))
4446
: $this->condition;
4547
}
4648

tests/Database/DatabaseProcessorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct()
3939
}
4040

4141
#[\ReturnTypeWillChange]
42-
public function lastInsertId(string $sequence = null)
42+
public function lastInsertId($sequence = null)
4343
{
4444
//
4545
}

tests/Http/HttpClientTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,27 @@ public function testTheRequestSendingAndResponseReceivedEventsAreFiredWhenAReque
940940
m::close();
941941
}
942942

943+
public function testTheRequestSendingAndResponseReceivedEventsAreFiredWhenARequestIsSentAsync()
944+
{
945+
$events = m::mock(Dispatcher::class);
946+
$events->shouldReceive('dispatch')->times(5)->with(m::type(RequestSending::class));
947+
$events->shouldReceive('dispatch')->times(5)->with(m::type(ResponseReceived::class));
948+
949+
$factory = new Factory($events);
950+
$factory->fake();
951+
$factory->pool(function (Pool $pool) {
952+
return [
953+
$pool->get('https://example.com'),
954+
$pool->head('https://example.com'),
955+
$pool->post('https://example.com'),
956+
$pool->patch('https://example.com'),
957+
$pool->delete('https://example.com'),
958+
];
959+
});
960+
961+
m::close();
962+
}
963+
943964
public function testTheTransferStatsAreCalledSafelyWhenFakingTheRequest()
944965
{
945966
$this->factory->fake(['https://example.com' => ['world' => 'Hello world']]);

tests/Integration/Auth/ApiAuthenticationWithEloquentTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ protected function getEnvironmentSetUp($app)
2121
$app['config']->set('auth.defaults.guard', 'api');
2222
$app['config']->set('auth.providers.users.model', User::class);
2323

24+
$app['config']->set('auth.guards.api', [
25+
'driver' => 'token',
26+
'provider' => 'users',
27+
'hash' => false,
28+
]);
29+
2430
// Database configuration
2531
$app['config']->set('database.default', 'testbench');
2632

0 commit comments

Comments
 (0)