Skip to content

Commit

Permalink
Merge pull request #1725 from laboro/fix/CRM-2961
Browse files Browse the repository at this point in the history
CRM-2961: Improve Magento Region connector
  • Loading branch information
dxops committed Apr 29, 2015
2 parents ae03ac6 + f41b490 commit f3f969b
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public function getConfigTreeBuilder()
)
->example('14 days')
->end()
->scalarNode('region_sync_interval')
->defaultValue('1 day')
->cannotBeEmpty()
->info('This interval will be used to sync regions')
->example('14 days')
->end()
->end()
->end()
->arrayNode(self::DISCOVERY_NODE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace OroCRM\Bundle\MagentoBundle\Migrations\Data\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;

use Oro\Bundle\IntegrationBundle\Entity\Channel;
use OroCRM\Bundle\MagentoBundle\Provider\ChannelType;
use OroCRM\Bundle\MagentoBundle\Provider\Connector\DictionaryConnectorInterface;

class UpdateConnectors extends AbstractFixture
{
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
/** @var Channel[] $channels */
$channels = $manager->getRepository('OroIntegrationBundle:Channel')->findBy(['type' => ChannelType::TYPE]);

foreach ($channels as $channel) {
$connectors = $channel->getConnectors();
$key = array_search('region', $connectors, true);

if ($key === false) {
$connectors[] = 'region' . DictionaryConnectorInterface::DICTIONARY_CONNECTOR_SUFFIX;
} else {
$connectors[$key] = 'region' . DictionaryConnectorInterface::DICTIONARY_CONNECTOR_SUFFIX;
}

$channel->setConnectors($connectors);
}

$manager->flush($channels);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

abstract class AbstractInitialProcessor extends SyncProcessor
{
const DICTIONARY_CONNECTOR_SUFFIX = '_dictionary';
const INITIAL_SYNC_START_DATE = 'initialSyncStartDate';
const INITIAL_SYNCED_TO = 'initialSyncedTo';
const CONNECTORS_INITIAL_SYNCED_TO = 'connectorsInitialSyncedTo';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

interface DictionaryConnectorInterface
{
const DICTIONARY_CONNECTOR_SUFFIX = '_dictionary';
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Oro\Bundle\IntegrationBundle\Provider\ForceConnectorInterface;
use OroCRM\Bundle\MagentoBundle\Command\InitialSyncCommand;
use OroCRM\Bundle\MagentoBundle\Entity\MagentoSoapTransport;
use OroCRM\Bundle\MagentoBundle\Provider\Connector\DictionaryConnectorInterface;

/**
* Schedule initial synchronization if it is required.
Expand Down Expand Up @@ -109,7 +110,7 @@ protected function processConnectors(Integration $integration, array $parameters
if (null === $callback) {
$callback = function ($connector) {
return strpos($connector, InitialSyncProcessor::INITIAL_CONNECTOR_SUFFIX) === false
&& strpos($connector, self::DICTIONARY_CONNECTOR_SUFFIX) === false;
&& strpos($connector, DictionaryConnectorInterface::DICTIONARY_CONNECTOR_SUFFIX) === false;
};
}

Expand Down
29 changes: 27 additions & 2 deletions src/OroCRM/Bundle/MagentoBundle/Provider/RegionConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace OroCRM\Bundle\MagentoBundle\Provider;

class RegionConnector extends AbstractMagentoConnector
use OroCRM\Bundle\MagentoBundle\Provider\Connector\DictionaryConnectorInterface;

class RegionConnector extends AbstractMagentoConnector implements DictionaryConnectorInterface
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -33,14 +35,37 @@ public function getImportJobName()
*/
public function getType()
{
return 'region';
return 'region_dictionary';
}

/**
* {@inheritdoc}
*/
protected function getConnectorSource()
{
if (!empty($this->bundleConfiguration['sync_settings']['region_sync_interval'])) {
$interval = \DateInterval::createFromDateString(
$this->bundleConfiguration['sync_settings']['region_sync_interval']
);

$dateToCheck = new \DateTime('now', new \DateTimeZone('UTC'));
$dateToCheck->sub($interval);

$lastStatus = $this->getLastCompletedIntegrationStatus($this->channel, $this->getType());

if ($lastStatus && $lastStatus->getDate() > $dateToCheck) {
$this->logger->info(
sprintf(
'Regions are up to date, last sync date is %s, interval is %s',
$lastStatus->getDate()->format(\DateTime::RSS),
$this->bundleConfiguration['sync_settings']['region_sync_interval']
)
);

return new \EmptyIterator();
}
}

return $this->transport->getRegions();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ services:
class: %orocrm_magento.mage.region_connector.class%
parent: orocrm_magento.connector.abstract
tags:
- { name: oro_integration.connector, type: region, channel_type: magento }
- { name: oro_integration.connector, type: region_dictionary, channel_type: magento }
- { name: orocrm_magento.bundle_config.aware, argument_number: 3 }

orocrm_magento.mage.newsletter_subscriber_connector:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected function createIntegration()
$integration = new Integration();
$integration->setName('Demo Web store');
$integration->setType('magento');
$integration->setConnectors(['customer', 'order', 'cart', 'region']);
$integration->setConnectors(['customer', 'order', 'cart']);
$integration->setTransport($this->transport);
$integration->setOrganization($this->organization);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public function testConfigPassedToConnectors()
$config = [
'sync_settings' => [
'mistiming_assumption_interval' => '10 minutes',
'initial_import_step_interval' => '1 day'
'initial_import_step_interval' => '1 day',
'region_sync_interval' => '1 day',
]
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace OroCRM\Bundle\MagentoBundle\Tests\Unit\Provider\Connector;

use OroCRM\Bundle\MagentoBundle\Provider\Connector\AbstractInitialConnector;
use OroCRM\Bundle\MagentoBundle\Provider\AbstractMagentoConnector;
use OroCRM\Bundle\MagentoBundle\Provider\Connector\InitialConnectorInterface;
use OroCRM\Bundle\MagentoBundle\Tests\Unit\Provider\MagentoConnectorTestCase;

abstract class InitialConnectorTestCase extends MagentoConnectorTestCase
Expand Down Expand Up @@ -42,7 +43,7 @@ public function testGetImportEntityFQCNFailed()

public function testGetImportEntityFQCN()
{
/** @var AbstractInitialConnector $connector */
/** @var InitialConnectorInterface|AbstractMagentoConnector $connector */
$connector = $this->getConnector($this->transportMock, $this->stepExecutionMock);

$connector->setClassName('\stdClass');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Oro\Bundle\IntegrationBundle\Entity\Channel;
use Oro\Bundle\IntegrationBundle\Entity\Status;
use Oro\Bundle\IntegrationBundle\Entity\Repository\ChannelRepository;
use Oro\Bundle\IntegrationBundle\Entity\Transport;
use Oro\Bundle\IntegrationBundle\Logger\LoggerStrategy;
use Oro\Bundle\ImportExportBundle\Context\Context;
use Oro\Bundle\ImportExportBundle\Context\ContextRegistry;
Expand Down Expand Up @@ -104,6 +105,7 @@ public function testInitializationInUpdatedMode()
$status = new Status();
$status->setCode($status::STATUS_COMPLETED);
$status->setConnector($connector->getType());
$status->setDate(new \DateTime('-10 days', new \DateTimeZone('UTC')));

$this->expectLastCompletedStatusForConnector($status, $channel, $connector->getType());

Expand Down Expand Up @@ -169,7 +171,7 @@ public function testInitializationErrors()
$connector = $this->getConnector(null, $this->stepExecutionMock);
$this->transportMock->expects($this->never())->method('init');

$connector->setStepExecution($this->stepExecutionMock, $this->stepExecutionMock);
$connector->setStepExecution($this->stepExecutionMock);
}

/**
Expand Down Expand Up @@ -293,11 +295,15 @@ public function readItemDatesDataProvider()
*/
protected function getConnector($transport, $stepExecutionMock, $channel = null, $context = null)
{
/** @var \PHPUnit_Framework_MockObject_MockObject|ContextRegistry $contextRegistryMock */
$contextRegistryMock = $this->getMock('Oro\Bundle\ImportExportBundle\Context\ContextRegistry');

/** @var \PHPUnit_Framework_MockObject_MockObject|ConnectorContextMediator $contextMediatorMock */
$contextMediatorMock = $this
->getMockBuilder('Oro\\Bundle\\IntegrationBundle\\Provider\\ConnectorContextMediator')
->disableOriginalConstructor()->getMock();

/** @var \PHPUnit_Framework_MockObject_MockObject|Transport $transportSettings */
$transportSettings = $this->getMockForAbstractClass('Oro\\Bundle\\IntegrationBundle\\Entity\\Transport');
$channel = $channel ? : new Channel();
$channel->setTransport($transportSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
namespace OroCRM\Bundle\MagentoBundle\Tests\Unit\Provider;

use Oro\Bundle\ImportExportBundle\Context\ContextRegistry;
use Oro\Bundle\IntegrationBundle\Entity\Channel;
use Oro\Bundle\IntegrationBundle\Entity\Status;
use Oro\Bundle\IntegrationBundle\Logger\LoggerStrategy;
use Oro\Bundle\IntegrationBundle\Provider\ConnectorContextMediator;

use OroCRM\Bundle\MagentoBundle\Provider\RegionConnector;

class RegionConnectorTest extends MagentoConnectorTestCase
{
/** @var array */
protected $config = [
'sync_settings' => [
'mistiming_assumption_interval' => '2 minutes',
'region_sync_interval' => '1 day',
],
];

/**
* {@inheritdoc}
*/
Expand All @@ -31,15 +41,49 @@ protected function getIteratorGetterMethodName()

public function testPublicInterface()
{
/** @var \PHPUnit_Framework_MockObject_MockObject|ConnectorContextMediator $contextMediatorMock */
$contextMediatorMock = $this
->getMockBuilder('Oro\\Bundle\\IntegrationBundle\\Provider\\ConnectorContextMediator')
->getMockBuilder('Oro\Bundle\IntegrationBundle\Provider\ConnectorContextMediator')
->disableOriginalConstructor()->getMock();

$connector = $this->getConnectorInstance(new ContextRegistry(), new LoggerStrategy(), $contextMediatorMock);

$this->assertEquals('region', $connector->getType());
$this->assertEquals('region_dictionary', $connector->getType());
$this->assertEquals('mage_region_import', $connector->getImportJobName());
$this->assertEquals('OroCRM\\Bundle\\MagentoBundle\\Entity\\Region', $connector->getImportEntityFQCN());
$this->assertEquals('OroCRM\Bundle\MagentoBundle\Entity\Region', $connector->getImportEntityFQCN());
$this->assertEquals('orocrm.magento.connector.region.label', $connector->getLabel());
}

/**
* @param Status $expectedStatus
* @param Channel $channel
* @param string $connector
*/
protected function expectLastCompletedStatusForConnector($expectedStatus, $channel, $connector)
{
$this->integrationRepositoryMock->expects($this->any())
->method('getLastStatusForConnector')
->with($channel, $connector, Status::STATUS_COMPLETED)
->will($this->returnValue($expectedStatus));
}

public function testSkippedIfSyncedDuringConfiguredInterval()
{
$transport = $this->getMock('OroCRM\Bundle\MagentoBundle\Provider\Transport\MagentoTransportInterface');
$channel = new Channel();

$connector = $this->getConnector($this->transportMock, $this->stepExecutionMock, $channel);

$status = new Status();
$status->setCode($status::STATUS_COMPLETED);
$status->setConnector($connector->getType());
$status->setDate(new \DateTime('-10 minutes', new \DateTimeZone('UTC')));

$this->expectLastCompletedStatusForConnector($status, $channel, $connector->getType());

$connector->setStepExecution($this->stepExecutionMock);

$transport->expects($this->never())->method('getRegions');
$this->assertInstanceOf('\EmptyIterator', $connector->getSourceIterator());
}
}

0 comments on commit f3f969b

Please sign in to comment.