Skip to content

Commit a35ac9a

Browse files
author
Vaidas Bagdonas
committed
clear MappedBulkSaver even on crash
1 parent f769250 commit a35ac9a

2 files changed

Lines changed: 169 additions & 8 deletions

File tree

src/LogicItLab/Salesforce/MapperBundle/MappedBulkSaver.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,15 @@ private function storeModel($model, $matchField)
5858
$this->bulkModels[$section][] = $model;
5959
}
6060

61-
private function clearModels()
61+
private function clear()
6262
{
6363
$this->bulkModels = [];
64+
$this->bulkSaver->clear();
65+
}
66+
67+
public function itemsInQueue($section)
68+
{
69+
return isset($this->bulkModels[$section]) ? count($this->bulkModels[$section]) : 0;
6470
}
6571

6672
/**
@@ -88,7 +94,7 @@ public function save($model, $matchField = null)
8894
{
8995
$this->storeModel($model, $matchField);
9096

91-
$record = $this->mapper->mapToSalesforceObject($model, null !== $matchField);
97+
$record = $this->mapper->mapToSalesforceObject($model);
9298
$objectMapping = $this->annotationReader->getSalesforceObject($model);
9399

94100
$matchFieldName = null;
@@ -125,12 +131,14 @@ public function delete($model)
125131
*/
126132
public function flush()
127133
{
128-
$results = $this->bulkSaver->flush();
129-
130-
$this->populatModelIds($results, 'upserted');
131-
$this->populatModelIds($results, 'created');
132-
$this->clearModels();
133-
$this->bulkSaver->clear();
134+
try {
135+
$results = $this->bulkSaver->flush();
136+
$this->populatModelIds($results, 'upserted');
137+
$this->populatModelIds($results, 'created');
138+
}
139+
finally {
140+
$this->clear();
141+
}
134142

135143
return $results;
136144
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
namespace LogicItLab\Salesforce\MapperBundle;
4+
5+
use LogicItLab\Salesforce\MapperBundle\Annotation\AnnotationReader;
6+
use LogicItLab\Salesforce\MapperBundle\Model\Account;
7+
use Phpforce\SoapClient\BulkSaver;
8+
use Phpforce\SoapClient\Result\SaveResult;
9+
use PHPUnit\Framework\MockObject\MockObject;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class MappedBulkSaverTest extends TestCase
13+
{
14+
/** @var MappedBulkSaver */
15+
private $mappedBulkSaver;
16+
17+
/** @var BulkSaver|MockObject */
18+
private $bulkSaverMock;
19+
20+
/** @var Mapper|MockObject */
21+
private $mapperMock;
22+
23+
/** @var AnnotationReader|MockObject */
24+
private $annotationReaderMock;
25+
26+
public function setUp(): void
27+
{
28+
$this->bulkSaverMock = $this->createMock(BulkSaver::class);
29+
$this->mapperMock = $this->createMock(Mapper::class);
30+
$this->annotationReaderMock = $this->createMock(AnnotationReader::class);
31+
32+
$this->mappedBulkSaver = new MappedBulkSaver(
33+
$this->bulkSaverMock,
34+
$this->mapperMock,
35+
$this->annotationReaderMock
36+
);
37+
}
38+
39+
public function testFlushesClearsOnException(): void
40+
{
41+
$account = new Account();
42+
$account->setName('Aa');
43+
$account2 = new Account();
44+
$account2->setName('Bb');
45+
46+
$objectMapping = new \stdClass();
47+
$objectMapping->name = 'Account';
48+
49+
$this->annotationReaderMock->expects($this->exactly(2))
50+
->method('getSalesforceObject')
51+
->willReturnOnConsecutiveCalls(
52+
$objectMapping,
53+
$objectMapping
54+
);
55+
56+
$mappedAccount = new \stdClass();
57+
$mappedAccount->Name = $account->getName();
58+
$mappedAccount2 = new \stdClass();
59+
$mappedAccount2->Name = $account2->getName();
60+
61+
$this->mapperMock->expects($this->exactly(2))
62+
->method('mapToSalesforceObject')
63+
->willReturnOnConsecutiveCalls(
64+
$mappedAccount,
65+
$mappedAccount2
66+
);
67+
68+
$this->mappedBulkSaver->save($account);
69+
$this->mappedBulkSaver->save($account2);
70+
71+
$exception = new \Exception('Some exception');
72+
73+
$this->bulkSaverMock->expects($this->once())
74+
->method('flush')
75+
->willThrowException($exception);
76+
77+
$this->assertEquals(2, $this->mappedBulkSaver->itemsInQueue('created'));
78+
79+
$this->bulkSaverMock->expects($this->once())
80+
->method('clear');
81+
82+
$this->expectExceptionObject($exception);
83+
84+
$this->mappedBulkSaver->flush();
85+
86+
$this->assertEquals(0, $this->mappedBulkSaver->itemsInQueue('created'));
87+
}
88+
89+
public function testFlushesAndClearsOnSuccess(): void
90+
{
91+
$account = new Account();
92+
$account->setName('Aa');
93+
$account2 = new Account();
94+
$account2->setName('Bb');
95+
96+
$objectMapping = new \stdClass();
97+
$objectMapping->name = 'Account';
98+
99+
$this->annotationReaderMock->expects($this->exactly(2))
100+
->method('getSalesforceObject')
101+
->willReturnOnConsecutiveCalls(
102+
$objectMapping,
103+
$objectMapping
104+
);
105+
106+
$mappedAccount = new \stdClass();
107+
$mappedAccount->Name = $account->getName();
108+
$mappedAccount2 = new \stdClass();
109+
$mappedAccount2->Name = $account2->getName();
110+
111+
$this->mapperMock->expects($this->exactly(2))
112+
->method('mapToSalesforceObject')
113+
->willReturnOnConsecutiveCalls(
114+
$mappedAccount,
115+
$mappedAccount2
116+
);
117+
118+
$this->mappedBulkSaver->save($account);
119+
$this->mappedBulkSaver->save($account2);
120+
121+
$savedAccount = clone $account;
122+
$savedAccount->setId(1);
123+
124+
$savedAccount2 = clone $account2;
125+
$savedAccount2->setId(1);
126+
127+
$result = $this->createMock(SaveResult::class);
128+
$result->expects($this->any())
129+
->method('isSuccess')
130+
->willReturn(true);
131+
132+
$result2 = $this->createMock(SaveResult::class);
133+
$result2->expects($this->any())
134+
->method('isSuccess')
135+
->willReturn(true);
136+
137+
$this->bulkSaverMock->expects($this->once())
138+
->method('flush')
139+
->willReturn([
140+
'created' => [$result, $result2],
141+
'upserted' => []
142+
]);
143+
144+
$this->assertEquals(2, $this->mappedBulkSaver->itemsInQueue('created'));
145+
146+
$this->bulkSaverMock->expects($this->once())
147+
->method('clear');
148+
149+
$this->mappedBulkSaver->flush();
150+
151+
$this->assertEquals(0, $this->mappedBulkSaver->itemsInQueue('created'));
152+
}
153+
}

0 commit comments

Comments
 (0)