Skip to content

Commit

Permalink
Merge pull request #10 from pikulsky/pikulsky/issue-9-convert-lead
Browse files Browse the repository at this point in the history
Pikulsky/issue 9 convert lead
  • Loading branch information
cristianpontes authored Jun 19, 2017
2 parents 1ae4515 + 1957093 commit 4c15e70
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 21 deletions.
161 changes: 161 additions & 0 deletions Request/ConvertLead.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
namespace CristianPontes\ZohoCRMClient\Request;

use CristianPontes\ZohoCRMClient\Response\MutationResult;

/**
* ConvertLead API Call
*
* You can use this method to convert lead to potential, account and contact.
* You can also associate the existing contact or account to the converting lead
* using this method.
*
* @see https://www.zoho.com/crm/help/api/convertlead.html
*/
class ConvertLead extends AbstractRequest
{
/** @var array */
private $record = array();

/**
* Returns option field names
* @return array
*/
static public function getOptionFields()
{
return array(
'createPotential',
'assignTo',
'notifyLeadOwner',
'notifyNewEntityOwner',
);
}

protected function configureRequest()
{
$this->request
->setMethod('convertLead')
->setParam('newFormat', '1')
->setParam('version', 2);
}

/**
* Specify unique lead ID to convert
* @param $leadId
* @return ConvertLead
*/
public function setLeadId($leadId)
{
$this->request->setParam('leadId', $leadId);
return $this;
}

/**
* @param array $record Record as a simple associative array
* @return ConvertLead
*/
public function addRecord(array $record)
{
$this->record = array_merge($this->record, $record);
return $this;
}

/**
* Specify the existing Account to associate the converting lead
* @param string $accountId
* @return ConvertLead
*/
public function setAccountId($accountId)
{
$this->record['ACCOUNTID'] = $accountId;
return $this;
}

/**
* Specify the existing Contact the converting lead should be associated to
* @param string $contactId
* @return ConvertLead
*/
public function setContactId($contactId)
{
$this->record['CONTACTID'] = $contactId;
return $this;
}

/**
* Specify if Account name will be replaced by the Company name
* while associating with the existing account
* @param bool $overwrite
* @return ConvertLead
*/
public function setOverwriteAccountName($overwrite)
{
$this->record['OVERWRITE'] = $overwrite ? 'true' : 'false';
return $this;
}

/**
* Specify if Potential should be created
* @param bool $createPotential
* @return ConvertLead
*/
public function setCreatePotential($createPotential)
{
$this->request->setParam('createPotential', $createPotential ? 'true' : 'false');
return $this;
}

/**
* Specify a user the converted Lead should be assign to
* @param string $assignTo could be SMOWNERID value or user email
* @return ConvertLead
*/
public function setAssignTo($assignTo)
{
$this->request->setParam('assignTo', $assignTo);
return $this;
}

/**
* Specify if a Lead owner should be notified
* @param bool $notifyLeadOwner
* @return ConvertLead
*/
public function setNotifyLeadOwner($notifyLeadOwner)
{
$this->request->setParam('notifyLeadOwner', $notifyLeadOwner ? 'true' : 'false');
return $this;
}

/**
* Specify if a new entity owner should be notified
* @param bool $notifyNewEntityOwner
* @return ConvertLead
*/
public function setNotifyNewEntityOwner($notifyNewEntityOwner)
{
$this->request->setParam('notifyNewEntityOwner', $notifyNewEntityOwner ? 'true' : 'false');
return $this;
}

/**
* Include the empty fields in the response.
*
* @return ConvertLead
*/
public function withEmptyFields()
{
$this->request->setParam('newFormat', "2");
return $this;
}

/**
* @return MutationResult[]
*/
public function request()
{
return $this->request
->setParam('xmlData', $this->record)
->request();
}
}
106 changes: 106 additions & 0 deletions Tests/Request/ConvertLeadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
namespace CristianPontes\ZohoCRMClient\Tests\Request;

use CristianPontes\ZohoCRMClient\Request;
use CristianPontes\ZohoCRMClient\Transport\MockTransport;
use CristianPontes\ZohoCRMClient\Transport\TransportRequest;
use CristianPontes\ZohoCRMClient\Transport\XmlDataTransportDecorator;

class ConvertLeadTest extends \PHPUnit_Framework_TestCase
{
/** @var MockTransport */
private $mockTransport;
/** @var XmlDataTransportDecorator */
private $transport;
/** @var TransportRequest */
private $request;
/** @var Request\ConvertLead */
private $convertLead;

public function testInitialStatus()
{
$this->assertEquals('convertLead', $this->request->getMethod());
$this->assertEquals(2, $this->request->getParam('version'));
$this->assertEquals('1', $this->request->getParam('newFormat'));
}

public function testConvertLead()
{
$this->mockTransport->response = file_get_contents(__DIR__ . '/../XML/convertLeadResponseSample.xml');

$this->convertLead->setCreatePotential(true);
$this->convertLead->setAssignTo('sample@zoho.com');
$this->convertLead->setNotifyLeadOwner('true');
$this->convertLead->setNotifyNewEntityOwner('true');

$this->convertLead->setAccountId('543212345');
$this->convertLead->setContactId('1234554321');
$this->convertLead->setOverwriteAccountName(true);

$record = array(
"Potential Name" => 'Samplepotential',
"Closing Date" => '12/21/2009',
"Potential Stage" => 'Closed Won',
"Contact Role" => 'Purchasing',
"Amount" => '3432.23',
"Probability" => '100',
);
$this->convertLead->addRecord($record);

$result = $this->convertLead->request();

$this->assertEquals(
file_get_contents(__DIR__ . '/../XML/convertLeadTest.xml'),
$this->mockTransport->paramList['xmlData']
);

$expectedResult = array(
'Account' => '161789000005836001',
'Contact' => '161789000005836003',
'Potential' => '161789000005848039',
);
$this->assertEquals($expectedResult, $result);
}

public function testLeadId()
{
$this->convertLead->setLeadId('123456789');
$this->assertEquals('123456789', $this->request->getParam('leadId'));
}

public function testCreatePotential()
{
$this->convertLead->setCreatePotential(true);
$this->assertEquals('true', $this->request->getParam('createPotential'));

$this->convertLead->setCreatePotential(false);
$this->assertEquals('false', $this->request->getParam('createPotential'));
}

public function testNotify()
{
$this->convertLead->setNotifyLeadOwner(true);
$this->assertEquals('true', $this->request->getParam('notifyLeadOwner'));

$this->convertLead->setNotifyLeadOwner(false);
$this->assertEquals('false', $this->request->getParam('notifyLeadOwner'));

$this->convertLead->setNotifyNewEntityOwner(true);
$this->assertEquals('true', $this->request->getParam('notifyNewEntityOwner'));

$this->convertLead->setNotifyNewEntityOwner(false);
$this->assertEquals('false', $this->request->getParam('notifyNewEntityOwner'));
}


protected function setUp()
{
$this->request = new TransportRequest('Leads');
$this->mockTransport = new MockTransport();
$this->transport = new XmlDataTransportDecorator($this->mockTransport);
$this->request->setTransport($this->transport);

$this->convertLead = new Request\ConvertLead($this->request);
}
}

6 changes: 6 additions & 0 deletions Tests/XML/convertLeadResponseSample.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<success>
<Contact param="id">161789000005836003</Contact>
<Potential param="id">161789000005848039</Potential>
<Account param="id">161789000005836001</Account>
</success>
2 changes: 2 additions & 0 deletions Tests/XML/convertLeadTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0"?>
<Potentials><row no="1"><option val="createPotential">true</option><option val="assignTo">sample@zoho.com</option><option val="notifyLeadOwner">true</option><option val="notifyNewEntityOwner">true</option></row><row no="2"><FL val="ACCOUNTID">543212345</FL><FL val="CONTACTID">1234554321</FL><FL val="OVERWRITE">true</FL><FL val="Potential Name">Samplepotential</FL><FL val="Closing Date">12/21/2009</FL><FL val="Potential Stage">Closed Won</FL><FL val="Contact Role">Purchasing</FL><FL val="Amount">3432.23</FL><FL val="Probability">100</FL></row></Potentials>
Loading

0 comments on commit 4c15e70

Please sign in to comment.