Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/dev/1.4' into 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrebenchuk committed Feb 17, 2015
2 parents a328616 + 8552d99 commit 04679c0
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 37 deletions.
78 changes: 53 additions & 25 deletions src/Oro/Bundle/CacheBundle/Tests/Unit/Provider/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,54 @@

namespace Oro\Bundle\CacheBundle\Tests\Unit\Provider;

use Symfony\Component\Filesystem\Filesystem;

use Oro\Bundle\CacheBundle\Provider\SyncCacheInterface;

class FileCacheTest extends \PHPUnit_Framework_TestCase
{
/**
* @param string $cacheClass
* @param string $id
* @param string $namespace
* @param string $expectedFileName
*
* @dataProvider getFilenameProvider
*/
public function testGetFilename($cacheClass, $id, $expectedFileName)
public function testGetFilename($cacheClass, $id, $namespace, $expectedFileName)
{
$fs = new Filesystem();
$directory = 'dir' . uniqid();

$cache = $this->getMockBuilder($cacheClass)
->disableOriginalConstructor()
->setMethods(['fetch'])
->setConstructorArgs([$directory, '.ext'])
->setMethods(['fetch', 'getNamespace'])
->getMock();

self::setProtectedProperty($cache, 'directory', 'dir');
self::setProtectedProperty($cache, 'extension', '.ext');
$cache->expects($this->any())
->method('getNamespace')
->will($this->returnValue($namespace));

$result = self::callProtectedMethod($cache, 'getFilename', [$id]);
$this->assertEquals(
$expectedFileName,
self::callProtectedMethod($cache, 'getFilename', array($id))
$directory . DIRECTORY_SEPARATOR . $expectedFileName,
str_replace(realpath($directory), $directory, $result)
);

$this->assertTrue($fs->exists($directory));
$fs->remove($directory);
}

/**
* @param string $cacheClass
*
* @dataProvider syncProvider
*/
public function testSync($cacheClass)
{
$namespace = '123';

/** @var \PHPUnit_Framework_MockObject_MockObject|SyncCacheInterface $cache */
$cache = $this->getMockBuilder($cacheClass)
->disableOriginalConstructor()
->setMethods(['setNamespace', 'getNamespace'])
Expand All @@ -45,32 +65,54 @@ public function testSync($cacheClass)
$cache->sync();
}

/**
* @return array
*/
public static function getFilenameProvider()
{
return [
[
'Oro\Bundle\CacheBundle\Provider\FilesystemCache',
'test',
'dir' . DIRECTORY_SEPARATOR . 'test.ext',
null,
'test.ext',
],
[
'Oro\Bundle\CacheBundle\Provider\FilesystemCache',
'test',
'namespace',
'namespace' . DIRECTORY_SEPARATOR . 'test.ext',
],
[
'Oro\Bundle\CacheBundle\Provider\FilesystemCache',
'test\\\\//::""**??<<>>||file',
'dir' . DIRECTORY_SEPARATOR . 'testfile.ext'
'namespace\\\\//::""**??<<>>||',
'namespace' . DIRECTORY_SEPARATOR . 'testfile.ext',
],
[
'Oro\Bundle\CacheBundle\Provider\PhpFileCache',
'test',
'dir' . DIRECTORY_SEPARATOR . 'test.ext',
null,
'test.ext',
],
[
'Oro\Bundle\CacheBundle\Provider\PhpFileCache',
'test',
'namespace',
'namespace' . DIRECTORY_SEPARATOR . 'test.ext',
],
[
'Oro\Bundle\CacheBundle\Provider\PhpFileCache',
'test\\\\//::""**??<<>>||file',
'dir' . DIRECTORY_SEPARATOR . 'testfile.ext'
'namespace\\\\//::""**??<<>>||',
'namespace' . DIRECTORY_SEPARATOR . 'testfile.ext',
],
];
}

/**
* @return array
*/
public static function syncProvider()
{
return [
Expand All @@ -79,20 +121,6 @@ public static function syncProvider()
];
}

/**
* @param mixed $obj
* @param string $propName
* @param mixed $val
*/
public static function setProtectedProperty($obj, $propName, $val)
{
$class = new \ReflectionClass($obj);
$prop = $class->getProperty($propName);
$prop->setAccessible(true);

$prop->setValue($obj, $val);
}

/**
* @param mixed $obj
* @param string $methodName
Expand Down
6 changes: 4 additions & 2 deletions src/Oro/Bundle/IntegrationBundle/Entity/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,10 @@ public function getStatuses()
}

/**
* @param string $connector
* @param int|int $codeFilter
* @deprecated Deprecated since 1.7.0 in favor of getLastStatusForConnector because of performance impact.
* @see Oro\Bundle\IntegrationBundle\Entity\Repository\ChannelRepository::getLastStatusForConnector
* @param string $connector
* @param int|null $codeFilter
*
* @return ArrayCollection
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,36 @@

class ChannelRepository extends EntityRepository
{
/**
* Returns latest status for integration's connector and code if it exists.
*
* @param Integration $integration
* @param string $connector
* @param int|null $code
* @return Status|null
*/
public function getLastStatusForConnector(Integration $integration, $connector, $code = null)
{
$queryBuilder = $this->getEntityManager()->createQueryBuilder()
->select('status')
->from('OroIntegrationBundle:Status', 'status')
->where('status.channel = :integration')
->andWhere('status.connector = :connector')
->setParameters(['integration' => $integration, 'connector' => (string)$connector])
->orderBy('status.date', 'DESC')
->setFirstResult(0)
->setMaxResults(1);

if ($code) {
$queryBuilder->andWhere('status.code = :code')
->setParameter('code', (string)$code);
};

$statuses = $queryBuilder->getQuery()->execute();

return $statuses ? reset($statuses) : null;
}

/**
* Returns channels that have configured transports
* Assume that they are ready for sync
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ and then methods `addStatusData` and `getStatusData` will be available.


// retrieve data from status
$status = $this->channel->getStatusesForConnector($this->getType(), Status::STATUS_COMPLETED)->first();
$status = $this->container->get('doctrine')->getRepository('OroIntegrationBundle:Channel')
->getLastStatusForConnector($channel, $this->getType(), Status::STATUS_COMPLETED);
/** @var array **/
$data = $status->getData();
$lastItemUpdatedAt = $data['lastItemUpdatedAt'];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Oro\Bundle\IntegrationBundle\Tests\Functional\DataFixtures;

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

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

use Oro\Bundle\IntegrationBundle\Entity\Channel as Integration;
use Oro\Bundle\UserBundle\Migrations\Data\ORM\LoadAdminUserData;

class LoadChannelData extends AbstractFixture implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
protected $container;

/**
* @var array
*/
protected $data = [
[
'name' => 'Foo Integration',
'type' => 'foo',
'enabled' => true,
'reference' => 'oro_integration:foo_integration'
],
[
'name' => 'Bar Integration',
'type' => 'bar',
'enabled' => true,
'reference' => 'oro_integration:bar_integration'
]
];

/**
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function load(ObjectManager $manager)
{
$userManager = $this->container->get('oro_user.manager');
$admin = $userManager->findUserByEmail(LoadAdminUserData::DEFAULT_ADMIN_EMAIL);

foreach ($this->data as $data) {
$integration = new Integration();

$integration->setName($data['name']);
$integration->setType($data['type']);
$integration->setEnabled($data['enabled']);
$integration->setDefaultUserOwner($admin);
$integration->setOrganization($admin->getOrganization());

$this->setReference($data['reference'], $integration);

$manager->persist($integration);
}

$manager->flush();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Oro\Bundle\IntegrationBundle\Tests\Functional\DataFixtures;

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

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

use Oro\Bundle\IntegrationBundle\Entity\Status;

class LoadStatusData extends AbstractFixture implements ContainerAwareInterface, DependentFixtureInterface
{
/**
* @var ContainerInterface
*/
protected $container;

/**
* @var array
*/
protected $data = [
[
'channel' => 'oro_integration:foo_integration',
'status' => Status::STATUS_COMPLETED,
'connector' => 'first_connector',
'date' => '2015-02-01 00:00:00',
'message' => '',
'reference' => 'oro_integration:foo_first_connector_first_status_completed'
],
[
'channel' => 'oro_integration:foo_integration',
'status' => Status::STATUS_COMPLETED,
'connector' => 'first_connector',
'date' => '2015-02-01 00:05:00',
'message' => '',
'reference' => 'oro_integration:foo_first_connector_second_status_completed'
],
[
'channel' => 'oro_integration:foo_integration',
'status' => Status::STATUS_FAILED,
'connector' => 'first_connector',
'date' => '2015-02-01 00:10:00',
'message' => '',
'reference' => 'oro_integration:foo_first_connector_third_status_failed'
],
[
'channel' => 'oro_integration:foo_integration',
'status' => Status::STATUS_COMPLETED,
'connector' => 'second_connector',
'date' => '2015-02-01 00:15:00',
'message' => '',
'reference' => 'oro_integration:foo_second_connector_first_status_completed'
],
[
'channel' => 'oro_integration:bar_integration',
'status' => Status::STATUS_COMPLETED,
'connector' => 'first_connector',
'date' => '2015-02-01 00:20:00',
'message' => '',
'reference' => 'oro_integration:bar_first_connector_first_status_completed'
],
];

/**
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function load(ObjectManager $manager)
{
foreach ($this->data as $data) {
$status = new Status();
$status->setChannel($this->getReference($data['channel']));
$status->setCode($data['status']);
$status->setConnector($data['connector']);
$status->setDate(new \DateTime($data['date'], new \DateTimeZone('UTC')));
$status->setMessage($data['message']);

$this->setReference($data['reference'], $status);

$manager->persist($status);
}

$manager->flush();
}

/**
* {@inheritdoc}
*/
public function getDependencies()
{
return [
'Oro\Bundle\IntegrationBundle\Tests\Functional\DataFixtures\LoadChannelData'
];
}
}
Loading

0 comments on commit 04679c0

Please sign in to comment.