Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
assertJson example
$url
= '/api/app-system/action-button/run/' .
$action
->
getId
(
)
;
$ids
=
[
Uuid::
randomHex
(
)
]
;
$postData
=
[
'ids' =>
$ids
,
]
;
$this
->
appendNewResponse
(
new
Response
(
200
)
)
;
$postData
= \
json_encode
(
$postData
)
;
static
::
assertNotFalse
(
$postData
)
;
static
::
assertJson
(
$postData
)
;
$this
->
getBrowser
(
)
->
request
(
'POST',
$url
,
[
]
,
[
]
,
[
]
,
$postData
)
;
static
::
assertEquals
(
200,
$this
->
getBrowser
(
)
->
getResponse
(
)
->
getStatusCode
(
)
)
;
$request
=
$this
->
getLastRequest
(
)
;
static
::
assertNotNull
(
$request
)
;
static
::
assertEquals
(
'POST',
$request
->
getMethod
(
)
)
;
$body
=
$request
->
getBody
(
)
->
getContents
(
)
;
static
::
assertNotFalse
(
$body
)
;
public
function
testGetBlocks
(
)
: void
{
$this
->
loadAppsFromDir
(
__DIR__ . '/../Manifest/_fixtures/test'
)
;
$this
->
getBrowser
(
)
->
request
(
'GET', '/api/app-system/cms/blocks'
)
;
$response
=
$this
->
getBrowser
(
)
->
getResponse
(
)
;
static
::
assertNotFalse
(
$response
->
getContent
(
)
)
;
static
::
assertEquals
(
200,
$response
->
getStatusCode
(
)
)
;
$json
= \
file_get_contents
(
__DIR__ . '/_fixtures/expectedCmsBlocks.json'
)
;
static
::
assertNotFalse
(
$json
)
;
static
::
assertJson
(
$json
)
;
$expected
= \
json_decode
(
$json
, true, 512, \JSON_THROW_ON_ERROR
)
;
$actual
= \
json_decode
(
$response
->
getContent
(
)
, true, 512, \JSON_THROW_ON_ERROR
)
;
$expected
[
'blocks'
]
[
0
]
[
'template'
]
=
$this
->
stripWhitespace
(
$expected
[
'blocks'
]
[
0
]
[
'template'
]
)
;
$expected
[
'blocks'
]
[
1
]
[
'template'
]
=
$this
->
stripWhitespace
(
$expected
[
'blocks'
]
[
1
]
[
'template'
]
)
;
$actual
[
'blocks'
]
[
0
]
[
'template'
]
=
$this
->
stripWhitespace
(
$actual
[
'blocks'
]
[
0
]
[
'template'
]
)
;
$actual
[
'blocks'
]
[
1
]
[
'template'
]
=
$this
->
stripWhitespace
(
$actual
[
'blocks'
]
[
1
]
[
'template'
]
)
;
static
::
assertEquals
(
$expected
,
/** * @testWith [true] * [false] */
public
function
testDumpWithoutTitleIsValidJson
(
bool
$debug
)
{
$tester
=
$this
->
createCommandTester
(
$debug
)
;
$ret
=
$tester
->
execute
(
[
'name' => 'TestBundle', '--format' => 'json'
]
)
;
$this
->
assertSame
(
0,
$ret
, 'Returns 0 in case of success'
)
;
$this
->
assertJson
(
$tester
->
getDisplay
(
)
)
;
}
/** * @testWith [true] * [false] */
public
function
testDumpWithUnsupportedFormat
(
bool
$debug
)
{
$tester
=
$this
->
createCommandTester
(
$debug
)
;
$this
->
expectException
(
InvalidArgumentException::
class
)
;
static
::
assertEquals
(
200,
$this
->
getBrowser
(
)
->
getResponse
(
)
->
getStatusCode
(
)
)
;
$appUrlChangeResolver
=
$this
->
getContainer
(
)
->
get
(
Resolver::
class
)
;
static
::
assertEquals
(
$appUrlChangeResolver
->
getAvailableStrategies
(
)
,
$response
)
;
}
public
function
testResolveWithExistingStrategy
(
)
: void
{
$url
= '/api/app-system/app-url-change/resolve';
$json
= \
json_encode
(
[
'strategy' => UninstallAppsStrategy::STRATEGY_NAME
]
)
;
static
::
assertNotFalse
(
$json
)
;
static
::
assertJson
(
$json
)
;
$this
->
getBrowser
(
)
->
request
(
'POST',
$url
,
[
]
,
[
]
,
[
]
,
$json
)
;
$response
=
$this
->
getBrowser
(
)
->
getResponse
(
)
->
getContent
(
)
;
$this
->
createMock
(
ExtensionCompatibility::
class
)
,
$this
->
createMock
(
EventDispatcherInterface::
class
)
,
$this
->
createMock
(
SystemConfigService::
class
)
,
$this
->
createMock
(
AbstractExtensionLifecycle::
class
)
,
'6.5.1.0'
)
;
$response
=
$updateController
->
updateApiCheck
(
)
;
$content
=
$response
->
getContent
(
)
;
static
::
assertJson
(
(string)
$content
)
;
static
::
assertSame
(
'{}',
$content
)
;
}
public
function
testCheckForUpdatesWithUpdate
(
)
: void
{
$apiClient
=
$this
->
createMock
(
ApiClient::
class
)
;
$apiClient
->
method
(
'checkForUpdates'
)
->
willReturn
(
new
Version
(
[
'version' => '6.5.0.0', 'date' => '2020-01-01'
]
)
)
;
$updateController
=
new
UpdateController
(
use
AdminFunctionalTestBehaviour;
public
function
testGetSnippetsWithoutPlugins
(
)
: void
{
$url
= '/api/_action/custom-snippet';
$client
=
$this
->
getBrowser
(
)
;
$client
->
request
(
'GET',
$url
)
;
$content
=
$client
->
getResponse
(
)
->
getContent
(
)
;
static
::
assertNotFalse
(
$content
)
;
static
::
assertJson
(
$content
)
;
$content
=
json_decode
(
$content
, true, 512, \JSON_THROW_ON_ERROR
)
;
static
::
assertArrayHasKey
(
'data',
$content
)
;
static
::
assertSame
(
[
'address/city',
'address/company',
'address/country',
'address/country_state',
'address/department',
'address/first_name',
// check that event wasn't replaced
$returnedEvent
=
$webhookDispatcher
->
dispatch
(
$event
)
;
static
::
assertSame
(
$event
,
$returnedEvent
)
;
/** @var Request $request */
$request
=
$this
->
getLastRequest
(
)
;
static
::
assertEquals
(
'POST',
$request
->
getMethod
(
)
)
;
$body
=
$request
->
getBody
(
)
->
getContents
(
)
;
static
::
assertJson
(
$body
)
;
$payload
=
json_decode
(
$body
, true, 512, \JSON_THROW_ON_ERROR
)
;
static
::
assertArrayHasKey
(
'timestamp',
$payload
)
;
static
::
assertArrayHasKey
(
'eventId',
$payload
[
'source'
]
)
;
unset
(
$payload
[
'timestamp'
]
,
$payload
[
'source'
]
[
'eventId'
]
)
;
static
::
assertEquals
(
[
'data' =>
[
'payload' =>
[
'email' => 'test@example.com',
]
,
$this
->bus,
true
)
;
$webhookDispatcher
->
dispatch
(
$event
)
;
/** @var Request $request */
$request
=
$this
->
getLastRequest
(
)
;
static
::
assertEquals
(
'POST',
$request
->
getMethod
(
)
)
;
$body
=
$request
->
getBody
(
)
->
getContents
(
)
;
static
::
assertJson
(
$body
)
;
$data
=
json_decode
(
$body
, true, 512, \JSON_THROW_ON_ERROR
)
;
static
::
assertEquals
(
'Max',
$data
[
'data'
]
[
'payload'
]
[
'customer'
]
[
'firstName'
]
)
;
static
::
assertEquals
(
'Mustermann',
$data
[
'data'
]
[
'payload'
]
[
'customer'
]
[
'lastName'
]
)
;
static
::
assertArrayHasKey
(
'timestamp',
$data
)
;
static
::
assertArrayHasKey
(
'eventId',
$data
[
'source'
]
)
;
unset
(
$data
[
'timestamp'
]
,
$data
[
'data'
]
[
'payload'
]
[
'customer'
]
,
$data
[
'source'
]
[
'eventId'
]
)
;
static
::
assertEquals
(
[
'data' =>
[
'payload' =>
[
'contextToken' => 'testToken',
]
,
$body
=
$request
->
getBody
(
)
->
getContents
(
)
;
$appSecret
=
$this
->app->
getAppSecret
(
)
;
static
::
assertNotNull
(
$appSecret
)
;
static
::
assertTrue
(
$request
->
hasHeader
(
'shopware-shop-signature'
)
)
;
static
::
assertSame
(
hash_hmac
(
'sha256',
$body
,
$appSecret
)
,
$request
->
getHeaderLine
(
'shopware-shop-signature'
)
)
;
static
::
assertNotEmpty
(
$request
->
getHeaderLine
(
'sw-version'
)
)
;
static
::
assertNotEmpty
(
$request
->
getHeaderLine
(
AuthMiddleware::SHOPWARE_USER_LANGUAGE
)
)
;
static
::
assertNotEmpty
(
$request
->
getHeaderLine
(
AuthMiddleware::SHOPWARE_CONTEXT_LANGUAGE
)
)
;
static
::
assertSame
(
'POST',
$request
->
getMethod
(
)
)
;
static
::
assertJson
(
$body
)
;
$content
=
json_decode
(
$body
, true, 512, \JSON_THROW_ON_ERROR
)
;
static
::
assertIsArray
(
$content
)
;
static
::
assertArrayHasKey
(
'source',
$content
)
;
static
::
assertSame
(
[
'url' =>
$this
->shopUrl,
'shopId' =>
$this
->shopIdProvider->
getShopId
(
)
,
'appVersion' => '1.0.0',
]
,
$content
[
'source'
]
)
;
static
::
assertArrayHasKey
(
'cart',
$content
)
;
static
::
assertIsArray
(
$content
[
'cart'
]
)
;
static
::
assertArrayHasKey
(
'requestData',
$content
)
;
class
EntityWrittenEventSerializationTest
extends
TestCase
{
use
IntegrationTestBehaviour;
public
function
testEventCanBeSerialized
(
)
: void
{
$container
=
$this
->
writeTestProduct
(
)
;
$event
=
$container
->
getEventByEntityName
(
ProductDefinition::ENTITY_NAME
)
;
$encoded
=
json_encode
(
$event
, \JSON_THROW_ON_ERROR
)
;
static
::
assertNotFalse
(
$encoded
)
;
static
::
assertJson
(
$encoded
)
;
$encoded
=
json_encode
(
$container
, \JSON_THROW_ON_ERROR
)
;
static
::
assertNotFalse
(
$encoded
)
;
static
::
assertJson
(
$encoded
)
;
}
private
function
writeTestProduct
(
)
: EntityWrittenContainerEvent
{
/** @var EntityRepository $productRepository */
$productRepository
=
$this
->
getContainer
(
)
->
get
(
'product.repository'
)
;
'private_allowed_extensions' =>
$this
->
getContainer
(
)
->
getParameter
(
'shopware.filesystem.private_allowed_extensions'
)
,
'enableHtmlSanitizer' =>
$this
->
getContainer
(
)
->
getParameter
(
'shopware.html_sanitizer.enabled'
)
,
]
,
]
;
$url
= '/api/_info/config';
$client
=
$this
->
getBrowser
(
)
;
$client
->
request
(
'GET',
$url
)
;
$content
=
$client
->
getResponse
(
)
->
getContent
(
)
;
static
::
assertNotFalse
(
$content
)
;
static
::
assertJson
(
$content
)
;
$decodedResponse
=
json_decode
(
$content
, true, 512, \JSON_THROW_ON_ERROR
)
;
static
::
assertSame
(
200,
$client
->
getResponse
(
)
->
getStatusCode
(
)
)
;
foreach
(
array_keys
(
$expected
)
as
$key
)
{
static
::
assertArrayHasKey
(
$key
,
$decodedResponse
)
;
}
static
::
assertEquals
(
$expected
[
'settings'
]
,
$decodedResponse
[
'settings'
]
)
;
$body
=
$request
->
getBody
(
)
->
getContents
(
)
;
$appSecret
=
$this
->app->
getAppSecret
(
)
;
static
::
assertNotNull
(
$appSecret
)
;
static
::
assertTrue
(
$request
->
hasHeader
(
'shopware-shop-signature'
)
)
;
static
::
assertSame
(
\
hash_hmac
(
'sha256',
$body
,
$appSecret
)
,
$request
->
getHeaderLine
(
'shopware-shop-signature'
)
)
;
static
::
assertNotEmpty
(
$request
->
getHeaderLine
(
'sw-version'
)
)
;
static
::
assertNotEmpty
(
$request
->
getHeaderLine
(
AuthMiddleware::SHOPWARE_USER_LANGUAGE
)
)
;
static
::
assertNotEmpty
(
$request
->
getHeaderLine
(
AuthMiddleware::SHOPWARE_CONTEXT_LANGUAGE
)
)
;
static
::
assertSame
(
'POST',
$request
->
getMethod
(
)
)
;
static
::
assertJson
(
$body
)
;
$content
=
json_decode
(
$body
, true, 512, \JSON_THROW_ON_ERROR
)
;
static
::
assertIsArray
(
$content
)
;
static
::
assertArrayHasKey
(
'source',
$content
)
;
static
::
assertSame
(
[
'url' =>
$this
->shopUrl,
'shopId' =>
$this
->shopIdProvider->
getShopId
(
)
,
'appVersion' => '1.0.0',
]
,
$content
[
'source'
]
)
;
static
::
assertArrayHasKey
(
'order',
$content
)
;
static
::
assertIsArray
(
$content
[
'order'
]
)
;
static
::
assertArrayHasKey
(
'orderCustomer',
$content
[
'order'
]
)
;
$migration
=
new
Migration1673946817FixMediaFolderAssociationFields
(
)
;
static
::
assertEquals
(
'1673946817',
$migration
->
getCreationTimestamp
(
)
)
;
}
public
function
testFieldsAreMigrated
(
)
: void
{
$migration
=
new
Migration1673946817FixMediaFolderAssociationFields
(
)
;
$migration
->
update
(
$this
->connection
)
;
$fields
=
$this
->connection->
fetchOne
(
'SELECT association_fields FROM media_default_folder WHERE entity = :user',
[
'user' => 'user'
]
)
;
static
::
assertJson
(
$fields
)
;
$fields
= \
json_decode
(
$fields
, true
)
;
static
::
assertIsArray
(
$fields
)
;
static
::
assertContains
(
'avatarUsers',
$fields
)
;
}
}
$body
=
$request
->
getBody
(
)
->
getContents
(
)
;
$appSecret
=
$this
->app->
getAppSecret
(
)
;
static
::
assertNotNull
(
$appSecret
)
;
static
::
assertTrue
(
$request
->
hasHeader
(
'shopware-shop-signature'
)
)
;
static
::
assertSame
(
\
hash_hmac
(
'sha256',
$body
,
$appSecret
)
,
$request
->
getHeaderLine
(
'shopware-shop-signature'
)
)
;
static
::
assertNotEmpty
(
$request
->
getHeaderLine
(
'sw-version'
)
)
;
static
::
assertNotEmpty
(
$request
->
getHeaderLine
(
AuthMiddleware::SHOPWARE_CONTEXT_LANGUAGE
)
)
;
static
::
assertNotEmpty
(
$request
->
getHeaderLine
(
AuthMiddleware::SHOPWARE_USER_LANGUAGE
)
)
;
static
::
assertSame
(
'POST',
$request
->
getMethod
(
)
)
;
static
::
assertJson
(
$body
)
;
$content
=
json_decode
(
$body
, true, 512, \JSON_THROW_ON_ERROR
)
;
static
::
assertIsArray
(
$content
)
;
static
::
assertArrayHasKey
(
'source',
$content
)
;
static
::
assertSame
(
[
'url' =>
$this
->shopUrl,
'shopId' =>
$this
->shopIdProvider->
getShopId
(
)
,
'appVersion' => '1.0.0',
]
,
$content
[
'source'
]
)
;
static
::
assertArrayHasKey
(
'orderTransaction',
$content
)
;
static
::
assertIsArray
(
$content
[
'orderTransaction'
]
)
;
static
::
assertNull
(
$content
[
'orderTransaction'
]
[
'paymentMethod'
]
[
'appPaymentMethod'
]
[
'app'
]
)
;
$body
=
$request
->
getBody
(
)
->
getContents
(
)
;
$appSecret
=
$this
->app->
getAppSecret
(
)
;
static
::
assertNotNull
(
$appSecret
)
;
static
::
assertTrue
(
$request
->
hasHeader
(
'shopware-shop-signature'
)
)
;
static
::
assertSame
(
hash_hmac
(
'sha256',
$body
,
$appSecret
)
,
$request
->
getHeaderLine
(
'shopware-shop-signature'
)
)
;
static
::
assertNotEmpty
(
$request
->
getHeaderLine
(
'sw-version'
)
)
;
static
::
assertNotEmpty
(
$request
->
getHeaderLine
(
AuthMiddleware::SHOPWARE_USER_LANGUAGE
)
)
;
static
::
assertNotEmpty
(
$request
->
getHeaderLine
(
AuthMiddleware::SHOPWARE_CONTEXT_LANGUAGE
)
)
;
static
::
assertSame
(
'POST',
$request
->
getMethod
(
)
)
;
static
::
assertJson
(
$body
)
;
$content
=
json_decode
(
$body
, true, 512, \JSON_THROW_ON_ERROR
)
;
static
::
assertArrayHasKey
(
'source',
$content
)
;
static
::
assertSame
(
[
'url' =>
$this
->shopUrl,
'shopId' =>
$this
->shopIdProvider->
getShopId
(
)
,
'appVersion' => '1.0.0',
]
,
$content
[
'source'
]
)
;
static
::
assertArrayHasKey
(
'refund',
$content
)
;
static
::
assertIsArray
(
$content
[
'refund'
]
)
;
$this
->
assertRefundState
(
OrderTransactionCaptureRefundStates::STATE_COMPLETED,
$refundId
)
;
}