Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add injection of solutions in Exception constructor #119

Merged
merged 6 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ public function __construct(
// message contain additional parameters
$this->params = $message;
$message = array_shift($this->params);
if ($this->params['solutions'] ?? null) {
DarkSide666 marked this conversation as resolved.
Show resolved Hide resolved
if (!is_array($this->params['solutions'])) {
$this->params['solutions'] = [$this->params['solutions']];
}
foreach ($this->params['solutions'] as $solution) {
DarkSide666 marked this conversation as resolved.
Show resolved Hide resolved
$this->addSolution($solution);
}
unset($this->params['solutions']);
}
}

parent::__construct($message, $code ?? 0, $previous);
Expand Down
23 changes: 23 additions & 0 deletions tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ public function testSolution(): void
$this->assertRegExp('/One Solution/', $ret);
}

public function testSolution2(): void
{
$m = new Exception([
'Exception with solution',
'solutions' => '1st Solution',
]);

$ret = $m->getColorfulText();
$this->assertRegExp('/1st Solution/', $ret);

$m = new Exception([
'Exception with solution',
'solutions' => [
'1st Solution',
'2nd Solution',
],
]);

$ret = $m->getColorfulText();
$this->assertRegExp('/1st Solution/', $ret);
$this->assertRegExp('/2nd Solution/', $ret);
}

public function testCustomName(): void
{
$m = new ExceptionCustomName(
Expand Down