-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from php-soap/deal-with-null-responses
Deal with null responses coming back from server
- Loading branch information
Showing
8 changed files
with
141 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phive xmlns="https://phar.io/phive"> | ||
<phar name="psalm" version="^4.13.1" installed="4.13.1" location="./tools/psalm.phar" copy="true"/> | ||
<phar name="psalm" version="^4.17" installed="4.17.0" location="./tools/psalm.phar" copy="true"/> | ||
<phar name="php-cs-fixer" version="^3.3.2" installed="3.3.2" location="./tools/php-cs-fixer.phar" copy="true"/> | ||
</phive> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Soap\ExtSoapEngine\ErrorHandling; | ||
|
||
use Soap\ExtSoapEngine\Exception\RequestException; | ||
|
||
/** | ||
* @psalm-internal Soap\ExtSoapEngine | ||
*/ | ||
final class ExtSoapErrorHandler | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param (callable(): T) $fun | ||
* | ||
* @return array{0: T, 1: ?string} | ||
* | ||
* @psalm-suppress MissingThrowsDocblock | ||
*/ | ||
public function __invoke(callable $fun): array | ||
{ | ||
$lastMessage = null; | ||
/** @psalm-suppress InvalidArgument */ | ||
set_error_handler(static function (int $_type, string $message) use (&$lastMessage) { | ||
$lastMessage = $message; | ||
}); | ||
|
||
try { | ||
$value = $fun(); | ||
|
||
/** @var array{0: T, 1: ?string} $result */ | ||
$result = [$value, $lastMessage]; | ||
|
||
return $result; | ||
} finally { | ||
restore_error_handler(); | ||
} | ||
} | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param (callable(): T) $fun | ||
* | ||
* @return T | ||
*/ | ||
public static function handleInternalErrors(callable $fun) | ||
{ | ||
[$result, $lastMessage] = (new self)($fun); | ||
|
||
if ($lastMessage) { | ||
throw RequestException::internalSoapError($lastMessage); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param T $response | ||
* @psalm-assert !null $response | ||
* | ||
* @return T | ||
*/ | ||
public static function handleNullResponse($response) | ||
{ | ||
if ($response === null) { | ||
throw RequestException::internalSoapError( | ||
'An empty response got returned after contacting the SOAP server.' | ||
); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace SoapTest\ExtSoapEngine\Unit\ErrorHandling; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Soap\ExtSoapEngine\ErrorHandling\ExtSoapErrorHandler; | ||
use Soap\ExtSoapEngine\Exception\RequestException; | ||
|
||
final class ExtSoapErrorHandlerTest extends TestCase | ||
{ | ||
public function test_it_can_deal_with_null_responses(): void | ||
{ | ||
$this->expectException(RequestException::class); | ||
|
||
ExtSoapErrorHandler::handleNullResponse(null); | ||
} | ||
|
||
public function test_it_can_deal_with_non_null_responses(): void | ||
{ | ||
$res = ExtSoapErrorHandler::handleNullResponse('hello'); | ||
|
||
static::assertSame('hello', $res); | ||
} | ||
|
||
public function test_it_can_detect_no_internal_errors_during_callback(): void | ||
{ | ||
$res = ExtSoapErrorHandler::handleInternalErrors( | ||
static fn () => 'hello' | ||
); | ||
|
||
static::assertSame($res, 'hello'); | ||
} | ||
|
||
public function test_it_can_detect_internal_errors_during_callback(): void | ||
{ | ||
$this->expectException(RequestException::class); | ||
$this->expectExceptionMessage('hello'); | ||
|
||
ExtSoapErrorHandler::handleInternalErrors( | ||
static function () { | ||
trigger_error('hello'); | ||
return 'x'; | ||
} | ||
); | ||
} | ||
} |
Binary file not shown.