Skip to content

Commit c727dda

Browse files
committed
tests: Add unit tests
1 parent 22ce088 commit c727dda

File tree

10 files changed

+305
-161
lines changed

10 files changed

+305
-161
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# WP Exceptions
22

3-
Stop returning WP_Error's. Throw exceptions instead.
3+
*WP_Error* was a cool WordPress feature in 2007 but today, we should throw exceptions instead.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "attributes-php/wp-exceptions",
33
"type": "library",
44
"license": "MIT",
5-
"description": "Rely on exceptions instead of the WP_Error. WordPress exceptions made easy",
5+
"description": "Stop returning WP_Error. Throw exceptions instead",
66
"authors": [
77
{
88
"name": "André Gil"

rector.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/ExceptionHandler.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Attributes\Wp\Exceptions;
1212

13+
use Exception;
1314
use Throwable;
1415

1516
use function wp_die;
@@ -34,6 +35,8 @@ class ExceptionHandler
3435
*/
3536
protected array $onExceptionHandlers = [];
3637

38+
protected $invoker = null;
39+
3740
/**
3841
* Registers the exception handler
3942
*/
@@ -87,7 +90,13 @@ public function __invoke(Throwable $ex): void
8790
throw $ex;
8891
}
8992

90-
call_user_func($handler, $ex);
93+
if (! $this->invoker) {
94+
call_user_func($handler, $ex);
95+
96+
return;
97+
}
98+
99+
call_user_func($this->invoker, $handler, $ex);
91100
}
92101

93102
/**
@@ -121,12 +130,18 @@ protected function handleHttpException(HttpException $ex): void
121130
require_once ABSPATH.WPINC.'/class-wp-error.php';
122131
}
123132

133+
if (! headers_sent() && (! function_exists('did_action') || ! did_action('admin_head'))) {
134+
foreach ($ex->getHeaders() as $name => $value) {
135+
header("$name: $value");
136+
}
137+
}
138+
124139
wp_die($ex->toWpError());
125140
}
126141

127142
/**
128-
* Retrieves an instance of this class. If it doesn't exist will store it on a global variable,
129-
* to allow compatibility with tools like Mozart which change the namespace.
143+
* Retrieves an instance of this class. If it doesn't exist creates a new one and stores it in a global
144+
* variable, to be compatible with tools like Mozart or PHP-Scoper which change the namespace.
130145
*/
131146
public static function getInstance(): ExceptionHandler
132147
{
@@ -140,4 +155,9 @@ public static function getInstance(): ExceptionHandler
140155

141156
return $attributes_wp_exceptions_exception_handler;
142157
}
158+
159+
public function setInvoker(callable $invoker): void
160+
{
161+
$this->invoker = $invoker;
162+
}
143163
}

src/HttpException.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,47 +35,55 @@ class HttpException extends Exception
3535

3636
protected ?WP_Error $wpError = null;
3737

38+
/**
39+
* @param int $status HTTP status code to be sent
40+
* @param string $message Short description
41+
* @param array<string,mixed> $data Additional data to be sent
42+
* @param array<string,mixed> $headers Headers to be sent
43+
*/
3844
public function __construct(
3945
int $status = WP_Http::INTERNAL_SERVER_ERROR,
4046
string $message = 'Something went wrong',
4147
array $data = [],
4248
array $headers = [],
4349
?Throwable $previous = null,
44-
?WP_Error $wpError = null,
4550
) {
51+
$this->status = $status;
4652
$this->data = $data;
47-
$this->data['status'] = $status;
53+
$this->data['status'] = $this->status;
4854

4955
$this->headers = $headers;
50-
$this->status = $status;
51-
$this->wpError = $wpError;
5256
parent::__construct($message, $this->status, $previous);
5357
}
5458

5559
/**
5660
* Creates a new instance using a WP_Error class
61+
*
62+
* @param WP_Error $error The WP_Error to base the exception from. Nested WP_Error's are also supported.
63+
* @param int $defaultStatus If the provided error code is a string and no 'data.status' is set, this value is used.
5764
*/
5865
public static function fromWpError(WP_Error $error, int $defaultStatus = 500): HttpException
5966
{
6067
$data = $error->get_error_data();
6168
$data = $data ?: [];
6269
$data = is_array($data) ? $data : ['data' => $data];
6370

64-
$status = $error->get_error_code();
65-
if (! is_int($status)) {
66-
$status = isset($data['status']) && is_int($data['status']) ? $data['status'] : $defaultStatus;
67-
}
71+
$status = isset($data['status']) && is_int($data['status']) ? $data['status'] : $error->get_error_code();
72+
$status = is_int($status) ? $status : $defaultStatus;
6873

69-
return new HttpException(
74+
$httpException = new HttpException(
7075
$status,
7176
$error->get_error_message(),
7277
$data,
73-
[],
74-
null,
75-
$error,
7678
);
79+
$httpException->wpError = $error;
80+
81+
return $httpException;
7782
}
7883

84+
/**
85+
* Converts this exception into a WP_Error object
86+
*/
7987
public function toWpError(): WP_Error
8088
{
8189
if ($this->wpError) {

src/RootsExceptionHandler.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/Integration/HttpExceptionTest.php

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)