Skip to content

Commit

Permalink
Requests: Add "multiple.request.before_request_multiple" hook
Browse files Browse the repository at this point in the history
  • Loading branch information
brianstoop committed Apr 25, 2024
1 parent 7f12bd4 commit 1f3080c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ Available Hooks
Parameters: `WpOrg\Requests\Response &$return`, `array &$req_headers`,
`array &$req_data`, `array &$options`

* **`multiple.request.before_request_multiple`**

Alter the request before it is sent to the transport.

Parameters: `array &$request, string|int $id`

* **`multiple.request.complete`**

Alter the response for an individual request in a multi-request.
Expand Down
2 changes: 2 additions & 0 deletions src/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,8 @@ public static function request_multiple($requests, $options = []) {
$request['options']['hooks']->register('multiple.request.complete', $request['options']['complete']);
}
}

$options['hooks']->dispatch('multiple.request.before_request_multiple', [&$request, $id]);
}

unset($request);
Expand Down
37 changes: 37 additions & 0 deletions tests/RequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,43 @@ public function testRequestMultipleInvalidOptions($input) {
Requests::request_multiple([], $input);
}

public function testRequestMultipleTriggersRequestsFailedCallback() {
$mock = $this->getMockBuilder(stdClass::class)->setMethods(['before_request_multiple'])->getMock();
$mock->expects($this->exactly(2))->method('before_request_multiple');
$hooks = new Hooks();
$hooks->register('multiple.request.before_request_multiple', [ $mock, 'before_request_multiple' ]);

$transport = new TransportMock();

$options = [
'hooks' => $hooks,
'transport' => $transport,
];

$requests = [
'test' => [
'url' => 'http://test-url.com',

Check warning on line 171 in tests/RequestsTest.php

View workflow job for this annotation

GitHub Actions / PHPCS

Array double arrow not aligned correctly; expected 5 space(s) between "'url'" and double arrow, but found 1.
'options' => [
'mock.code' => 200,
'mock.chunked' => false,
'mock.body' => 'Test Body',
'mock.raw_headers' => '',
]

Check failure on line 177 in tests/RequestsTest.php

View workflow job for this annotation

GitHub Actions / PHPCS

Each array item in a multi-line array declaration must end in a comma
],
'test2' => [
'url' => 'http://test-url.com',

Check warning on line 180 in tests/RequestsTest.php

View workflow job for this annotation

GitHub Actions / PHPCS

Array double arrow not aligned correctly; expected 5 space(s) between "'url'" and double arrow, but found 1.
'options' => [
'mock.code' => 200,
'mock.chunked' => false,
'mock.body' => 'Test Body',
'mock.raw_headers' => '',
]

Check failure on line 186 in tests/RequestsTest.php

View workflow job for this annotation

GitHub Actions / PHPCS

Each array item in a multi-line array declaration must end in a comma
]

Check failure on line 187 in tests/RequestsTest.php

View workflow job for this annotation

GitHub Actions / PHPCS

Each array item in a multi-line array declaration must end in a comma
];

Requests::request_multiple($requests, $options);
}

/**
* Data Provider.
*
Expand Down

0 comments on commit 1f3080c

Please sign in to comment.