// create unrelated newsletter recipient which should not be involved when updating the customer's newsletter sales channel ids
$this->
createNewsletterRecipient($context, 'foobar@example.com', TestDefaults::SALES_CHANNEL
);
// subscribe to default sales channel and assert that array contains only that id
$newsletterRecipientA =
$this->
createNewsletterRecipient($context,
$email, TestDefaults::SALES_CHANNEL
);
/** @var EntityRepository<CustomerCollection> $customerRepository */
$customerRepository =
$this->
getContainer()->
get('customer.repository'
);
$customer =
$customerRepository->
search(new Criteria([$customerId]),
$context)->
getEntities()->
first();
static::
assertNotNull($customer);
static::
assertNotNull($customer->
getNewsletterSalesChannelIds());
static::
assertCount(1,
$customer->
getNewsletterSalesChannelIds());
static::
assertContains(TestDefaults::SALES_CHANNEL,
$customer->
getNewsletterSalesChannelIds());
// subscribe to alternative sales channel and assert that array contains ids of both sales channels
$newsletterRecipientB =
$this->
createNewsletterRecipient($context,
$email,
$alternativeSalesChannel['id'
], NewsletterSubscribeRoute::STATUS_DIRECT
);
$customer =
$customerRepository->
search(new Criteria([$customerId]),
$context)->
getEntities()->
first();
static::
assertNotNull($customer);
static::
assertNotNull($customer->
getNewsletterSalesChannelIds());
static::
assertCount(2,
$customer->
getNewsletterSalesChannelIds());