Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
EntityResolverContext example
$slot
->
setUniqueIdentifier
(
'id'
)
;
$slot
->
setType
(
'buy-box'
)
;
$slot
->
setFieldConfig
(
$fieldConfig
)
;
$variantProduct
=
new
SalesChannelProductEntity
(
)
;
$variantProduct
->
setId
(
$investigatedProductId
)
;
if
(
isset
(
$variantIds
[
0
]
)
)
{
$variantProduct
->
setParentId
(
$productId
)
;
}
$variantProduct
->
setOptionIds
(
[
Uuid::
randomHex
(
)
]
)
;
$resolverContext
=
new
EntityResolverContext
(
$salesChannelContext
,
new
Request
(
)
,
$this
->
createMock
(
SalesChannelProductDefinition::
class
)
,
$variantProduct
)
;
$result
=
new
ElementDataCollection
(
)
;
$buyBoxResolver
=
$this
->
getContainer
(
)
->
get
(
BuyBoxCmsElementResolver::
class
)
;
$buyBoxResolver
->
enrich
(
$slot
,
$resolverContext
,
$result
)
;
public
function
testMediaWithLocal
(
)
: void
{
$media
=
new
MediaEntity
(
)
;
$media
->
setUniqueIdentifier
(
'media123'
)
;
$productMedia
=
new
ProductMediaEntity
(
)
;
$productMedia
->
setMedia
(
$media
)
;
$product
=
new
ProductEntity
(
)
;
$product
->
setCover
(
$productMedia
)
;
$resolverContext
=
new
EntityResolverContext
(
$this
->
createMock
(
SalesChannelContext::
class
)
,
new
Request
(
)
,
$this
->
createMock
(
ProductDefinition::
class
)
,
$product
)
;
$mediaSearchResult
=
new
EntitySearchResult
(
'media',
0,
new
MediaCollection
(
)
,
null,
new
Criteria
(
)
,
Context::
createDefaultContext
(
)
)
;
$result
=
new
ElementDataCollection
(
)
;
#[Route(path: '/store-api/landing-page/{landingPageId}', name: 'store-api.landing-page.detail', methods: ['POST'])]
public
function
load
(
string
$landingPageId
, Request
$request
, SalesChannelContext
$context
)
: LandingPageRouteResponse
{
$landingPage
=
$this
->
loadLandingPage
(
$landingPageId
,
$context
)
;
$pageId
=
$landingPage
->
getCmsPageId
(
)
;
if
(
!
$pageId
)
{
return
new
LandingPageRouteResponse
(
$landingPage
)
;
}
$resolverContext
=
new
EntityResolverContext
(
$context
,
$request
,
$this
->landingPageDefinition,
$landingPage
)
;
$pages
=
$this
->cmsPageLoader->
load
(
$request
,
$this
->
createCriteria
(
$pageId
,
$request
)
,
$context
,
$landingPage
->
getTranslation
(
'slotConfig'
)
,
$resolverContext
)
;
if
(
!
$pages
->
has
(
$pageId
)
)
{
throw
new
PageNotFoundException
(
$pageId
)
;
}
static
::
assertInstanceOf
(
ManufacturerLogoStruct::
class
,
$manufacturerLogoStruct
)
;
static
::
assertNull
(
$manufacturerLogoStruct
->
getManufacturer
(
)
)
;
}
public
function
testEnrichEntityResolverContext
(
)
: void
{
$manufacturer
=
new
ProductManufacturerEntity
(
)
;
$manufacturer
->
setId
(
'manufacturer_01'
)
;
$product
=
new
SalesChannelProductEntity
(
)
;
$product
->
setId
(
'product_01'
)
;
$product
->
setManufacturer
(
$manufacturer
)
;
$resolverContext
=
new
EntityResolverContext
(
$this
->
createMock
(
SalesChannelContext::
class
)
,
new
Request
(
)
,
$this
->
getContainer
(
)
->
get
(
SalesChannelProductDefinition::
class
)
,
$product
)
;
$result
=
new
ElementDataCollection
(
)
;
$media
=
new
MediaEntity
(
)
;
$media
->
setId
(
'media_01'
)
;
$result
->
add
(
'media_id',
new
EntitySearchResult
(
'media',
1,
new
MediaCollection
(
[
$media
]
)
,
null,
new
Criteria
(
)
,
$salesChannel
=
$context
->
getSalesChannel
(
)
;
if
(
$category
->
getId
(
)
===
$salesChannel
->
getNavigationCategoryId
(
)
&&
$salesChannel
->
getHomeCmsPageId
(
)
)
{
$pageId
=
$salesChannel
->
getHomeCmsPageId
(
)
;
$slotConfig
=
$salesChannel
->
getTranslation
(
'homeSlotConfig'
)
;
}
if
(
!
$pageId
)
{
return
new
CategoryRouteResponse
(
$category
)
;
}
$resolverContext
=
new
EntityResolverContext
(
$context
,
$request
,
$this
->categoryDefinition,
$category
)
;
$pages
=
$this
->cmsPageLoader->
load
(
$request
,
$this
->
createCriteria
(
$pageId
,
$request
)
,
$context
,
$slotConfig
,
$resolverContext
)
;
if
(
!
$pages
->
has
(
$pageId
)
)
{
throw
CategoryException::
pageNotFound
(
$pageId
)
;
}
/** @var TextStruct|null $textStruct */
$textStruct
=
$slot
->
getData
(
)
;
static
::
assertInstanceOf
(
TextStruct::
class
,
$textStruct
)
;
static
::
assertSame
(
'lorem',
$textStruct
->
getContent
(
)
)
;
}
public
function
testWithMappedContent
(
)
: void
{
$product
=
new
ProductEntity
(
)
;
$product
->
setDescription
(
'foobar loo'
)
;
$resolverContext
=
new
EntityResolverContext
(
$this
->
createMock
(
SalesChannelContext::
class
)
,
new
Request
(
)
,
$this
->
createMock
(
ProductDefinition::
class
)
,
$product
)
;
$result
=
new
ElementDataCollection
(
)
;
$fieldConfig
=
new
FieldConfigCollection
(
)
;
$fieldConfig
->
add
(
new
FieldConfig
(
'content', FieldConfig::SOURCE_MAPPED, 'product.description'
)
)
;
$slot
=
new
CmsSlotEntity
(
)
;
$slot
->
setUniqueIdentifier
(
'id'
)
;
$slot
->
setType
(
'text'
)
;
$slot
->
setConfig
(
[
]
)
;
$slot
->
setFieldConfig
(
$fieldConfig
)
;
$slot
->
setFieldConfig
(
$fieldConfig
)
;
$criteriaCollection
=
$this
->dummyResolver->
collect
(
$slot
,
$resolverContext
)
;
static
::
assertNull
(
$criteriaCollection
)
;
}
public
function
testCollectWithEntityResolver
(
)
: void
{
$product
=
new
SalesChannelProductEntity
(
)
;
$product
->
setId
(
'product-01'
)
;
$entityResolverContext
=
new
EntityResolverContext
(
$this
->
createMock
(
SalesChannelContext::
class
)
,
new
Request
(
)
,
$this
->
createMock
(
SalesChannelProductDefinition::
class
)
,
$product
)
;
$fieldConfig
=
new
FieldConfigCollection
(
)
;
$slot
=
new
CmsSlotEntity
(
)
;
$slot
->
setUniqueIdentifier
(
'id'
)
;
$slot
->
setType
(
'dummy-type'
)
;
/** @var TextStruct|null $textStruct */
$textStruct
=
$slot
->
getData
(
)
;
static
::
assertInstanceOf
(
TextStruct::
class
,
$textStruct
)
;
static
::
assertNull
(
$textStruct
->
getContent
(
)
)
;
}
public
function
testEnrichEntityResolverContext
(
)
: void
{
$product
=
new
SalesChannelProductEntity
(
)
;
$product
->
setId
(
'product_01'
)
;
$product
->
setName
(
'Product 01'
)
;
$resolverContext
=
new
EntityResolverContext
(
$this
->
createMock
(
SalesChannelContext::
class
)
,
new
Request
(
)
,
$this
->
getContainer
(
)
->
get
(
SalesChannelProductDefinition::
class
)
,
$product
)
;
$result
=
new
ElementDataCollection
(
)
;
$slot
=
new
CmsSlotEntity
(
)
;
$slot
->
setUniqueIdentifier
(
'id'
)
;
$slot
->
setType
(
'product-name'
)
;
$slot
->
setFieldConfig
(
new
FieldConfigCollection
(
[
new
FieldConfig
(
'content', FieldConfig::SOURCE_MAPPED, 'product.name'
)
]
)
)
;
$this
->productNameCmsElementResolver->
enrich
(
$slot
,
$resolverContext
,
$result
)
;
/** @var TextStruct|null $textStruct */
if
(
$coverId
)
{
$product
->
setCoverId
(
$coverId
)
;
}
if
(
\
is_string
(
$coverId
)
&&
$productMediaCollection
->
has
(
$coverId
)
)
{
$cover
=
new
ProductMediaEntity
(
)
;
$cover
->
setId
(
$coverId
)
;
$product
->
setCover
(
$cover
)
;
}
return
new
EntityResolverContext
(
$this
->
createMock
(
SalesChannelContext::
class
)
,
new
Request
(
)
,
$this
->
getContainer
(
)
->
get
(
SalesChannelProductDefinition::
class
)
,
$product
)
;
}
protected
function
getEntitySearchResult
(
ProductMediaCollection
$productMediaCollection
, EntityResolverContext
$resolverContext
)
: ElementDataCollection
{
$result
=
new
ElementDataCollection
(
)
;
$result
->
add
(
'media_id',
new
EntitySearchResult
(
static
::
assertNull
(
$product
)
;
}
public
function
testEnrichWithProductConfigIsMapped
(
)
: void
{
$productId
= Uuid::
randomHex
(
)
;
$salesChannelContext
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$product
=
new
SalesChannelProductEntity
(
)
;
$product
->
setId
(
$productId
)
;
$product
->
setUniqueIdentifier
(
'product1'
)
;
$resolverContext
=
new
EntityResolverContext
(
$salesChannelContext
,
new
Request
(
)
,
$this
->
createMock
(
SalesChannelProductDefinition::
class
)
,
$product
)
;
$result
=
new
ElementDataCollection
(
)
;
$fieldConfig
=
new
FieldConfigCollection
(
)
;
$fieldConfig
->
add
(
new
FieldConfig
(
'product', FieldConfig::SOURCE_MAPPED,
$productId
)
)
;
$slot
=
new
CmsSlotEntity
(
)
;
$slot
->
setUniqueIdentifier
(
'id'
)
;
$slot
->
setType
(
''
)
;
$slot
->
setFieldConfig
(
$fieldConfig
)
;
$this
->boxCmsElementResolver->
enrich
(
$slot
,
$resolverContext
,
$result
)
;
$collection
=
$this
->sliderResolver->
collect
(
$slot
,
$resolverContext
)
;
static
::
assertNull
(
$collection
)
;
}
public
function
testCollectWithMappedConfigButWithInvalidProperty
(
)
: void
{
$category
=
new
CategoryEntity
(
)
;
$category
->
setUniqueIdentifier
(
'category1'
)
;
$resolverContext
=
new
EntityResolverContext
(
$this
->
createMock
(
SalesChannelContext::
class
)
,
new
Request
(
)
,
$this
->
createMock
(
CategoryDefinition::
class
)
,
$category
)
;
$fieldConfig
=
new
FieldConfigCollection
(
)
;
$fieldConfig
->
add
(
new
FieldConfig
(
'products', FieldConfig::SOURCE_MAPPED, 'category.foo'
)
)
;
$slot
=
new
CmsSlotEntity
(
)
;
$slot
->
setUniqueIdentifier
(
'id'
)
;
$slot
->
setType
(
'product-slider'
)
;
$slot
->
setFieldConfig
(
$fieldConfig
)
;
$this
->
expectException
(
\InvalidArgumentException::
class
)
;
$this
->
expectExceptionMessage
(
'Property foo do not exist in class ' . CategoryEntity::
class
)
;
$product
->
setSeoCategory
(
$this
->breadcrumbBuilder->
getProductSeoCategory
(
$product
,
$context
)
)
;
$configurator
=
$this
->configuratorLoader->
load
(
$product
,
$context
)
;
$pageId
=
$product
->
getCmsPageId
(
)
;
if
(
$pageId
)
{
// clone product to prevent recursion encoding (see NEXT-17603)
$resolverContext
=
new
EntityResolverContext
(
$context
,
$request
,
$this
->productDefinition,
clone
$product
)
;
$pages
=
$this
->cmsPageLoader->
load
(
$request
,
$this
->
createCriteria
(
$pageId
,
$request
)
,
$context
,
$product
->
getTranslation
(
'slotConfig'
)
,
$resolverContext
)
;
if
(
$page
=
$pages
->
first
(
)
)
{
$product
->
setCmsPage
(
$page
)
;
}
public
function
testEnrichWithMappedConfig
(
)
: void
{
$parent
=
new
SalesChannelProductEntity
(
)
;
$parent
->
setId
(
'product_parent'
)
;
$product
=
new
SalesChannelProductEntity
(
)
;
$product
->
setId
(
'product123'
)
;
$product
->
setParent
(
$parent
)
;
$resolverContext
=
new
EntityResolverContext
(
$this
->
createMock
(
SalesChannelContext::
class
)
,
new
Request
(
)
,
$this
->
createMock
(
ProductDefinition::
class
)
,
$product
)
;
$result
=
new
ElementDataCollection
(
)
;
$fieldConfig
=
new
FieldConfigCollection
(
)
;
$fieldConfig
->
add
(
new
FieldConfig
(
'product', FieldConfig::SOURCE_MAPPED, 'product.parent'
)
)
;
$slot
=
new
CmsSlotEntity
(
)
;
$slot
->
setUniqueIdentifier
(
'id'
)
;
$slot
->
setType
(
'product-box'
)
;
$slot
->
setFieldConfig
(
$fieldConfig
)
;
$this
->productBoxResolver->
enrich
(
$slot
,
$resolverContext
,
$result
)
;