Skip to content

Commit

Permalink
implemented method updateRelatedRecords
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian Pontes authored and Cristian Pontes committed Aug 14, 2017
1 parent b88e39e commit 5f75257
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 17 deletions.
88 changes: 88 additions & 0 deletions Request/UpdateRelatedRecords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
namespace CristianPontes\ZohoCRMClient\Request;

use CristianPontes\ZohoCRMClient\Response\MutationResult;

/**
* UpdateRelatedRecords API Call
*
* You can use the updateRelatedRecords method to update records related to another record.
*
* @see https://www.zoho.eu/crm/help/api/updaterelatedrecords.html
*/
class UpdateRelatedRecords extends AbstractRequest
{
/** @var array */
private $records = array();

/**
*
*/
protected function configureRequest()
{
$this->request
->setMethod('updateRelatedRecords')
->setParam('version', 4);
}

/**
* @param array $record Record as a simple associative array
* @return UpdateRelatedRecords
*/
public function addRecord(array $record)
{
$this->records[] = $record;
return $this;
}

/**
* @return array
*/
public function getRecords()
{
return $this->records;
}

/**
* @param array $records array containing records otherwise added by addRecord()
* @return UpdateRelatedRecords
*/
public function setRecords(array $records)
{
$this->records = $records;
return $this;
}

/**
* The ID of the record to be updated.
* @param $id
* @return UpdateRelatedRecords
*/
public function id($id)
{
$this->request->setParam('id', $id);
return $this;
}

/**
* The module to which a record is related.
* i.e: If a lead related to a campaign needs to be updated, the value for this parameter will be Leads.
* @param $module
* @return UpdateRelatedRecords
*/
public function relatedModule($module)
{
$this->request->setParam('relatedModule', $module);
return $this;
}

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

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

class UpdateRelatedRecordsTest extends \PHPUnit_Framework_TestCase
{
/** @var MockTransport */
private $transport;

/** @var TransportRequest */
private $request;

/** @var Request\UpdateRelatedRecords */
private $updateRelatedRecords;

protected function setUp()
{
$this->transport = new MockTransport();
$this->request = new TransportRequest('Products');
$this->request->setTransport($this->transport);
$this->updateRelatedRecords = new Request\UpdateRelatedRecords($this->request);
}

public function testInitial()
{
$this->assertEquals('updateRelatedRecords', $this->request->getMethod());
$this->assertEquals(
4,
$this->request->getParam('version')
);
}

public function testRecords()
{
$record = array(
'PRODUCTID' => '508020000000061987',
'list_price' => '100.00'
);

$this->updateRelatedRecords->addRecord($record);

$this->transport->response = true;

$this->assertTrue($this->updateRelatedRecords->request());
$this->assertEquals(array('version' => 4, 'xmlData' => array($record)), $this->transport->paramList);
}

public function testRelatedModule()
{
$this->updateRelatedRecords->relatedModule('Leads');
$this->assertEquals(
'Leads',
$this->request->getParam('relatedModule')
);
}

public function testId()
{
$this->updateRelatedRecords->id('123');
$this->assertEquals(
123,
$this->request->getParam('id')
);
}
}
49 changes: 49 additions & 0 deletions Tests/ZohoCRMClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,34 @@ public function testUpdateRecords()
$this->assertInstanceOf('CristianPontes\ZohoCRMClient\Request\UpdateRecords', $request);
}

public function testUpdateRelatedRecords()
{
$request = $this->client->updateRelatedRecords();

$this->assertInstanceOf('CristianPontes\ZohoCRMClient\Request\UpdateRelatedRecords', $request);
}

public function testSearchRecords()
{
$request = $this->client->searchRecords();

$this->assertInstanceOf('CristianPontes\ZohoCRMClient\Request\SearchRecords', $request);
}

public function testGetSearchRecordsByPDC()
{
$request = $this->client->getSearchRecordsByPDC();

$this->assertInstanceOf('CristianPontes\ZohoCRMClient\Request\GetSearchRecordsByPDC', $request);
}

public function testConvertLead()
{
$request = $this->client->ConvertLead();

$this->assertInstanceOf('CristianPontes\ZohoCRMClient\Request\ConvertLead', $request);
}

public function testGetFields()
{
$request = $this->client->getFields();
Expand All @@ -68,13 +96,34 @@ public function testDeleteRecords()
$this->assertInstanceOf('CristianPontes\ZohoCRMClient\Request\DeleteRecords', $request);
}

public function testGetDeletedRecordIds()
{
$request = $this->client->getDeletedRecordIds();

$this->assertInstanceOf('CristianPontes\ZohoCRMClient\Request\GetDeletedRecordIds', $request);
}

public function testUploadFile()
{
$request = $this->client->uploadFile();

$this->assertInstanceOf('CristianPontes\ZohoCRMClient\Request\UploadFile', $request);
}

public function testDownloadFile()
{
$request = $this->client->downloadFile();

$this->assertInstanceOf('CristianPontes\ZohoCRMClient\Request\DownloadFile', $request);
}

public function testDeleteFile()
{
$request = $this->client->deleteFile();

$this->assertInstanceOf('CristianPontes\ZohoCRMClient\Request\DeleteFile', $request);
}

public function testRequest()
{
$request = $this->client->publicRequest();
Expand Down
2 changes: 1 addition & 1 deletion Transport/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface Transport
* @param string $module
* @param string $method
* @param array $paramList
* @return array
* @return string
*/
public function call($module, $method, array $paramList);
}
50 changes: 36 additions & 14 deletions Transport/XmlDataTransportDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class XmlDataTransportDecorator extends AbstractTransportDecorator
private $module;
/** @var string */
private $method;
/** @var string */
/** @var array */
private $call_params;


Expand Down Expand Up @@ -62,6 +62,7 @@ public function call($module, $method, array $paramList)
private function encodeRequestConvertLead(array $paramList)
{
$root = new SimpleXMLElement('<Potentials></Potentials>');
$options = array();

// row 1 (options)
foreach (ConvertLead::getOptionFields() as $optionName) {
Expand All @@ -87,14 +88,15 @@ private function encodeRequestConvertLead(array $paramList)

/**
* @param array $records
* @param string $rootName
* @param array $options
* @throws \CristianPontes\ZohoCRMClient\Exception\RuntimeException
* @return string XML representation of the records
*/
private function encodeRecords(array $records)
{
$root = new SimpleXMLElement('<'.$this->module.'></'.$this->module.'>');
$module = $this->method == 'updateRelatedRecords' ?
$this->call_params['relatedModule'] : $this->module;

$root = new SimpleXMLElement('<'.$module.'></'.$module.'>');

foreach ($records as $no => $record) {
$row = $root->addChild('row');
Expand All @@ -111,7 +113,6 @@ private function encodeRecords(array $records)
* @param array $record
* @param string $childName XML node name
* @param SimpleXMLElement $xml
* @return string XML representation of the record
*/
private function encodeRecord($record, $childName, &$xml)
{
Expand Down Expand Up @@ -140,7 +141,7 @@ private function encodeRecord($record, $childName, &$xml)

/**
* @param $array
* @param $xml
* @param SimpleXMLElement $xml
*/
private function parseNestedValues($array, &$xml)
{
Expand Down Expand Up @@ -171,7 +172,7 @@ private function parseNestedValues($array, &$xml)
* @throws Exception\UnexpectedValueException When invalid XML is given to parse
* @throws Exception\NoDataException when Zoho tells us there is no data
* @throws Exception\ZohoErrorException when content is a Error response
* @return Response\Record[]|Response\Field[]|Response\MutationResult[]
* @return bool|Response\Record|Response\Record[]|Response\Field[]|Response\MutationResult
*/
private function parse($content)
{
Expand Down Expand Up @@ -222,6 +223,10 @@ private function parse($content)
return $this->parseResponseConvertLead($xml);
}

if ($this->method == 'updateRelatedRecords') {
return $this->parseUpdateRelatedRecords($xml);
}

if (isset($xml->result->{$this->module})) {
return $this->parseResponseGetRecords($xml);
}
Expand Down Expand Up @@ -251,7 +256,7 @@ private function parseResponseGetRecords(SimpleXMLElement $xml)
* @param SimpleXMLElement $row
* @return Response\Record
*/
private function rowToRecord(SimpleXMLElement $row)
private function rowToRecord($row)
{
$data = array();
foreach($row as $field) {
Expand All @@ -271,7 +276,7 @@ private function rowToRecord(SimpleXMLElement $row)
}

/**
* @param $xml
* @param SimpleXMLElement $xml
* @return array
*/
private function parseResponseGetFields($xml)
Expand Down Expand Up @@ -304,7 +309,7 @@ private function parseResponseGetFields($xml)
}

/**
* @param $xml
* @param SimpleXMLElement $xml
* @return Response\MutationResult
*/
private function parseResponseDeleteRecords($xml)
Expand All @@ -313,7 +318,7 @@ private function parseResponseDeleteRecords($xml)
}

/**
* @param $xml
* @param SimpleXMLElement $xml
* @return Response\MutationResult
*/
private function parseResponseUploadFile($xml)
Expand All @@ -330,7 +335,7 @@ private function parseResponseUploadFile($xml)
}

/**
* @param $xml
* @param SimpleXMLElement $xml
* @return Response\MutationResult
*/
private function parseResponseDeleteFile($xml)
Expand All @@ -357,7 +362,7 @@ private function parseResponseDownloadFile($file_content)
}

/**
* @param $xml
* @param SimpleXMLElement $xml
* @return Response\Record
*/
private function parseResponseGetDeletedRecordIds($xml)
Expand All @@ -367,7 +372,7 @@ private function parseResponseGetDeletedRecordIds($xml)
}

/**
* @param $xml
* @param SimpleXMLElement $xml
* @return array
*/
private function parseResponseConvertLead($xml)
Expand All @@ -380,6 +385,23 @@ private function parseResponseConvertLead($xml)
return $records;
}

/**
* @param SimpleXMLElement $xml
* @return Response\Record
*/
private function parseUpdateRelatedRecords($xml)
{
$result = (array) $xml->result;
$added = isset($result['added-ids']) ? eval('return ' . $result['added-ids'] . ';') : [];
$updated = isset($result['updated-ids']) ? eval('return ' . $result['updated-ids'] . ';') : [];
$data = array(
'added-ids' => $added,
'updated-ids' => $updated
);
return new Response\Record($data, 1);
}


/**
* @param $xml
* @return array
Expand Down
Loading

0 comments on commit 5f75257

Please sign in to comment.