Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
createCart example
public
function
testFilter
(
)
: void
{
$item
=
$this
->
createLineItemWithPrice
(
'test', 40
)
;
$item
->
setGood
(
true
)
;
$item2
=
$this
->
createLineItemWithPrice
(
'test', 100
)
;
$item2
->
setGood
(
true
)
;
$item3
=
$this
->
createLineItemWithPrice
(
'test-not-matching', 30
)
;
$item3
->
setGood
(
true
)
;
$cart
=
$this
->
createCart
(
new
LineItemCollection
(
[
$item
,
$item2
,
$item3
]
)
)
;
$this
->
assertRuleMatches
(
$cart
)
;
}
public
function
testFilterNested
(
)
: void
{
$item
=
$this
->
createLineItemWithPrice
(
'test', 40
)
;
$item
->
setGood
(
true
)
;
$item2
=
$this
->
createLineItemWithPrice
(
'test', 100
)
;
$item2
->
setGood
(
true
)
;
]
,
$this
->context
)
;
static
::
assertNotNull
(
$this
->conditionRepository->
search
(
new
Criteria
(
[
$id
]
)
,
$this
->context
)
->
get
(
$id
)
)
;
}
public
function
testRuleMatchWithoutItemsInCart
(
)
: void
{
$rule
=
new
PromotionsInCartCountRule
(
)
;
$rule
->
assign
(
[
'count' => 0, 'operator' => Rule::OPERATOR_EQ
]
)
;
static
::
assertTrue
(
$rule
->
match
(
new
CartRuleScope
(
$this
->
createCart
(
new
LineItemCollection
(
)
)
,
$this
->
createMock
(
SalesChannelContext::
class
)
)
)
)
;
}
public
function
testRuleMatchesWithTwoLineItems
(
)
: void
{
$rule
=
new
PromotionsInCartCountRule
(
)
;
$rule
->
assign
(
[
'count' => 2, 'operator' => Rule::OPERATOR_EQ
]
)
;
$lineItemCollection
=
new
LineItemCollection
(
[
$this
->
createLineItem
(
LineItem::PROMOTION_LINE_ITEM_TYPE
)
,
$this
->
createLineItem
(
LineItem::PROMOTION_LINE_ITEM_TYPE
)
,
]
)
;
$lineItem2
=
$this
->
createLineItemWithWeight
(
$lineItemWeight2
)
;
if
(
$lineItem2WithoutDeliveryInfo
)
{
$lineItem2
=
$this
->
createLineItem
(
)
;
}
$lineItemCollection
=
new
LineItemCollection
(
[
$lineItem1
,
$lineItem2
,
]
)
;
$cart
=
$this
->
createCart
(
$lineItemCollection
)
;
$match
=
$this
->rule->
match
(
new
CartRuleScope
(
$cart
,
$this
->
createMock
(
SalesChannelContext::
class
)
)
)
;
static
::
assertSame
(
$expected
,
$match
)
;
}
/** * @dataProvider getCartRuleScopeTestData */
$lineItem2
=
$this
->
createLineItemWithListPrice
(
$lineItemAmount2
)
;
if
(
$lineItem2WithoutPrice
)
{
$lineItem2
=
$this
->
createLineItem
(
)
;
}
$lineItemCollection
=
new
LineItemCollection
(
[
$lineItem1
,
$lineItem2
,
]
)
;
$cart
=
$this
->
createCart
(
$lineItemCollection
)
;
$match
=
$this
->rule->
match
(
new
CartRuleScope
(
$cart
,
$this
->
createMock
(
SalesChannelContext::
class
)
)
)
;
static
::
assertSame
(
$expected
,
$match
)
;
}
/** * @dataProvider getCartRuleScopeTestData */
$lineItem2
=
$this
->
createLineItemWithLength
(
$lineItemAmount2
)
;
if
(
$lineItem2WithoutDeliveryInfo
)
{
$lineItem2
=
$this
->
createLineItem
(
)
;
}
$lineItemCollection
=
new
LineItemCollection
(
[
$lineItem1
,
$lineItem2
,
]
)
;
$cart
=
$this
->
createCart
(
$lineItemCollection
)
;
$match
=
$this
->rule->
match
(
new
CartRuleScope
(
$cart
,
$this
->
createMock
(
SalesChannelContext::
class
)
)
)
;
static
::
assertSame
(
$expected
,
$match
)
;
}
/** * @dataProvider getCartRuleScopeTestData */
$lineItem2
= self::
createLineItemWithWidth
(
$lineItemAmount2
)
;
if
(
$lineItem2WithoutDeliveryInfo
)
{
$lineItem2
= self::
createLineItem
(
)
;
}
$lineItemCollection
=
new
LineItemCollection
(
[
$lineItem1
,
$lineItem2
,
]
)
;
$cart
=
$this
->
createCart
(
$lineItemCollection
)
;
$match
=
$this
->rule->
match
(
new
CartRuleScope
(
$cart
,
$this
->
createMock
(
SalesChannelContext::
class
)
)
)
;
static
::
assertSame
(
$expected
,
$match
)
;
}
/** * @dataProvider getCartRuleScopeTestData */
$order
=
new
OrderEntity
(
)
;
$collection
=
new
DocumentCollection
(
)
;
if
(
$documentId
)
{
$document
=
new
DocumentEntity
(
)
;
$document
->
setId
(
Uuid::
randomHex
(
)
)
;
$document
->
setDocumentTypeId
(
$documentId
)
;
$collection
->
add
(
$document
)
;
}
$order
->
setDocuments
(
$collection
)
;
$cart
= Generator::
createCart
(
)
;
$context
= Generator::
createSalesChannelContext
(
)
;
$scope
=
new
FlowRuleScope
(
$order
,
$cart
,
$context
)
;
$this
->rule->
assign
(
[
'documentIds' =>
$selectedDocumentIds
, 'operator' =>
$operator
]
)
;
static
::
assertSame
(
$expected
,
$this
->rule->
match
(
$scope
)
)
;
}
public
function
testInvalidScopeIsFalse
(
)
: void
{
$invalidScope
=
$this
->
createMock
(
RuleScope::
class
)
;
$this
->rule->
assign
(
[
'documentIds' =>
[
uuid::
randomHex
(
)
]
, 'operator' => Rule::OPERATOR_EQ
]
)
;
$this
->script =
new
Script
(
'test', '',
new
\
DateTimeImmutable
(
)
)
;
}
/** * @dataProvider addProductProvider */
public
function
testAddProduct
(
string
$input
, ?string
$expected
)
: void
{
$context
=
$this
->
getContainer
(
)
->
get
(
SalesChannelContextFactory::
class
)
->
create
(
Uuid::
randomHex
(
)
, TestDefaults::SALES_CHANNEL,
[
]
)
;
$hook
=
new
CartHook
(
$this
->
createCart
(
)
,
$context
)
;
$service
=
$this
->
getContainer
(
)
->
get
(
CartFacadeHookFactory::
class
)
->
factory
(
$hook
,
$this
->script
)
;
$service
->
products
(
)
->
add
(
$this
->ids->
get
(
$input
)
)
;
$service
->
calculate
(
)
;
$item
=
$service
->
products
(
)
->
get
(
$this
->ids->
get
(
$input
)
)
;
if
(
$expected
=== null
)
{
static
::
assertNull
(
$item
)
;
void
{
$this
->rule->
assign
(
[
'streamIds' =>
$streamIds
,
'operator' =>
$operator
,
]
)
;
$lineItemCollection
=
new
LineItemCollection
(
[
$this
->
createLineItemWithProductStreams
(
[
'1'
]
)
,
$this
->
createLineItemWithProductStreams
(
$lineItemCategoryIds
)
,
]
)
;
$cart
=
$this
->
createCart
(
$lineItemCollection
)
;
$match
=
$this
->rule->
match
(
new
CartRuleScope
(
$cart
,
$this
->
createMock
(
SalesChannelContext::
class
)
)
)
;
static
::
assertSame
(
$expected
,
$match
)
;
}
/** * @dataProvider getCartRuleScopeTestData * * @param array<string> $streamIds * @param array<string> $lineItemCategoryIds */
static
::
assertInstanceOf
(
LineItemOfTypeRule::
class
,
$filterRule
->
getRules
(
)
[
0
]
)
;
}
public
function
testFilter
(
)
: void
{
$item
=
$this
->
createLineItemWithPrice
(
'test-not-matching', 40
)
;
$item
->
setGood
(
true
)
;
$item2
=
$this
->
createLineItemWithPrice
(
'test', 100
)
;
$item2
->
setGood
(
true
)
;
$cart
=
$this
->
createCart
(
new
LineItemCollection
(
[
$item
,
$item2
]
)
)
;
$this
->
assertRuleMatches
(
$cart
)
;
}
public
function
testFilterNested
(
)
: void
{
$item
=
$this
->
createLineItemWithPrice
(
'test-not-matching', 40
)
;
$item
->
setGood
(
true
)
;
$item2
=
$this
->
createLineItemWithPrice
(
'test', 100
)
;
$item2
->
setGood
(
true
)
;
string
$lineItemCreationDate1
,
string
$lineItemCreationDate2
,
bool
$expected
)
: void
{
$this
->rule->
assign
(
[
'lineItemCreationDate' =>
$ruleCreationDate
, 'operator' => Rule::OPERATOR_EQ
]
)
;
$lineItemCollection
=
new
LineItemCollection
(
[
$this
->
createLineItemWithCreatedDate
(
$lineItemCreationDate1
)
,
$this
->
createLineItemWithCreatedDate
(
$lineItemCreationDate2
)
,
]
)
;
$cart
=
$this
->
createCart
(
$lineItemCollection
)
;
$match
=
$this
->rule->
match
(
new
CartRuleScope
(
$cart
,
$this
->
createMock
(
SalesChannelContext::
class
)
)
)
;
static
::
assertSame
(
$expected
,
$match
)
;
}
/** * @dataProvider getCartRuleScopeTestData */
]
,
$config
->
getData
(
)
)
;
}
private
function
createCartDummyWithShippingCosts
(
CalculatedPrice
$calculatedPrice
)
: Cart
{
$lineItemCollection
=
new
LineItemCollection
(
[
$this
->
createLineItemWithDeliveryInfo
(
false, 3, 10, 40, 3, 0.5
)
,
$this
->
createLineItemWithDeliveryInfo
(
true, 3, 10, 40, 3, 0.5
)
,
]
)
;
$cart
=
$this
->
createCart
(
$lineItemCollection
)
;
$deliveryPositionCollection
=
new
DeliveryPositionCollection
(
)
;
$deliveryDate
=
new
DeliveryDate
(
new
\
DateTimeImmutable
(
'now'
)
,
new
\
DateTimeImmutable
(
'now'
)
)
;
foreach
(
$cart
->
getLineItems
(
)
as
$lineItem
)
{
$deliveryPositionCollection
->
add
(
new
DeliveryPosition
(
Uuid::
randomHex
(
)
,
$lineItem
,
$lineItem
->
getQuantity
(
)
,
$calculatedPrice
,
$deliveryDate
)
)
;
}
$this
->ids->
get
(
'delivery-position-1'
)
=>
new
CalculatedTaxCollection
(
[
new
CalculatedTax
(
7,
7,
100
)
,
]
)
,
]
,
)
;
$cart
=
$this
->
createCart
(
)
;
static
::
assertNotNull
(
$cart
->
get
(
$this
->ids->
get
(
'line-item-1'
)
)
)
;
$cart
->
get
(
$this
->ids->
get
(
'line-item-1'
)
)
->
setPrice
(
null
)
;
$context
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$context
->
method
(
'getTotalRounding'
)
->
willReturn
(
new
CashRoundingConfig
(
2, 0.01, true
)
)
;
static
::
expectException
(
CartException::
class
)
;
$this
->conditionRepository =
$this
->
getContainer
(
)
->
get
(
'rule_condition.repository'
)
;
$this
->context = Context::
createDefaultContext
(
)
;
}
public
function
testIfShippingFreeLineItemsAreCaught
(
)
: void
{
$lineItemCollection
=
new
LineItemCollection
(
[
$this
->
createLineItemWithDeliveryInfo
(
false
)
,
$this
->
createLineItemWithDeliveryInfo
(
true
)
,
]
)
;
$cart
=
$this
->
createCart
(
$lineItemCollection
)
;
$match
=
(
new
CartHasDeliveryFreeItemRule
(
)
)
->
match
(
new
CartRuleScope
(
$cart
,
$this
->
createMock
(
SalesChannelContext::
class
)
)
)
;
static
::
assertTrue
(
$match
)
;
}
public
function
testIfShippingFreeNestedLineItemsAreCaught
(
)
: void
{
$childLineItemCollection
=
new
LineItemCollection
(
[
$this
->
createLineItemWithDeliveryInfo
(
false
)
,
$this
->builder =
$this
->
getContainer
(
)
->
get
(
DeliveryBuilder::
class
)
;
$this
->processor =
$this
->
getContainer
(
)
->
get
(
DeliveryProcessor::
class
)
;
$this
->context =
$this
->
getContainer
(
)
->
get
(
SalesChannelContextFactory::
class
)
->
create
(
Uuid::
randomHex
(
)
, TestDefaults::SALES_CHANNEL
)
;
}
public
function
testIndependenceOfLineItemAndDeliveryPositionPrices
(
)
: void
{
$cart
=
$this
->
createCart
(
)
;
$firstDelivery
=
$this
->
getDeliveries
(
$cart
)
->
first
(
)
;
static
::
assertNotNull
(
$firstDelivery
)
;
$firstPosition
=
$firstDelivery
->
getPositions
(
)
->
first
(
)
;
static
::
assertNotSame
(
$firstPosition
?->
getPrice
(
)
,
$cart
->
getLineItems
(
)
->
first
(
)
?->
getPrice
(
)
)
;
}
public
function
testEmptyCart
(
)
: void
{
$cart
=
$this
->
createCart
(
true
)
;