1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class ZohoClientTest extends PHPUnit_Framework_TestCase |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
public function getClient() |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
return new \Wabel\Zoho\CRM\ZohoClient($GLOBALS['auth_token']); |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
public function testGetModules() |
11
|
|
|
{ |
12
|
|
|
$zohoClient = $this->getClient(); |
13
|
|
|
|
14
|
|
|
$modules = $zohoClient->getModules(); |
15
|
|
|
|
16
|
|
|
$found = false; |
17
|
|
|
foreach ($modules->getRecords() as $record) { |
18
|
|
|
if ($record['pl'] == 'Leads') { |
19
|
|
|
$found = true; |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->assertTrue($found); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testGetFields() |
27
|
|
|
{ |
28
|
|
|
$zohoClient = $this->getClient(); |
29
|
|
|
|
30
|
|
|
$fields = $zohoClient->getFields('Leads'); |
31
|
|
|
|
32
|
|
|
$this->assertArrayHasKey('Lead Owner', $fields->getRecords()['Lead Information']); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws Exception |
37
|
|
|
* @throws \Wabel\Zoho\CRM\Exception\ZohoCRMResponseException |
38
|
|
|
*/ |
39
|
|
|
public function testDao() |
40
|
|
|
{ |
41
|
|
|
require_once __DIR__.'/generated/Contact.php'; |
42
|
|
|
require_once __DIR__.'/generated/ContactZohoDao.php'; |
43
|
|
|
|
44
|
|
|
$contactZohoDao = new \TestNamespace\ContactZohoDao($this->getClient()); |
45
|
|
|
|
46
|
|
|
// First, let's clean up the records (in case a previous test did not run correctly). |
47
|
|
|
$records = $contactZohoDao->searchRecords('(First Name:TestMultiplePoolUser)'); |
48
|
|
|
foreach ($records as $record) { |
49
|
|
|
$contactZohoDao->delete($record->getZohoId()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// |
|
|
|
|
53
|
|
|
// $lastName = uniqid("Test"); |
54
|
|
|
// $email = $lastName."@test.com"; |
55
|
|
|
// |
56
|
|
|
// $contactBean = new \TestNamespace\Contact(); |
57
|
|
|
// $contactBean->setFirstName("Testuser"); |
58
|
|
|
// $contactBean->setLastName($lastName); |
59
|
|
|
// // Testing special characters. |
60
|
|
|
// $contactBean->setTitle("M&M's épatant"); |
61
|
|
|
// |
62
|
|
|
// $contactZohoDao->save($contactBean); |
63
|
|
|
// |
64
|
|
|
// $this->assertNotEmpty($contactBean->getZohoId(), "ZohoID must be set in the bean after save."); |
65
|
|
|
// |
66
|
|
|
// // Second save (to verify the updateRecords method). |
67
|
|
|
// $contactBean->setEmail($email); |
68
|
|
|
// $contactZohoDao->save($contactBean); |
69
|
|
|
// $record = $contactZohoDao->getById($contactBean->getZohoId()); |
70
|
|
|
// $this->assertEquals($record[0]->getEmail(), $email); |
71
|
|
|
// |
72
|
|
|
// $multipleContact = []; |
73
|
|
|
// // Now, let's test multiple saves under 100. |
74
|
|
|
// for($i = 0; $i < 98; $i++) { |
75
|
|
|
// $multipleContact["contact"][$i] = new \TestNamespace\Contact(); |
76
|
|
|
// $multipleContact["lastName"][$i] = uniqid("Test"); |
77
|
|
|
// $multipleContact["email"][$i] = $multipleContact["lastName"][$i]."@test.com"; |
78
|
|
|
// $multipleContact["contact"][$i]->setLastName($multipleContact["lastName"][$i]); |
79
|
|
|
// $multipleContact["contact"][$i]->setFirstName("TestMultipleUser"); |
80
|
|
|
// } |
81
|
|
|
// $contactZohoDao->save($multipleContact["contact"]); |
82
|
|
|
// |
83
|
|
|
// |
84
|
|
|
// for($i = 0; $i < 98; $i++) { |
85
|
|
|
// $multipleContact["contact"][$i]->setEmail($multipleContact["email"][$i]); |
86
|
|
|
// } |
87
|
|
|
// $contactZohoDao->save($multipleContact["contact"]); |
88
|
|
|
|
89
|
|
|
// Now, let's test multiple saves over 100. |
90
|
|
|
$multiplePoolContact = []; |
91
|
|
|
for ($i = 0; $i < 302; ++$i) { |
92
|
|
|
$multiplePoolContact['contact']['key'.$i] = new \TestNamespace\Contact(); |
93
|
|
|
$multiplePoolContact['lastName']['key'.$i] = uniqid('Test'); |
94
|
|
|
$multiplePoolContact['email']['key'.$i] = $multiplePoolContact['lastName']['key'.$i].'@test.com'; |
95
|
|
|
$multiplePoolContact['contact']['key'.$i]->setLastName($multiplePoolContact['lastName']['key'.$i]); |
96
|
|
|
$multiplePoolContact['contact']['key'.$i]->setFirstName('TestMultiplePoolUser'); |
97
|
|
|
} |
98
|
|
|
$contactZohoDao->save($multiplePoolContact['contact']); |
99
|
|
|
|
100
|
|
|
for ($i = 0; $i < 302; ++$i) { |
101
|
|
|
$multiplePoolContact['contact']['key'.$i]->setEmail($multiplePoolContact['email']['key'.$i]); |
102
|
|
|
} |
103
|
|
|
$beforePoolMultiple = new DateTime(); |
|
|
|
|
104
|
|
|
$contactZohoDao->save($multiplePoolContact['contact']); |
105
|
|
|
|
106
|
|
|
for ($i = 0; $i < 302; ++$i) { |
107
|
|
|
$this->assertNotNull($multiplePoolContact['contact']['key'.$i]->getZohoId()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// We need to wait for Zoho to index the record. |
111
|
|
|
sleep(120); |
112
|
|
|
|
113
|
|
|
// Test if the unique Contact has been well saved and is deleted |
|
|
|
|
114
|
|
|
// $records = $contactZohoDao->searchRecords("(Last Name:$lastName)"); |
115
|
|
|
// |
116
|
|
|
// $this->assertCount(1, $records); |
117
|
|
|
// foreach ($records as $record) { |
118
|
|
|
// $this->assertInstanceOf("\\TestNamespace\\Contact", $record); |
119
|
|
|
// $this->assertEquals("Testuser", $record->getFirstName()); |
120
|
|
|
// $this->assertEquals($lastName, $record->getLastName()); |
121
|
|
|
// $this->assertEquals($email, $record->getEmail()); |
122
|
|
|
// $this->assertEquals("M&M's épatant", $record->getTitle()); |
123
|
|
|
// } |
124
|
|
|
// |
125
|
|
|
// $contactZohoDao->delete($contactBean->getZohoId()); |
126
|
|
|
// |
127
|
|
|
// $records = $contactZohoDao->searchRecords("(Last Name:$lastName)"); |
128
|
|
|
// $this->assertCount(0, $records); |
129
|
|
|
// |
130
|
|
|
// // Test if the 98 Contacts has been well saved and are deleted |
131
|
|
|
// $records = $contactZohoDao->searchRecords("(First Name:TestMultipleUser)"); |
132
|
|
|
// |
133
|
|
|
// $this->assertCount(98, $records); |
134
|
|
|
// foreach ($records as $key=>$record) { |
135
|
|
|
// $this->assertInstanceOf("\\TestNamespace\\Contact", $record); |
136
|
|
|
// $this->assertEquals("TestMultipleUser", $record->getFirstName()); |
137
|
|
|
// $contactZohoDao->delete($record->getZohoId()); |
138
|
|
|
// } |
139
|
|
|
// |
140
|
|
|
// $records = $contactZohoDao->searchRecords("(First Name:TestMultipleUser)"); |
141
|
|
|
// $this->assertCount(0, $records); |
142
|
|
|
|
143
|
|
|
$records = $contactZohoDao->searchRecords('(First Name:TestMultiplePoolUser)'); |
144
|
|
|
$this->assertCount(302, $records); |
145
|
|
|
|
146
|
|
|
$modifiedTime = $records[0]->getModifiedTime()->sub(DateInterval::createFromDateString('10 seconds')); |
147
|
|
|
|
148
|
|
|
// Test if the 302 Contacts has been well saved and are deleted |
149
|
|
|
//$records = $contactZohoDao->getRecords("Modified Time", "asc", $modifiedTime); |
|
|
|
|
150
|
|
|
$records = $contactZohoDao->getRecords(null, null, $modifiedTime); |
151
|
|
|
|
152
|
|
|
$beforeDeleteTime = new DateTimeImmutable(); |
153
|
|
|
|
154
|
|
|
$this->assertCount(302, $records); |
155
|
|
|
foreach ($records as $key => $record) { |
156
|
|
|
$this->assertInstanceOf('\\TestNamespace\\Contact', $record); |
157
|
|
|
$this->assertEquals('TestMultiplePoolUser', $record->getFirstName()); |
158
|
|
|
$this->assertContains($record->getLastName(), $multiplePoolContact['lastName']); |
159
|
|
|
$this->assertContains($record->getEmail(), $multiplePoolContact['email']); |
160
|
|
|
$contactZohoDao->delete($record->getZohoId()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$records2 = $contactZohoDao->searchRecords('(First Name:TestMultiplePoolUser)'); |
164
|
|
|
$this->assertCount(0, $records2); |
165
|
|
|
|
166
|
|
|
$deletedRecords = $contactZohoDao->getDeletedRecordIds($beforeDeleteTime->sub(new DateInterval('PT3M'))); |
167
|
|
|
// Let's check that each deleted record is present in the deleted record list. |
168
|
|
|
foreach ($records as $record) { |
169
|
|
|
$this->assertContains($record->getZohoId(), $deletedRecords); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @throws Exception |
175
|
|
|
* @throws \Wabel\Zoho\CRM\Exception\ZohoCRMResponseException |
176
|
|
|
*/ |
177
|
|
|
public function testDaoUpdateException() |
178
|
|
|
{ |
179
|
|
|
require_once __DIR__.'/generated/Contact.php'; |
180
|
|
|
require_once __DIR__.'/generated/ContactZohoDao.php'; |
181
|
|
|
|
182
|
|
|
$contactZohoDao = new \TestNamespace\ContactZohoDao($this->getClient()); |
183
|
|
|
|
184
|
|
|
// Now, let's try to update records that do not exist. |
185
|
|
|
$notExistingRecords = array(); |
186
|
|
|
|
187
|
|
|
$contact1 = new \TestNamespace\Contact(); |
188
|
|
|
$contact1->setZohoId('123495843958439432'); |
189
|
|
|
$contact1->setLastName('I DONT EXIST'); |
190
|
|
|
$contact1->setFirstName('I DONT EXIST'); |
191
|
|
|
$contact1->setEmail('[email protected]'); |
192
|
|
|
$notExistingRecords[] = $contact1; |
193
|
|
|
|
194
|
|
|
$contact2 = new \TestNamespace\Contact(); |
195
|
|
|
$contact2->setZohoId('54349584395439432'); |
196
|
|
|
$contact2->setLastName('I DONT EXIST AT ALL'); |
197
|
|
|
$contact2->setFirstName('I DONT EXIST AT ALL'); |
198
|
|
|
$contact2->setEmail('[email protected]'); |
199
|
|
|
$notExistingRecords[] = $contact2; |
200
|
|
|
|
201
|
|
|
$updateExceptionTriggered = false; |
202
|
|
|
try { |
203
|
|
|
$contactZohoDao->updateRecords($notExistingRecords); |
204
|
|
|
} catch (\Wabel\Zoho\CRM\Exception\ZohoCRMUpdateException $updateException) { |
205
|
|
|
$updateExceptionTriggered = true; |
206
|
|
|
$failedBeans = $updateException->getFailedBeans(); |
207
|
|
|
$this->assertTrue($failedBeans->offsetExists($contact1)); |
208
|
|
|
$this->assertTrue($failedBeans->offsetExists($contact2)); |
209
|
|
|
$innerException = $failedBeans->offsetGet($contact1); |
210
|
|
|
$this->assertEquals('401.2', $innerException->getZohoCode()); |
211
|
|
|
$this->assertEquals(2, $failedBeans->count()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$this->assertTrue($updateExceptionTriggered); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
protected function tearDown() |
218
|
|
|
{ |
219
|
|
|
if (class_exists('\TestNamespace\ContactZohoDao')) { |
220
|
|
|
$contactZohoDao = new \TestNamespace\ContactZohoDao($this->getClient()); |
221
|
|
|
// Let's end by removing past inserted clients: |
222
|
|
|
$pastContacts = $contactZohoDao->searchRecords('(First Name:TestMultiplePoolUser)'); |
223
|
|
|
foreach ($pastContacts as $pastContact) { |
224
|
|
|
$contactZohoDao->delete($pastContact->getZohoId()); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.