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