Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
CustomerAddressEntity example
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
[
new
ProfileSalutationMissingError
(
$customer
)
,
new
BillingAddressSalutationMissingError
(
$address
)
,
new
ShippingAddressSalutationMissingError
(
$address
)
,
]
;
}
#[Package('business-ops')]
class
DifferentAddressesRuleTest
extends
TestCase
{
public
function
testRuleMatch
(
)
: void
{
$rule
=
new
DifferentAddressesRule
(
)
;
$cart
=
new
Cart
(
'test'
)
;
$context
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$billing
=
new
CustomerAddressEntity
(
)
;
$billing
->
setId
(
'SWAG-CUSTOMER-ADDRESS-ID-1'
)
;
$shipping
=
new
CustomerAddressEntity
(
)
;
$shipping
->
setId
(
'SWAG-CUSTOMER-ADDRESS-ID-2'
)
;
$customer
=
new
CustomerEntity
(
)
;
$customer
->
setDefaultBillingAddress
(
$billing
)
;
$customer
->
setDefaultShippingAddress
(
$shipping
)
;
$context
->
method
(
'getCustomer'
)
static
::
assertEquals
(
new
Type
(
'string'
)
,
$streetName
[
1
]
)
;
}
/** * @dataProvider getMatchValues */
public
function
testRuleMatching
(
string
$operator
, bool
$isMatching
, string
$shippingStreet
, bool
$noAddress
= false
)
: void
{
$streetName
= 'kyln123';
$salesChannelContext
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$customerAddress
=
new
CustomerAddressEntity
(
)
;
$customerAddress
->
setStreet
(
$shippingStreet
)
;
if
(
$noAddress
)
{
$customerAddress
= null;
}
$location
=
new
ShippingLocation
(
new
CountryEntity
(
)
, null,
$customerAddress
)
;
$salesChannelContext
->
method
(
'getShippingLocation'
)
->
willReturn
(
$location
)
;
$scope
=
new
CheckoutRuleScope
(
$salesChannelContext
)
;
$this
->rule->
assign
(
[
'streetName' =>
$streetName
, 'operator' =>
$operator
]
)
;
$streetName
=
$ruleConstraints
[
'streetName'
]
;
static
::
assertEquals
(
new
NotBlank
(
)
,
$streetName
[
0
]
)
;
static
::
assertEquals
(
new
Type
(
'string'
)
,
$streetName
[
1
]
)
;
}
public
function
testUnsupportedValue
(
)
: void
{
try
{
$rule
=
new
BillingStreetRule
(
)
;
$salesChannelContext
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$customer
=
new
CustomerEntity
(
)
;
$customer
->
setActiveBillingAddress
(
new
CustomerAddressEntity
(
)
)
;
$salesChannelContext
->
method
(
'getCustomer'
)
->
willReturn
(
$customer
)
;
$rule
->
match
(
new
CheckoutRuleScope
(
$salesChannelContext
)
)
;
static
::
fail
(
'Exception was not thrown'
)
;
}
catch
(
\Throwable
$exception
)
{
static
::
assertInstanceOf
(
UnsupportedValueException::
class
,
$exception
)
;
}
}
public
function
testRuleNotMatchingWithoutAddress
(
)
: void
{
$this
->rule->
assign
(
[
'streetName' => 'foo', 'operator' => Rule::OPERATOR_EQ
]
)
;
catch
(
OrderException
)
{
}
catch
(
\Exception
)
{
static
::
fail
(
'Not an expected Exception'
)
;
}
}
/** * @return SalesChannelContext&MockObject */
private
function
getContextWithDummyCustomer
(
)
: SalesChannelContext
{
$address
=
(
new
CustomerAddressEntity
(
)
)
->
assign
(
[
'id' => Uuid::
randomHex
(
)
]
)
;
$customer
=
new
CustomerEntity
(
)
;
$customer
->
assign
(
[
'activeBillingAddress' =>
$address
,
'activeShippingAddress' =>
$address
,
'id' => Uuid::
randomHex
(
)
,
]
)
;
$context
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$context
->
method
(
'getCustomer'
)
->
willReturn
(
$customer
)
;
$country
->
setCustomerTax
(
new
TaxFreeConfig
(
false, Defaults::CURRENCY, 0
)
)
;
$country
->
setCompanyTax
(
new
TaxFreeConfig
(
false, Defaults::CURRENCY, 0
)
)
;
$country
->
setName
(
'Germany'
)
;
}
if
(
!
$state
)
{
$state
=
new
CountryStateEntity
(
)
;
$state
->
setId
(
'bd5e2dcf547e4df6bb1ff58a554bc69e'
)
;
$state
->
setCountryId
(
$country
->
getId
(
)
)
;
}
if
(
!
$shipping
)
{
$shipping
=
new
CustomerAddressEntity
(
)
;
$shipping
->
setCountry
(
$country
)
;
$shipping
->
setCountryState
(
$state
)
;
}
if
(
!
$paymentMethod
)
{
$paymentMethod
=
(
new
PaymentMethodEntity
(
)
)
->
assign
(
[
'id' => '19d144ffe15f4772860d59fca7f207c1',
'handlerIdentifier' => SyncTestPaymentHandler::
class
,
'name' => 'Generated Payment',
'active' => true,
]
$constraints
[
'operator'
]
)
;
static
::
assertEquals
(
[
new
NotBlank
(
)
,
new
Type
(
'string'
)
]
,
$constraints
[
'cityName'
]
)
;
}
/** * @dataProvider getMatchValues */
public
function
testRuleMatching
(
string
$operator
, bool
$isMatching
, string
$billingCity
)
: void
{
$cityName
= 'kyln123';
$salesChannelContext
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$customerAddress
=
new
CustomerAddressEntity
(
)
;
$customerAddress
->
setCity
(
$billingCity
)
;
$customer
=
new
CustomerEntity
(
)
;
$customer
->
setActiveBillingAddress
(
$customerAddress
)
;
$salesChannelContext
->
method
(
'getCustomer'
)
->
willReturn
(
$customer
)
;
$scope
=
new
CheckoutRuleScope
(
$salesChannelContext
)
;
$this
->rule->
assign
(
[
'cityName' =>
$cityName
, 'operator' =>
$operator
]
)
;
$match
=
$this
->rule->
match
(
$scope
)
;
if
(
$isMatching
)
{
static
::
assertTrue
(
$match
)
;
}
static
::
assertEquals
(
[
new
NotBlank
(
)
,
new
Choice
(
[
Rule::OPERATOR_EQ, Rule::OPERATOR_NEQ, Rule::OPERATOR_EMPTY
]
)
]
,
$constraints
[
'operator'
]
)
;
static
::
assertEquals
(
[
new
NotBlank
(
)
,
new
ArrayOfUuid
(
)
]
,
$constraints
[
'stateIds'
]
)
;
}
/** * @dataProvider getMatchValues */
public
function
testRuleMatching
(
string
$operator
, bool
$isMatching
, string
$stateId
)
: void
{
$countryIds
=
[
'kyln123', 'kyln456'
]
;
$salesChannelContext
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$customerAddress
=
new
CustomerAddressEntity
(
)
;
$customerAddress
->
setCountryStateId
(
$stateId
)
;
$customer
=
new
CustomerEntity
(
)
;
$customer
->
setActiveBillingAddress
(
$customerAddress
)
;
$salesChannelContext
->
method
(
'getCustomer'
)
->
willReturn
(
$customer
)
;
$scope
=
new
CheckoutRuleScope
(
$salesChannelContext
)
;
$this
->rule->
assign
(
[
'stateIds' =>
$countryIds
, 'operator' =>
$operator
]
)
;
$match
=
$this
->rule->
match
(
$scope
)
;
if
(
$isMatching
)
{
static
::
assertTrue
(
$match
)
;
}
catch
(
CartException
$exception
)
{
}
static
::
assertInstanceOf
(
CartException::
class
,
$exception
)
;
static
::
assertStringContainsString
(
'Line item "test" incomplete. Property "label" missing.',
$exception
->
getMessage
(
)
)
;
}
private
function
getCustomer
(
)
: CustomerEntity
{
$faker
= Factory::
create
(
)
;
$billingAddress
=
new
CustomerAddressEntity
(
)
;
$billingAddress
->
setId
(
'SWAG-ADDRESS-ID-1'
)
;
$billingAddress
->
setSalutationId
(
$this
->
getValidSalutationId
(
)
)
;
$billingAddress
->
setFirstName
(
$faker
->firstName
)
;
$billingAddress
->
setLastName
(
$faker
->lastName
)
;
$billingAddress
->
setStreet
(
$faker
->streetAddress
)
;
$billingAddress
->
setZipcode
(
$faker
->postcode
)
;
$billingAddress
->
setCity
(
$faker
->city
)
;
$billingAddress
->
setCountryId
(
'SWAG-AREA-COUNTRY-ID-1'
)
;
$customer
=
new
CustomerEntity
(
)
;
$customer
->
setId
(
'SWAG-CUSTOMER-ID-1'
)
;
#[Package('business-ops')]
class
BillingStreetRuleTest
extends
TestCase
{
public
function
testWithExactMatch
(
)
: void
{
$rule
=
(
new
BillingStreetRule
(
)
)
->
assign
(
[
'streetName' => 'example street'
]
)
;
$cart
=
new
Cart
(
'test'
)
;
$context
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$billing
=
new
CustomerAddressEntity
(
)
;
$billing
->
setStreet
(
'example street'
)
;
$customer
=
new
CustomerEntity
(
)
;
$customer
->
setDefaultBillingAddress
(
$billing
)
;
$context
->
method
(
'getCustomer'
)
->
willReturn
(
$customer
)
;
static
::
assertTrue
(
$rule
->
match
(
new
CartRuleScope
(
$cart
,
$context
)
)
)
;
private
function
getCountry
(
)
: CountryEntity
{
$country
=
new
CountryEntity
(
)
;
$country
->
setId
(
'country-id'
)
;
return
$country
;
}
private
function
getAddress
(
)
: CustomerAddressEntity
{
$address
=
new
CustomerAddressEntity
(
)
;
$address
->
setId
(
'address-id'
)
;
$address
->
setZipcode
(
'2'
)
;
$address
->
setCountry
(
$this
->
getCountry
(
)
)
;
return
$address
;
}
}
private
function
getCountry
(
)
: CountryEntity
{
$country
=
new
CountryEntity
(
)
;
$country
->
setId
(
'country-id'
)
;
return
$country
;
}
private
function
getAddress
(
)
: CustomerAddressEntity
{
$address
=
new
CustomerAddressEntity
(
)
;
$address
->
setId
(
'address-id'
)
;
$address
->
setZipcode
(
'zip-code'
)
;
$address
->
setCountry
(
$this
->
getCountry
(
)
)
;
return
$address
;
}
}
public
function
testCustomFields
(
)
: void
{
$addressRepository
=
$this
->
createMock
(
EntityRepository::
class
)
;
$addressRepository
->
method
(
'searchIds'
)
->
willReturn
(
new
IdSearchResult
(
1,
[
[
'data' =>
[
'address-1'
]
, 'primaryKey' => 'address-1'
]
]
,
new
Criteria
(
)
, Context::
createDefaultContext
(
)
)
)
;
$result
=
$this
->
createMock
(
EntitySearchResult::
class
)
;
$result
->
method
(
'first'
)
->
willReturn
(
new
CustomerAddressEntity
(
)
)
;
$addressRepository
->
method
(
'search'
)
->
willReturn
(
$result
)
;
$addressRepository
->
method
(
'upsert'
)
->
with
(
[
[
'salutationId' => '1',
'firstName' => null,
#[Package('checkout')]
class
UpsertAddressRouteTest
extends
TestCase
{
public
function
testCustomFields
(
)
: void
{
$systemConfigService
=
$this
->
createMock
(
SystemConfigService::
class
)
;
$systemConfigService
->
method
(
'get'
)
->
willReturn
(
'1'
)
;
$result
=
$this
->
createMock
(
EntitySearchResult::
class
)
;
$address
=
new
CustomerAddressEntity
(
)
;
$address
->
setId
(
Uuid::
randomHex
(
)
)
;
$result
->
method
(
'first'
)
->
willReturn
(
$address
)
;
$addressRepository
=
$this
->
createMock
(
EntityRepository::
class
)
;
$addressRepository
->
method
(
'search'
)
->
willReturn
(
$result
)
;
$addressRepository
->
expects
(
static
::
once
(
)
)
->
method
(
'upsert'
)
->
willReturnCallback
(
function
Darray
$data
)
{
static
::
assertSame
(
[
'mapped' => 1
]
,
$data
[
0
]
[
'customFields'
]
)
;
static
::
assertEquals
(
new
ArrayOfType
(
'string'
)
,
$zipCodes
[
1
]
)
;
}
/** * @dataProvider getMatchValuesNumeric */
public
function
testRuleMatchingNumeric
(
string
$operator
, bool
$isMatching
, string
$zipCode
)
: void
{
$zipCodes
=
[
'90210', '81985'
]
;
$salesChannelContext
=
$this
->
createMock
(
SalesChannelContext::
class
)
;
$customerAddress
=
new
CustomerAddressEntity
(
)
;
$customerAddress
->
setZipcode
(
$zipCode
)
;
$location
=
new
ShippingLocation
(
new
CountryEntity
(
)
, null,
$customerAddress
)
;
$salesChannelContext
->
method
(
'getShippingLocation'
)
->
willReturn
(
$location
)
;
$scope
=
new
CheckoutRuleScope
(
$salesChannelContext
)
;
$this
->rule->
assign
(
[
'zipCodes' =>
$zipCodes
, 'operator' =>
$operator
]
)
;
$match
=
$this
->rule->
match
(
$scope
)
;
if
(
$isMatching
)
{
static
::
assertTrue
(
$match
)
;
}
else
{