Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
invalidJson example
use
Symfony\Component\HttpFoundation\Response;
/** * @internal * * @covers \Shopware\Core\Framework\Util\UtilException */
class
UtilExceptionTest
extends
TestCase
{
public
function
testInvalidJson
(
)
: void
{
$e
= UtilException::
invalidJson
(
$p
=
new
\
JsonException
(
'invalid'
)
)
;
static
::
assertEquals
(
Response::HTTP_BAD_REQUEST,
$e
->
getStatusCode
(
)
)
;
static
::
assertEquals
(
'UTIL_INVALID_JSON',
$e
->
getErrorCode
(
)
)
;
static
::
assertEquals
(
'JSON is invalid',
$e
->
getMessage
(
)
)
;
static
::
assertEquals
(
$p
,
$e
->
getPrevious
(
)
)
;
}
public
function
testInvalidJsonNotList
(
)
: void
{
$e
= UtilException::
invalidJsonNotList
(
)
;
public
static
function
decodeToList
(
string
$value
)
: array
{
if
(
$value
=== ''
)
{
return
[
]
;
}
try
{
$result
=
json_decode
(
$value
, true, flags: \JSON_THROW_ON_ERROR
)
;
}
catch
(
\JsonException
$e
)
{
throw
UtilException::
invalidJson
(
$e
)
;
}
if
(
\
is_array
(
$result
)
&& \
array_is_list
(
$result
)
)
{
return
$result
;
}
throw
UtilException::
invalidJsonNotList
(
)
;
}
}