Skip to content

Commit

Permalink
Prevent an exception when a contact is deleted between the time logs … (
Browse files Browse the repository at this point in the history
#7443)

Prevent an exception when a contact is deleted between the time logs …
  • Loading branch information
kuzmany committed May 19, 2019
2 parents 4947c82 + 10ef69e commit 2991302
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Expand Up @@ -63,9 +63,14 @@ public function hydrateContacts(ArrayCollection $logs)

$contacts = $this->leadRepository->getContactCollection($contactIds);

foreach ($logs as $log) {
foreach ($logs as $key => $log) {
$contactId = $log->getLead()->getId();
$contact = $contacts->get($contactId);
if (!$contact = $contacts->get($contactId)) {
// the contact must have been deleted mid execution so remove this log from memory
$logs->remove($key);

continue;
}

$log->setLead($contact);
}
Expand Down
Expand Up @@ -94,6 +94,58 @@ public function testHydratedLeadsFromRepositoryAreFoundAndPushedIntoLogs()
$this->getContactFinder()->hydrateContacts($logs);
}

public function testHydratedLeadsFromRepositoryWithMissingLeadResultsLogBeingRemoved()
{
$lead1 = $this->getMockBuilder(Lead::class)
->getMock();
$lead1->expects($this->exactly(2))
->method('getId')
->willReturn(1);

$lead2 = $this->getMockBuilder(Lead::class)
->getMock();
$lead2->expects($this->exactly(2))
->method('getId')
->willReturn(2);

$log1 = $this->getMockBuilder(LeadEventLog::class)
->getMock();
$log1->expects($this->exactly(2))
->method('getLead')
->willReturn($lead1);
$log1->expects($this->once())
->method('setLead');

$log2 = $this->getMockBuilder(LeadEventLog::class)
->getMock();
$log2->expects($this->exactly(2))
->method('getLead')
->willReturn($lead2);
$log2->expects($this->never())
->method('setLead');

$logs = new ArrayCollection(
[
1 => $log1,
2 => $log2,
]
);

$contacs = new ArrayCollection(
[
1 => $lead1,
]
);

$this->leadRepository->expects($this->once())
->method('getContactCollection')
->willReturn($contacs);

$this->getContactFinder()->hydrateContacts($logs);

$this->assertCount(1, $logs);
}

public function testNoContactsFoundExceptionIsThrownIfEntitiesAreNotFound()
{
$this->leadRepository->expects($this->never())
Expand Down

0 comments on commit 2991302

Please sign in to comment.