Skip to content

Commit

Permalink
Fixing fetching data from last modified date
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Apr 16, 2015
1 parent b698022 commit 7574d70
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 50 deletions.
30 changes: 12 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ class MyContactMapper implements MappingInterface {
// This method will receive the Zoho bean from Zoho.
// It should
// 1- get the ZohoId (using $zohoBean->getZohoId() )
// 2- check in your database
// 2- check in your database if a bean with this ZohoId exists or not
// 3-a- If the bean exists in database, merge the Zoho bean into the application bean
// 3-b- If the bean does not exist in database, create the application bean from the Zoho bean
}

/**
Expand All @@ -122,29 +124,21 @@ class MyContactMapper implements MappingInterface {
*/
public function onSyncToZohoComplete($applicationBean, $zohoId, \DateTime $date)
{
if (!$applicationBean instanceof ContactApplicationBean) {
throw new ZohoCRMException("Expected ContactApplicationBean");
}
$applicationBean->setZohoId($zohoId);
$applicationBean->setZohoLastModificationDate($date);
// This function is called after an application bean has been stored into Zoho.
// Zoho returns a ZohoID, and a modification time. You must store both information in the application bean.
}


/**
* Filters the list of Zoho beans to store in database.
*
* This function should return the last Zoho modification date of a record saved in YOUR database.
* The date must be returned as a \DateTime object.
* Note: when a Zoho bean is inserted, the last modification date is passed to the `onSyncToZohoComplete`.
* You should store that date in the database.
*
* @param array $zohoBeans
* @return array The filtered list of Zoho beans.
* @return \DateTime
*/
public function filterZohoBeans(array $zohoBeans)
{
return $zohoBeans;
}

public function getLastZohoModificationDate() {
$now = new \DateTime();
return $now->sub(new \DateInterval("P1D"));
// You should perform a query in your database and return the "max" modification date
// stored into your application bean table.
}

}
Expand Down
9 changes: 0 additions & 9 deletions src/MappingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ public function onSyncToZohoComplete($applicationBean, $zohoId, \DateTime $modif
*/
public function getBeansToSynchronize();

/**
* Filters the list of Zoho beans to store in database.
*
*
* @param ZohoBeanInterface[] $zohoBeans
* @return ZohoBeanInterface[] The filtered list of Zoho beans.
*/
public function filterZohoBeans(array $zohoBeans);

/**
* This function should return the last Zoho modification date of a record saved in YOUR database.
* The date must be returned as a \DateTime object.
Expand Down
5 changes: 2 additions & 3 deletions src/ZohoSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function sync()
*/
public function sendAppBeansToZoho()
{
// TODO: fix the date!
$appBeans = $this->mapper->getBeansToSynchronize();

$zohoBeans = array_map(array($this->mapper, "toZohoBean"), $appBeans);
Expand All @@ -65,13 +64,13 @@ public function sendAppBeansToZoho()
}

/**
* Gets modified bean from Zoho into the application.
* Gets modified beans from Zoho into the application.
*/
public function getZohoBeansInApp() {

$lastZohoModificationDate = $this->mapper->getLastZohoModificationDate();

$zohoBeans = $this->dao->searchRecords(null, 1, null, $lastZohoModificationDate);
$zohoBeans = $this->dao->getRecords(null, null, $lastZohoModificationDate);

$appBeans = array_map(array($this->mapper, "toApplicationBean"), $zohoBeans);

Expand Down
14 changes: 5 additions & 9 deletions tests/ContactMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,13 @@ public function getBeansToSynchronize()
}

/**
* Filters the list of Zoho beans to store in database.
* This function should return the last Zoho modification date of a record saved in YOUR database.
* The date must be returned as a \DateTime object.
* Note: when a Zoho bean is inserted, the last modification date is passed to the `onSyncToZohoComplete`.
* You should store that date in the database.
*
*
* @param array $zohoBeans
* @return array The filtered list of Zoho beans.
* @return \DateTime
*/
public function filterZohoBeans(array $zohoBeans)
{
return $zohoBeans;
}

public function getLastZohoModificationDate() {
$now = new \DateTime();
return $now->sub(new \DateInterval("P1D"));
Expand Down
37 changes: 26 additions & 11 deletions tests/ZohoSynchronizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'ContactApplicationBean.php';
require 'ContactMapper.php';

use Psr\Log\NullLogger;
use TestNamespace\ContactZohoDao;
use Wabel\Zoho\CRM\Service\EntitiesGeneratorService;
use Wabel\Zoho\CRM\ZohoClient;
Expand All @@ -18,9 +19,11 @@ public function getZohoClient()

public function getEntitiesGeneratorService()
{
return new EntitiesGeneratorService($this->getZohoClient());
return new EntitiesGeneratorService($this->getZohoClient(), new NullLogger());
}

protected $firstName;

public function testSync()
{
$generator = $this->getEntitiesGeneratorService();
Expand All @@ -30,27 +33,29 @@ public function testSync()
require __DIR__.'/generated/ContactZohoDao.php';

$contactZohoDao = new ContactZohoDao($this->getZohoClient());
$firstName = uniqid("Test");
$this->firstName = $firstName;

$contacts = [
new ContactApplicationBean(1, "Test1", "Test", "test@yopmail.com", "0123456789"),
new ContactApplicationBean(2, "Test2", "Test", "test2@yopmail.com", "0123456789"),
new ContactApplicationBean(3, "Test3", "Test", "test3@yopmail.com", "0123456789"),
new ContactApplicationBean(4, "Test4", "Test", "test4@yopmail.com", "0123456789"),
new ContactApplicationBean(5, "Test5", "Test", "test5@yopmail.com", "0123456789"),
new ContactApplicationBean(1, "Test1", $firstName, "test@yopmail.com", "0123456789"),
new ContactApplicationBean(2, "Test2", $firstName, "test2@yopmail.com", "0123456789"),
new ContactApplicationBean(3, "Test3", $firstName, "test3@yopmail.com", "0123456789"),
new ContactApplicationBean(4, "Test4", $firstName, "test4@yopmail.com", "0123456789"),
new ContactApplicationBean(5, "Test5", $firstName, "test5@yopmail.com", "0123456789"),
];

$mapper = new ContactMapper();
$mapper->setTestContacts($contacts);

// Let's start by removing past inserted clients:
$pastContacts = $contactZohoDao->searchRecords('(First Name:Test)');
$pastContacts = $contactZohoDao->searchRecords('(First Name:'.$firstName.')');
foreach ($pastContacts as $pastContact) {
$contactZohoDao->delete($pastContact->getZohoId());
}

// Before calling sync, let's input some test data to sync!
$contactBean = new \TestNamespace\Contact();
$contactBean->setFirstName("Test");
$contactBean->setFirstName($firstName);
$contactBean->setLastName("InZohoFirst");
$contactZohoDao->save($contactBean);

Expand All @@ -63,7 +68,7 @@ public function testSync()

$found = false;
foreach ($appBeans as $appBean) {
$this->assertInstanceOf("Wabel\\Zoho\\CRM\\Sync\\ContactApplicationBean");
$this->assertInstanceOf("Wabel\\Zoho\\CRM\\Sync\\ContactApplicationBean", $appBean);
if ($appBean->getLastName() == 'InZohoFirst') {
$found = true;
}
Expand All @@ -78,11 +83,21 @@ public function testSync()

sleep(120);

$newContacts = $contactZohoDao->searchRecords('(First Name:Test)');
$newContacts = $contactZohoDao->searchRecords('(First Name:'.$firstName.')');
$this->assertCount(6, $newContacts);
// The ZohoID should be set in all fields:
foreach ($contacts as $contact) {
foreach ($newContacts as $contact) {
$this->assertNotEmpty($contact->getZohoId());
}
}

protected function tearDown()
{
$contactZohoDao = new ContactZohoDao($this->getZohoClient());
// Let's end by removing past inserted clients:
$pastContacts = $contactZohoDao->searchRecords('(First Name:'.$this->firstName.')');
foreach ($pastContacts as $pastContact) {
$contactZohoDao->delete($pastContact->getZohoId());
}
}
}

0 comments on commit 7574d70

Please sign in to comment.