Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
setCustomerNumber example
/** * @return array{ * 0: ProfileSalutationMissingError, * 1: BillingAddressSalutationMissingError, * 2: ShippingAddressSalutationMissingError * } */
private
static
function
getErrors
(
)
: array
{
$customer
=
new
CustomerEntity
(
)
;
$customer
->
setId
(
'1'
)
;
$customer
->
setCustomerNumber
(
''
)
;
$customer
->
setFirstName
(
''
)
;
$customer
->
setLastName
(
''
)
;
$address
=
new
CustomerAddressEntity
(
)
;
$address
->
setId
(
''
)
;
$address
->
setFirstName
(
''
)
;
$address
->
setLastName
(
''
)
;
$address
->
setZipcode
(
''
)
;
$address
->
setCity
(
''
)
;
return
[
public
function
testPreconditionRulePersonaCustomers
(
)
: void
{
$customer1
=
new
CustomerEntity
(
)
;
$customer1
->
setId
(
'C1'
)
;
$customer1
->
setCustomerNumber
(
'C1'
)
;
$customer2
=
new
CustomerEntity
(
)
;
$customer2
->
setId
(
'C2'
)
;
$customer2
->
setCustomerNumber
(
'C2'
)
;
$promotion
=
new
PromotionEntity
(
)
;
$promotion
->
setCustomerRestriction
(
true
)
;
$promotion
->
setPersonaCustomers
(
new
CustomerCollection
(
[
$customer1
,
$customer2
]
)
)
;
$custRule1
=
new
CustomerNumberRule
(
)
;
$billingAddress
->
setZipcode
(
$faker
->postcode
)
;
$billingAddress
->
setCity
(
$faker
->city
)
;
$billingAddress
->
setCountryId
(
'SWAG-AREA-COUNTRY-ID-1'
)
;
$customer
=
new
CustomerEntity
(
)
;
$customer
->
setId
(
'SWAG-CUSTOMER-ID-1'
)
;
$customer
->
setDefaultBillingAddress
(
$billingAddress
)
;
$customer
->
setEmail
(
'test@example.com'
)
;
$customer
->
setSalutationId
(
$this
->
getValidSalutationId
(
)
)
;
$customer
->
setFirstName
(
$faker
->firstName
)
;
$customer
->
setLastName
(
$faker
->lastName
)
;
$customer
->
setCustomerNumber
(
'Test'
)
;
return
$customer
;
}
private
function
getSalesChannelContext
(
)
: MockObject&SalesChannelContext
{
$customer
=
$this
->
getCustomer
(
)
;
$salesChannel
=
new
SalesChannelEntity
(
)
;
$salesChannel
->
setLanguageId
(
Defaults::LANGUAGE_SYSTEM
)
;
$salesChannelContext
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$salesChannelContext
->
method
(
'getCustomer'
)
->
willReturn
(
$customer
)
;
private
function
buildCustomerEntity
(
string
$id
)
: CustomerEntity
{
$customerEntity
=
new
CustomerEntity
(
)
;
$customerEntity
->
setId
(
$id
)
;
$customerEntity
->
setEmail
(
'test@example.org'
)
;
$customerEntity
->
setFirstName
(
'Max'
)
;
$customerEntity
->
setLastName
(
'Smith'
)
;
$customerEntity
->
setTitle
(
'Dr.'
)
;
$customerEntity
->
setCompany
(
'Acme Inc.'
)
;
$customerEntity
->
setCustomerNumber
(
'ABC123XY'
)
;
$customerEntity
->
setRemoteAddress
(
'Test street 123, NY'
)
;
$customerEntity
->
setCustomFields
(
[
'customerGroup' => 'premium', 'origin' => 'newsletter', 'active' => true
]
)
;
return
$customerEntity
;
}
}
}
private
function
getCustomer
(
bool
$withoutBillingAddress
)
: CustomerEntity
{
$customer
=
new
CustomerEntity
(
)
;
$customer
->
setId
(
'customer-id'
)
;
$customer
->
setEmail
(
'customer-email'
)
;
$customer
->
setSalutationId
(
'customer-salutation-id'
)
;
$customer
->
setFirstName
(
'customer-first-name'
)
;
$customer
->
setLastName
(
'customer-last-name'
)
;
$customer
->
setCustomerNumber
(
'customer-number'
)
;
$customer
->
setGroupId
(
'customer-group-id'
)
;
if
(
!
$withoutBillingAddress
)
{
$customer
->
setDefaultBillingAddress
(
$this
->
getCustomerAddress
(
)
)
;
}
return
$customer
;
}
private
function
getCustomerAddress
(
)
: CustomerAddressEntity
{
#[Package('business-ops')]
class
CustomerNumberRuleTest
extends
TestCase
{
public
function
testExactMatch
(
)
: void
{
$rule
=
(
new
CustomerNumberRule
(
)
)
->
assign
(
[
'numbers' =>
[
'NO. 1'
]
]
)
;
$cart
=
new
Cart
(
'test'
)
;
$customer
=
new
CustomerEntity
(
)
;
$customer
->
setCustomerNumber
(
'NO. 1'
)
;
$context
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$context
->
method
(
'getCustomer'
)
->
willReturn
(
$customer
)
;
static
::
assertTrue
(
$rule
->
match
(
new
CartRuleScope
(
$cart
,
$context
)
)
)
;
}
/** * @dataProvider getMatchValues * * @param array<string> $customerNumbers */
public
function
testRuleMatching
(
string
$operator
, bool
$isMatching
, array
$customerNumbers
, bool
$noCustomer
= false
)
: void
{
$salesChannelContext
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$customer
=
new
CustomerEntity
(
)
;
$customer
->
setCustomerNumber
(
'1337'
)
;
if
(
$noCustomer
)
{
$customer
= null;
}
$salesChannelContext
->
method
(
'getCustomer'
)
->
willReturn
(
$customer
)
;
$scope
=
new
CheckoutRuleScope
(
$salesChannelContext
)
;
$this
->rule->
assign
(
[
'numbers' =>
$customerNumbers
, 'operator' =>
$operator
]
)
;
$match
=
$this
->rule->
match
(
$scope
)
;
if
(
$isMatching
)
{
static
::
assertTrue
(
$match
)
;
}