Passed
Pull Request — master (#195)
by Jonathan
03:35 queued 24s
created

SforcePartnerClientTest   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 481
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 55
eloc 309
c 1
b 0
f 0
dl 0
loc 481
rs 6

How to fix   Complexity   

Complex Class

Complex classes like SforcePartnerClientTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SforcePartnerClientTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
require_once ('PHPUnit/Framework/TestCase.php');
3
require_once ('../soapclient/SforcePartnerClient.php');
4
require_once ('../soapclient/SforceHeaderOptions.php');
5
6
/*
7
 * Copyright (c) 2007, salesforce.com, inc.
8
 * All rights reserved.
9
 *
10
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
11
 * that the following conditions are met:
12
 *
13
 *    Redistributions of source code must retain the above copyright notice, this list of conditions and the
14
 *    following disclaimer.
15
 *
16
 *    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
17
 *    the following disclaimer in the documentation and/or other materials provided with the distribution.
18
 *
19
 *    Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or
20
 *    promote products derived from this software without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 * POSSIBILITY OF SUCH DAMAGE.
30
 */
31
32
/**
33
 * This file contains one class.
34
 * @package SalesforceTest
35
 */
36
/**
37
 * SforcePartnerClientTest class.
38
 *
39
 * @package SalesforceTest
40
 */
41
class SforcePartnerClientTest extends PHPUnit_Framework_TestCase {
42
  private $wsdl = '../soapclient/partner.wsdl.xml';
43
  private $username = '[email protected]';
44
  private $password = 'changeme';
45
  protected $mySforceConnection = null;
46
  protected $mylogin = null;
47
  protected $theId = null;
48
49
  public function deleteAll($queryResult) {
50
    $records = $queryResult->records;
51
    $ids = array ();
52
    $buckets = array_chunk($records, 200);
53
    foreach ($buckets as $bucket) {
54
      $ids = array ();
55
      foreach ($bucket as $record) {
56
        $sObject = new SObject($record);
57
        //var_dump($sObject);
58
        $ids[] = $sObject->Id;
59
      }
60
      try {
61
        global $mySforceConnection;
62
        $this->mySforceConnection->delete($ids);
63
      } catch (Exception $e) {
64
        print_r($e->faultstring);
65
      }
66
    }
67
68
    $queryLocator = $queryResult->queryLocator;
69
    if (isset($queryLocator)) {
70
      $result = $this->mySforceConnection->queryMore($queryLocator);
71
      $this->deleteAll($result);
72
    }
73
  }
74
75
  protected function setUp() {
76
    echo "Begin Test Setup\r\n";
77
    try {
78
      // Create the fixtures.
79
      $this->mySforceConnection = new SforcePartnerClient();
80
      $this->mySforceConnection->createConnection($this->wsdl);
81
      $this->mylogin = $this->mySforceConnection->login($this->username, $this->password);
82
83
      // CLEAN
84
      $queryOptions = new QueryOptions(300);
85
      $createQuery = 'SELECT Id from Contact where FirstName = \'DELETE_ME\'';
86
      $leadQuery = 'SELECT Id from Lead where FirstName = \'DELETE_ME\'';
87
      $createQueryResult = $this->mySforceConnection->query($createQuery, $queryOptions);
88
      $leadQueryResult = $this->mySforceConnection->query($leadQuery, $queryOptions);
89
      if ($createQueryResult->size > 0) {
90
        echo 'Deleting '.$createQueryResult->size." contacts.\r\n";
91
        $this->deleteAll($createQueryResult);
92
      }
93
      if ($leadQueryResult->size > 0) {
94
        $this->deleteAll($leadQueryResult);
95
      }
96
      $createFields = array (
97
        'FirstName' => 'DELETE_ME',
98
        'LastName' => 'DELETE_ME',
99
        'MailingCity' => 'San Diego',
100
        'MailingState' => 'CA'
101
        );
102
        $sObject1 = new SObject();
103
        $sObject1->fields = $createFields;
104
        $sObject1->type = 'Contact';
105
        $createResponse = $this->mySforceConnection->create(array($sObject1));
106
        $this->assertNotNull($createResponse);
107
        $this->assertTrue($createResponse->success);
108
    } catch (SoapFault $fault) {
109
      $this->fail($fault->faultstring);
110
    }
111
    $this->theId = $createResponse->id;
112
    echo "Test setup complete.\r\n";
113
  }
114
115
  public function testLogin() {
116
    echo "testLogin\r\n";
117
    try {
118
      $mylogin = $this->mylogin;
119
      $this->assertNotNull($mylogin);
120
      $this->assertNotNull($mylogin->userInfo);
121
    } catch (SoapFault $fault) {
122
      $this->fail($fault->faultstring);
123
    }
124
  }
125
126
  public function testServerTimestamp() {
127
    echo "testServerTimestamp\r\n";
128
    try {
129
      $timeStamp = $this->mySforceConnection->getServerTimestamp();
130
      $this->assertNotNull($timeStamp);
131
      $this->assertNotNull($timeStamp->timestamp);
132
    } catch (SoapFault $fault) {
133
      $this->fail($fault->faultstring);
134
    }
135
  }
136
137
  public function testUserInfo() {
138
    echo "testUserInfo\r\n";
139
    try {
140
      $userInfo = $this->mySforceConnection->getUserInfo();
141
      $this->assertNotNull($userInfo);
142
      $this->assertNotNull($userInfo->userId);
143
      $this->assertNotNull($userInfo->userFullName);
144
    } catch (SoapFault $fault) {
145
      $this->fail($fault->faultstring);
146
    }
147
  }
148
149
  public function testDescribeSObject() {
150
    echo "testDescribeSObject\r\n";
151
    try {
152
      $response = $this->mySforceConnection->describeSObject('Contact');
153
      $this->assertNotNull($response);
154
      $this->assertEquals('Contact', $response->name);
155
    } catch (SoapFault $fault) {
156
      echo $fault;
157
      $this->fail($fault->faultstring);
158
    }
159
  }
160
161
  public function testDescribeSObjects() {
162
    echo "testDescribeSObjects\r\n";
163
    try {
164
      $response = $this->mySforceConnection->describeSObjects(array (
165
        'Account',
166
        'Contact'
167
        ));
168
        $this->assertNotNull($response);
169
        $this->assertEquals(2, sizeof($response));
170
    } catch (SoapFault $fault) {
171
      $this->fail($fault->faultstring);
172
    }
173
  }
174
175
  public function testDescribeGlobal() {
176
    echo "testDescribeGlobal\r\n";
177
    try {
178
      $response = $this->mySforceConnection->describeGlobal();
179
      $this->assertNotNull($response);
180
      $this->assertNotNull($response->types);
181
    } catch (SoapFault $fault) {
182
      $this->fail($fault->faultstring);
183
    }
184
  }
185
186
  public function testDescribeLayout() {
187
    echo "testDescribeLayout\r\n";
188
    try {
189
      $response = $this->mySforceConnection->describeLayout('Contact');
190
      $this->assertNotNull($response);
191
      $this->assertNotNull($response->recordTypeMappings);
192
    } catch (SoapFault $fault) {
193
      $this->fail($fault->faultstring);
194
    }
195
  }
196
197
  public function testDescribeTabs() {
198
    echo "testDescribeTabs\r\n";
199
    try {
200
      $response = $this->mySforceConnection->describeTabs();
201
      $this->assertNotNull($response);
202
      $this->assertTrue(sizeof($response) > 0);
203
    } catch (SoapFault $fault) {
204
      $this->fail($fault->faultstring);
205
    }
206
  }
207
208
  public function testCreate() {
209
    // now done in setUp so that theId can be accessed by other tests.
210
  }
211
212
  public function testRetrieve() {
213
    echo "testRetrieve\r\n";
214
    try {
215
      $ids = array ($this->theId);
216
      $this->assertEquals(1, count($ids));
217
218
      $retrieveFields = 'LastName';
219
      $retrieveResponse = $this->mySforceConnection->retrieve($retrieveFields, 'Contact', $ids);
220
      $sObject = new SObject($retrieveResponse);
221
      $this->assertEquals($this->theId, $sObject->Id);
222
      // Fields on SObjects are SimpleXMLElements.
223
      //$this->assertEquals('DELETE_ME', $sObject->fields->LastName);
224
      //$this->assertEquals('San Diego', $sObject->MailingCity);
225
      //$this->assertEquals('CA', $sObject->MailingState);
226
    } catch (SoapFault $fault) {
227
      $this->fail($fault->faultstring);
228
    }
229
  }
230
231
  public function testUpdate() {
232
    echo "testUpdate\r\n";
233
    try {
234
      $id = $this->theId;
235
      $this->assertNotNull($id);
236
      $updateFields = array (
237
        'Id' => $id,
238
        'MailingCity' => 'New York',
239
        'MailingState' => 'NY'
240
        );
241
        $sObject1 = new SObject();
242
        $sObject1->fields = $updateFields;
243
        $sObject1->type = 'Contact';
244
        $updateResponse = $this->mySforceConnection->update(array ($sObject1));
245
        $this->assertNotNull($updateResponse);
246
        $this->assertTrue($updateResponse->success);
247
    } catch (SoapFault $fault) {
248
      $this->fail($fault->faultstring);
249
    }
250
  }
251
252
  public function testGetUpdated() {
253
    echo "testGetUpdated\r\n";
254
    try {
255
      $type = 'Contact';
256
      $currentTime = mktime();
257
      // assume that create or update occured within the last 5 mins.
258
      $startTime = $currentTime-(60*5);
259
      $endTime = $currentTime;
260
      $response = $this->mySforceConnection->getUpdated($type, $startTime, $endTime);
261
      $this->assertNotNull($response);
262
263
    } catch (SoapFault $fault) {
264
      $this->fail($fault->faultstring);
265
    }
266
  }
267
268
  public function testDelete() {
269
    echo "testDelete\r\n";
270
    try {
271
      $ids = array ($this->theId);
272
      $this->assertEquals(1, count($ids));
273
      $response = $this->mySforceConnection->delete($ids);
274
      $this->assertNotNull($response);
275
      $this->assertTrue($response->success);
276
    } catch (SoapFault $fault) {
277
      $this->fail($fault->faultstring);
278
    }
279
  }
280
281
  public function testGetDeleted() {
282
    echo "testGetDeleted\r\n";
283
    try {
284
      $type = 'Contact';
285
      $currentTime = mktime();
286
      // assume that delete occured within the last 5 mins.
287
      $startTime = $currentTime-(60*10);
288
      $endTime = $currentTime;
289
      $response = $this->mySforceConnection->getDeleted($type, $startTime, $endTime);
290
      if (isset($response)) {
291
        $this->assertNotNull(count($response->deletedRecords));
292
        echo'deleted records = '.count($response->deletedRecords)."\r\n";
293
      }
294
    } catch (SoapFault $fault) {
295
      $this->fail($fault->faultstring);
296
    }
297
  }
298
299
  public function testQuery() {
300
    echo "testQuery\r\n";
301
    $query = 'SELECT Id,Name,BillingCity,BillingState,Phone,Fax from Account';
302
    $queryOptions = new QueryOptions(200);
303
    try {
304
      $response = $this->mySforceConnection->query($query, $queryOptions);
305
      $this->assertNotNull($response);
306
      $this->assertTrue(sizeof($response) > 0);
307
    } catch (SoapFault $fault) {
308
      $this->fail($fault->faultstring);
309
    }
310
  }
311
  /*
312
   public function testQueryMore() {
313
   echo "testQueryMore\r\n";
314
   $createFields = array (
315
   'FirstName' => 'DELETE_ME',
316
   'LastName' => 'DELETE_ME',
317
   'MailingCity' => 'San Diego',
318
   'MailingState' => 'CA'
319
   );
320
321
   $sObject1 = new SObject();
322
   $sObject1->fields = $createFields;
323
   $sObject1->type = 'Contact';
324
325
   $createNum = 500;
326
   try {
327
   for ($counter = 0; $counter < $createNum; $counter++) {
328
   $createResponse = $this->mySforceConnection->create(array ($sObject1));
329
   $this->assertTrue($createResponse->success);
330
   }
331
332
   $query = 'SELECT Id from Contact where FirstName = \'DELETE_ME\'';
333
   $queryOptions = new QueryOptions(300);
334
335
   $queryResponse = $this->mySforceConnection->query($query, $queryOptions);
336
   $this->assertEquals($createNum+1, $queryResponse->size);
337
   if ($queryResponse->size) {
338
   $this->deleteAll($queryResponse);
339
   }
340
   $queryResponse = $this->mySforceConnection->query($query, $queryOptions);
341
   $this->assertEquals(0, $queryResponse->size);
342
   } catch (SoapFault $fault) {
343
   $this->fail($fault->faultstring);
344
   }
345
   }
346
   */
347
348
  public function testUpsert() {
349
    // Make the language field an external id field on the contact
350
    // table before proceeding.
351
    echo "testUpsert\r\n";
352
    $createFields = array (
353
      'FirstName' => 'DELETE_ME',
354
      'LastName' => 'DELETE_ME',
355
      'MailingCity' => 'TEST_UPSERT',
356
      'MailingState' => 'CA',
357
      'Languages__c' => 'TEST_UPSERT_ID'
358
      );
359
      $external_id = 'Languages__c';
360
      try {
361
        $sObject1 = new SObject();
362
        $sObject1->fields = $createFields;
363
        $sObject1->type = 'Contact';
364
        $upsertResponse = $this->mySforceConnection->upsert($external_id, array($sObject1));
365
        $this->assertNotNull($upsertResponse);
366
        $this->assertTrue($upsertResponse->success);
367
        $id = $upsertResponse->id;
368
        $ids = array ($id);
369
        $retrieveFields = 'Id, FirstName, LastName, MailingCity, MailingState';
370
        $retrieveResponse = $this->mySforceConnection->retrieve($retrieveFields, 'Contact', $ids);
371
        $this->assertNotNull($retrieveResponse);
372
        $sObject = new SObject($retrieveResponse);
373
        $this->assertEquals($id, $sObject->Id);
374
      } catch (SoapFault $fault) {
375
        $this->fail($fault->faultstring);
376
      }
377
  }
378
379
  public function testAssignmentRuleHeaderId() {
380
    echo "testAssignmentRuleHeaderId\r\n";
381
    $header = new AssignmentRuleHeader('01Q300000005lDgEAI', NULL);
382
    $createFields = array (
383
      'FirstName' => 'DELETE_ME',
384
      'LastName' => 'DELETE_ME',
385
      'Email' => '[email protected]',
386
      'Company' => 'DELETE_ME Company',
387
      'LeadSource' => 'PHPUnit2',
388
      'City' => 'San Diego',
389
      'State' => 'CA'
390
      );
391
      $sObject1 = new SObject();
392
      $sObject1->fields = $createFields;
393
      $sObject1->type = 'Lead';
394
      try {
395
        $createResponse = $this->mySforceConnection->create(array ($sObject1), $header, NULL);
396
      } catch (SoapFault $fault) {
397
        $this->fail($fault->faultstring);
398
      }
399
  }
400
401
  public function testAssignmentRuleHeaderFlag() {
402
    echo "testAssignmentRuleHeaderFlag\r\n";
403
    $header = new AssignmentRuleHeader(NULL, TRUE);
404
    $createFields = array (
405
      'FirstName' => 'DELETE_ME',
406
      'LastName' => 'DELETE_ME',
407
      'Email' => '[email protected]',
408
      'Company' => 'DELETE_ME Company',
409
      'LeadSource' => 'PHPUnit2',
410
      'City' => 'San Diego',
411
      'State' => 'CA'
412
      );
413
      $sObject1 = new SObject();
414
      $sObject1->fields = $createFields;
415
      $sObject1->type = 'Lead';
416
      try {
417
        $createResponse = $this->mySforceConnection->create(array ($sObject1), $header, NULL);
418
        $this->assertNotNull($createResponse);
419
        $this->assertTrue($createResponse->success);
420
      } catch (SoapFault $fault) {
421
        $this->fail($fault->faultstring);
422
      }
423
  }
424
425
  public function testMruHeader() {
426
    echo "testMruHeader\r\n";
427
    $header = new MruHeader(TRUE);
428
    $createFields = array (
429
      'FirstName' => 'DELETE_ME',
430
      'LastName' => 'DELETE_ME',
431
      'MailingCity' => 'San Diego',
432
      'MailingState' => 'CA'
433
      );
434
      $sObject1 = new SObject();
435
      $sObject1->fields = $createFields;
436
      $sObject1->type = 'Contact';
437
      try {
438
        $createResponse = $this->mySforceConnection->create(array ($sObject1), NULL, $header);
439
        $this->assertNotNull($createResponse);
440
        $this->assertTrue($createResponse->success);
441
      } catch (SoapFault $fault) {
442
        $this->fail($fault->faultstring);
443
      }
444
  }
445
446
  public function testSearch() {
447
    echo "testSearch\r\n";
448
    try {
449
      $searchResponse
450
      = $this->mySforceConnection->search('FIND {DELETE_ME} returning contact(id, phone, firstname, lastname)');
451
      $this->assertNotNull($searchResponse);
452
    } catch (SoapFault $fault) {
453
      $this->fail($fault->faultstring);
454
    }
455
  }
456
457
  public function testSendSingleEmail() {
458
    echo "testSendSingleEmail\r\n";
459
    try {
460
      $singleEmail1 = new SingleEmailMessage();
461
      $singleEmail1->toAddresses = "[email protected]";
462
      $singleEmail1->plainTextBody = "Hello there";
463
      $singleEmail1->subject = "First Single Email";
464
      $singleEmail1->saveAsActivity = true;
465
      $singleEmail1->emailPriority = EMAIL_PRIORITY_LOW;
466
467
      $singleEmail2 = new SingleEmailMessage();
468
      $singleEmail2->toAddresses = "[email protected]";
469
      $singleEmail2->plainTextBody = "Hello there";
470
      $singleEmail2->subject = "Second Single Email";
471
      $singleEmail2->saveAsActivity = true;
472
      $singleEmail2->emailPriority = EMAIL_PRIORITY_LOW;
473
474
      $emailResponse = $this->mySforceConnection->sendSingleEmail(array ($singleEmail1, $singleEmail2));
475
      $this->assertNotNull($emailResponse);
476
    } catch (SoapFault $fault) {
477
      $this->fail($fault->faultstring);
478
    }
479
  }
480
481
  public function testSendMassEmail() {
482
    echo "testSendMassEmail\r\n";
483
    try {
484
      $massEmail = new MassEmailMessage();
485
      $massEmail->subject = "Nicks Mass Email Message";
486
      $massEmail->saveAsActivity = true;
487
      $massEmail->emailPriority = EMAIL_PRIORITY_LOW;
488
      $massEmail->templateId = "00X50000000wX9q";
489
      $massEmail->targetObjectIds = array ("0035000000PiCMd");
490
491
      $emailResponse = $this->mySforceConnection->sendMassEmail(array($massEmail));
492
493
      $this->assertNotNull($emailResponse);
494
    } catch (SoapFault $fault) {
495
      $this->fail($fault->faultstring);
496
    }
497
  }
498
499
  public function testEmptyRecycleBin() {
500
    echo "testEmptyRecycleBin\r\n";
501
    try {
502
      $fields = array (
503
      'Type' => 'Electrical'
504
      );
505
506
      $sObject = new SObject();
507
      $sObject->fields = $fields;
508
      $sObject->type = 'Case';
509
510
      //echo "Creating Case:\n";
511
      $response = $this->mySforceConnection->create(array ($sObject));
512
      $this->assertNotNull($response);
513
      $id = $response->id;
514
      //echo "Deleted Case:\n";
515
      $response = $this->mySforceConnection->delete(array($id));
516
      $this->assertNotNull($response);
517
      //echo "Empty Recycle Bin:\n";
518
      $response = $this->mySforceConnection->emptyRecycleBin(array($id));
519
      $this->assertNotNull($response);
520
    } catch (SoapFault $fault) {
521
      $this->fail($fault->faultstring);
522
    }
523
  }
524
525
526
}
527
?>