mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-24 02:13:15 +00:00
Extract Proper NotifierInterface in TaskManager
This commit is contained in:
@@ -34,7 +34,7 @@ class ManipulatorServiceProvider implements ServiceProviderInterface
|
|||||||
public function register(SilexApplication $app)
|
public function register(SilexApplication $app)
|
||||||
{
|
{
|
||||||
$app['manipulator.task'] = $app->share(function (SilexApplication $app) {
|
$app['manipulator.task'] = $app->share(function (SilexApplication $app) {
|
||||||
return new TaskManipulator($app['orm.em'], $app['task-manager.notifier'], $app['translator']);
|
return new TaskManipulator($app['orm.em'], $app['translator'], $app['task-manager.notifier']);
|
||||||
});
|
});
|
||||||
|
|
||||||
$app['manipulator.user'] = $app->share(function ($app) {
|
$app['manipulator.user'] = $app->share(function ($app) {
|
||||||
|
|||||||
@@ -14,25 +14,31 @@ namespace Alchemy\Phrasea\Model\Manipulator;
|
|||||||
use Alchemy\Phrasea\Exception\RuntimeException;
|
use Alchemy\Phrasea\Exception\RuntimeException;
|
||||||
use Alchemy\Phrasea\Model\Entities\Task;
|
use Alchemy\Phrasea\Model\Entities\Task;
|
||||||
use Alchemy\Phrasea\TaskManager\Job\EmptyCollectionJob;
|
use Alchemy\Phrasea\TaskManager\Job\EmptyCollectionJob;
|
||||||
use Alchemy\Phrasea\TaskManager\Notifier;
|
use Alchemy\Phrasea\TaskManager\NotifierInterface;
|
||||||
|
use Alchemy\Phrasea\TaskManager\NullNotifier;
|
||||||
use Doctrine\Common\Persistence\ObjectManager;
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
use Doctrine\ORM\EntityRepository;
|
|
||||||
use Symfony\Component\Translation\TranslatorInterface;
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
|
|
||||||
class TaskManipulator implements ManipulatorInterface
|
class TaskManipulator implements ManipulatorInterface
|
||||||
{
|
{
|
||||||
/** @var Notifier */
|
/** @var NotifierInterface */
|
||||||
private $notifier;
|
private $notifier;
|
||||||
/** @var ObjectManager */
|
/** @var ObjectManager */
|
||||||
private $om;
|
private $om;
|
||||||
/** @var TranslatorInterface */
|
/** @var TranslatorInterface */
|
||||||
private $translator;
|
private $translator;
|
||||||
|
|
||||||
public function __construct(ObjectManager $om, Notifier $notifier, TranslatorInterface $translator)
|
public function __construct(ObjectManager $om, TranslatorInterface $translator, NotifierInterface $notifier=null)
|
||||||
{
|
{
|
||||||
$this->om = $om;
|
$this->om = $om;
|
||||||
$this->notifier = $notifier;
|
|
||||||
$this->translator = $translator;
|
$this->translator = $translator;
|
||||||
|
$this->setNotifier($notifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setNotifier(NotifierInterface $notifier=null)
|
||||||
|
{
|
||||||
|
$this->notifier = $notifier ?: new NullNotifier();
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -56,7 +62,7 @@ class TaskManipulator implements ManipulatorInterface
|
|||||||
$this->om->persist($task);
|
$this->om->persist($task);
|
||||||
$this->om->flush();
|
$this->om->flush();
|
||||||
|
|
||||||
$this->notify(Notifier::MESSAGE_CREATE);
|
$this->notify(NotifierInterface::MESSAGE_CREATE);
|
||||||
|
|
||||||
return $task;
|
return $task;
|
||||||
}
|
}
|
||||||
@@ -83,7 +89,7 @@ class TaskManipulator implements ManipulatorInterface
|
|||||||
$this->om->persist($task);
|
$this->om->persist($task);
|
||||||
$this->om->flush();
|
$this->om->flush();
|
||||||
|
|
||||||
$this->notify(Notifier::MESSAGE_CREATE);
|
$this->notify(NotifierInterface::MESSAGE_CREATE);
|
||||||
|
|
||||||
return $task;
|
return $task;
|
||||||
}
|
}
|
||||||
@@ -100,7 +106,7 @@ class TaskManipulator implements ManipulatorInterface
|
|||||||
$this->om->persist($task);
|
$this->om->persist($task);
|
||||||
$this->om->flush();
|
$this->om->flush();
|
||||||
|
|
||||||
$this->notify(Notifier::MESSAGE_UPDATE);
|
$this->notify(NotifierInterface::MESSAGE_UPDATE);
|
||||||
|
|
||||||
return $task;
|
return $task;
|
||||||
}
|
}
|
||||||
@@ -115,7 +121,7 @@ class TaskManipulator implements ManipulatorInterface
|
|||||||
$this->om->remove($task);
|
$this->om->remove($task);
|
||||||
$this->om->flush();
|
$this->om->flush();
|
||||||
|
|
||||||
$this->notify(Notifier::MESSAGE_DELETE);
|
$this->notify(NotifierInterface::MESSAGE_DELETE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -132,7 +138,7 @@ class TaskManipulator implements ManipulatorInterface
|
|||||||
$this->om->persist($task);
|
$this->om->persist($task);
|
||||||
$this->om->flush();
|
$this->om->flush();
|
||||||
|
|
||||||
$this->notify(Notifier::MESSAGE_UPDATE);
|
$this->notify(NotifierInterface::MESSAGE_UPDATE);
|
||||||
|
|
||||||
return $task;
|
return $task;
|
||||||
}
|
}
|
||||||
@@ -151,7 +157,7 @@ class TaskManipulator implements ManipulatorInterface
|
|||||||
$this->om->persist($task);
|
$this->om->persist($task);
|
||||||
$this->om->flush();
|
$this->om->flush();
|
||||||
|
|
||||||
$this->notify(Notifier::MESSAGE_UPDATE);
|
$this->notify(NotifierInterface::MESSAGE_UPDATE);
|
||||||
|
|
||||||
return $task;
|
return $task;
|
||||||
}
|
}
|
||||||
@@ -170,7 +176,7 @@ class TaskManipulator implements ManipulatorInterface
|
|||||||
$this->om->persist($task);
|
$this->om->persist($task);
|
||||||
$this->om->flush();
|
$this->om->flush();
|
||||||
|
|
||||||
$this->notify(Notifier::MESSAGE_UPDATE);
|
$this->notify(NotifierInterface::MESSAGE_UPDATE);
|
||||||
|
|
||||||
return $task;
|
return $task;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class LiveInformation
|
|||||||
private $status;
|
private $status;
|
||||||
private $notifier;
|
private $notifier;
|
||||||
|
|
||||||
public function __construct(TaskManagerStatus $status, Notifier $notifier)
|
public function __construct(TaskManagerStatus $status, NotifierInterface $notifier)
|
||||||
{
|
{
|
||||||
$this->status = $status;
|
$this->status = $status;
|
||||||
$this->notifier = $notifier;
|
$this->notifier = $notifier;
|
||||||
@@ -87,7 +87,7 @@ class LiveInformation
|
|||||||
private function query($throwException)
|
private function query($throwException)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->notifier->notify(Notifier::MESSAGE_INFORMATIONS);
|
return $this->notifier->notify(NotifierInterface::MESSAGE_INFORMATION);
|
||||||
} catch (RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
if ($throwException) {
|
if ($throwException) {
|
||||||
throw $e;
|
throw $e;
|
||||||
|
|||||||
@@ -16,17 +16,8 @@ use Alchemy\Phrasea\Exception\RuntimeException;
|
|||||||
use Alchemy\Phrasea\Exception\InvalidArgumentException;
|
use Alchemy\Phrasea\Exception\InvalidArgumentException;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
class Notifier
|
class Notifier implements NotifierInterface
|
||||||
{
|
{
|
||||||
/** Alerts the task manager a new Task has been created */
|
|
||||||
const MESSAGE_CREATE = 'create';
|
|
||||||
/** Alerts the task manager a Task has been updated */
|
|
||||||
const MESSAGE_UPDATE = 'update';
|
|
||||||
/** Alerts the task manager a Task has been deleted */
|
|
||||||
const MESSAGE_DELETE = 'delete';
|
|
||||||
/** Alerts the task manager to send its informations data */
|
|
||||||
const MESSAGE_INFORMATIONS = 'informations';
|
|
||||||
|
|
||||||
/** @var \ZMQSocket */
|
/** @var \ZMQSocket */
|
||||||
private $socket;
|
private $socket;
|
||||||
|
|
||||||
@@ -98,7 +89,7 @@ class Notifier
|
|||||||
case static::MESSAGE_UPDATE:
|
case static::MESSAGE_UPDATE:
|
||||||
case static::MESSAGE_DELETE:
|
case static::MESSAGE_DELETE:
|
||||||
return TaskManager::MESSAGE_PROCESS_UPDATE;
|
return TaskManager::MESSAGE_PROCESS_UPDATE;
|
||||||
case static::MESSAGE_INFORMATIONS:
|
case static::MESSAGE_INFORMATION:
|
||||||
return TaskManager::MESSAGE_STATE;
|
return TaskManager::MESSAGE_STATE;
|
||||||
default:
|
default:
|
||||||
throw new InvalidArgumentException(sprintf('Unable to understand %s message notification', $message));
|
throw new InvalidArgumentException(sprintf('Unable to understand %s message notification', $message));
|
||||||
|
|||||||
33
lib/Alchemy/Phrasea/TaskManager/NotifierInterface.php
Normal file
33
lib/Alchemy/Phrasea/TaskManager/NotifierInterface.php
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Phraseanet
|
||||||
|
*
|
||||||
|
* (c) 2005-2015 Alchemy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Alchemy\Phrasea\TaskManager;
|
||||||
|
|
||||||
|
|
||||||
|
interface NotifierInterface
|
||||||
|
{
|
||||||
|
/** Alerts the task manager a new Task has been created */
|
||||||
|
const MESSAGE_CREATE = 'create';
|
||||||
|
/** Alerts the task manager a Task has been updated */
|
||||||
|
const MESSAGE_UPDATE = 'update';
|
||||||
|
/** Alerts the task manager a Task has been deleted */
|
||||||
|
const MESSAGE_DELETE = 'delete';
|
||||||
|
/** Alerts the task manager to send its information data */
|
||||||
|
const MESSAGE_INFORMATION = 'information';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notifies the task manager given a message constant, see MESSAGE_* constants.
|
||||||
|
*
|
||||||
|
* @param string $message
|
||||||
|
*
|
||||||
|
* @return mixed|null The return value of the task manager.
|
||||||
|
*/
|
||||||
|
public function notify($message);
|
||||||
|
}
|
||||||
20
lib/Alchemy/Phrasea/TaskManager/NullNotifier.php
Normal file
20
lib/Alchemy/Phrasea/TaskManager/NullNotifier.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Phraseanet
|
||||||
|
*
|
||||||
|
* (c) 2005-2015 Alchemy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Alchemy\Phrasea\TaskManager;
|
||||||
|
|
||||||
|
|
||||||
|
class NullNotifier implements NotifierInterface
|
||||||
|
{
|
||||||
|
public function notify($message)
|
||||||
|
{
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,13 +3,13 @@
|
|||||||
namespace Alchemy\Tests\Phrasea\Model\Manipulator;
|
namespace Alchemy\Tests\Phrasea\Model\Manipulator;
|
||||||
|
|
||||||
use Alchemy\Phrasea\Model\Manipulator\TaskManipulator;
|
use Alchemy\Phrasea\Model\Manipulator\TaskManipulator;
|
||||||
use Alchemy\Phrasea\TaskManager\Notifier;
|
use Alchemy\Phrasea\TaskManager\NotifierInterface;
|
||||||
use Alchemy\Phrasea\Model\Entities\Task;
|
use Alchemy\Phrasea\Model\Entities\Task;
|
||||||
use Doctrine\Common\Persistence\ObjectManager;
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
|
|
||||||
class TaskManipulatorTest extends \PhraseanetTestCase
|
class TaskManipulatorTest extends \PhraseanetTestCase
|
||||||
{
|
{
|
||||||
/** @var Notifier|\PHPUnit_Framework_MockObject_MockObject */
|
/** @var NotifierInterface|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
private $notifier;
|
private $notifier;
|
||||||
|
|
||||||
/** @var TaskManipulator */
|
/** @var TaskManipulator */
|
||||||
@@ -20,7 +20,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->notifier = $this->createNotifierMock();
|
$this->notifier = $this->createNotifierMock();
|
||||||
|
|
||||||
$this->sut = new TaskManipulator(self::$DI['app']['orm.em'], $this->notifier, self::$DI['app']['translator']);
|
$this->sut = new TaskManipulator(self::$DI['app']['orm.em'], self::$DI['app']['translator'], $this->notifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreate()
|
public function testCreate()
|
||||||
@@ -28,7 +28,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
|
|||||||
$this->notifier
|
$this->notifier
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('notify')
|
->method('notify')
|
||||||
->with(Notifier::MESSAGE_CREATE);
|
->with(NotifierInterface::MESSAGE_CREATE);
|
||||||
|
|
||||||
$this->assertCount(2, $this->findAllTasks());
|
$this->assertCount(2, $this->findAllTasks());
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
|
|||||||
$this->notifier
|
$this->notifier
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('notify')
|
->method('notify')
|
||||||
->with(Notifier::MESSAGE_UPDATE);
|
->with(NotifierInterface::MESSAGE_UPDATE);
|
||||||
|
|
||||||
$task = $this->loadTask();
|
$task = $this->loadTask();
|
||||||
$task->setName('new name');
|
$task->setName('new name');
|
||||||
@@ -64,7 +64,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
|
|||||||
$this->notifier
|
$this->notifier
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('notify')
|
->method('notify')
|
||||||
->with(Notifier::MESSAGE_DELETE);
|
->with(NotifierInterface::MESSAGE_DELETE);
|
||||||
|
|
||||||
$task = $this->loadTask();
|
$task = $this->loadTask();
|
||||||
$this->sut->delete($task);
|
$this->sut->delete($task);
|
||||||
@@ -76,7 +76,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
|
|||||||
$this->notifier
|
$this->notifier
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('notify')
|
->method('notify')
|
||||||
->with(Notifier::MESSAGE_UPDATE);
|
->with(NotifierInterface::MESSAGE_UPDATE);
|
||||||
|
|
||||||
$task = $this->loadTask();
|
$task = $this->loadTask();
|
||||||
$task->setStatus(Task::STATUS_STOPPED);
|
$task->setStatus(Task::STATUS_STOPPED);
|
||||||
@@ -92,7 +92,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
|
|||||||
$this->notifier
|
$this->notifier
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('notify')
|
->method('notify')
|
||||||
->with(Notifier::MESSAGE_UPDATE);
|
->with(NotifierInterface::MESSAGE_UPDATE);
|
||||||
|
|
||||||
$task = $this->loadTask();
|
$task = $this->loadTask();
|
||||||
$task->setStatus(Task::STATUS_STARTED);
|
$task->setStatus(Task::STATUS_STARTED);
|
||||||
@@ -109,7 +109,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase
|
|||||||
$this->notifier
|
$this->notifier
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('notify')
|
->method('notify')
|
||||||
->with(Notifier::MESSAGE_UPDATE);
|
->with(NotifierInterface::MESSAGE_UPDATE);
|
||||||
|
|
||||||
$task = $this->loadTask();
|
$task = $this->loadTask();
|
||||||
$task->setCrashed(42);
|
$task->setCrashed(42);
|
||||||
@@ -137,6 +137,14 @@ class TaskManipulatorTest extends \PhraseanetTestCase
|
|||||||
$this->assertEquals(42, (int) $settings->bas_id);
|
$this->assertEquals(42, (int) $settings->bas_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testItsNotifierCanBeChanged()
|
||||||
|
{
|
||||||
|
$notifier = $this->createNotifierMock();
|
||||||
|
|
||||||
|
$this->assertSame($this->sut, $this->sut->setNotifier($notifier));
|
||||||
|
$this->assertAttributeSame($notifier, 'notifier', $this->sut);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Task[]
|
* @return Task[]
|
||||||
*/
|
*/
|
||||||
@@ -162,8 +170,6 @@ class TaskManipulatorTest extends \PhraseanetTestCase
|
|||||||
*/
|
*/
|
||||||
private function createNotifierMock()
|
private function createNotifierMock()
|
||||||
{
|
{
|
||||||
return $this->getMockBuilder(Notifier::class)
|
return $this->getMockBuilder(NotifierInterface::class)->getMock();
|
||||||
->disableOriginalConstructor()
|
|
||||||
->getMock();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
namespace Alchemy\Tests\Phrasea\TaskManager;
|
namespace Alchemy\Tests\Phrasea\TaskManager;
|
||||||
|
|
||||||
use Alchemy\Phrasea\TaskManager\LiveInformation;
|
use Alchemy\Phrasea\TaskManager\LiveInformation;
|
||||||
use Alchemy\Phrasea\TaskManager\Notifier;
|
use Alchemy\Phrasea\TaskManager\NotifierInterface;
|
||||||
use Alchemy\Phrasea\TaskManager\TaskManagerStatus;
|
use Alchemy\Phrasea\TaskManager\TaskManagerStatus;
|
||||||
use Alchemy\Phrasea\Model\Entities\Task;
|
use Alchemy\Phrasea\Model\Entities\Task;
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ class LiveInformationTest extends \PhraseanetTestCase
|
|||||||
$notifier = $this->createNotifierMock();
|
$notifier = $this->createNotifierMock();
|
||||||
$notifier->expects($this->once())
|
$notifier->expects($this->once())
|
||||||
->method('notify')
|
->method('notify')
|
||||||
->with(Notifier::MESSAGE_INFORMATIONS)
|
->with(NotifierInterface::MESSAGE_INFORMATION)
|
||||||
->will($this->returnValue([
|
->will($this->returnValue([
|
||||||
'manager' => [
|
'manager' => [
|
||||||
'process-id' => 1234,
|
'process-id' => 1234,
|
||||||
@@ -35,7 +35,7 @@ class LiveInformationTest extends \PhraseanetTestCase
|
|||||||
$notifier = $this->createNotifierMock();
|
$notifier = $this->createNotifierMock();
|
||||||
$notifier->expects($this->once())
|
$notifier->expects($this->once())
|
||||||
->method('notify')
|
->method('notify')
|
||||||
->with(Notifier::MESSAGE_INFORMATIONS)
|
->with(NotifierInterface::MESSAGE_INFORMATION)
|
||||||
->will($this->returnValue(null));
|
->will($this->returnValue(null));
|
||||||
|
|
||||||
$live = new LiveInformation($this->createStatusMock(TaskManagerStatus::STATUS_STARTED), $notifier);
|
$live = new LiveInformation($this->createStatusMock(TaskManagerStatus::STATUS_STARTED), $notifier);
|
||||||
@@ -54,7 +54,7 @@ class LiveInformationTest extends \PhraseanetTestCase
|
|||||||
$notifier = $this->createNotifierMock();
|
$notifier = $this->createNotifierMock();
|
||||||
$notifier->expects($this->once())
|
$notifier->expects($this->once())
|
||||||
->method('notify')
|
->method('notify')
|
||||||
->with(Notifier::MESSAGE_INFORMATIONS)
|
->with(NotifierInterface::MESSAGE_INFORMATION)
|
||||||
->will($this->returnValue([
|
->will($this->returnValue([
|
||||||
'manager' => [
|
'manager' => [
|
||||||
'process-id' => 1234,
|
'process-id' => 1234,
|
||||||
@@ -83,7 +83,7 @@ class LiveInformationTest extends \PhraseanetTestCase
|
|||||||
$notifier = $this->createNotifierMock();
|
$notifier = $this->createNotifierMock();
|
||||||
$notifier->expects($this->once())
|
$notifier->expects($this->once())
|
||||||
->method('notify')
|
->method('notify')
|
||||||
->with(Notifier::MESSAGE_INFORMATIONS)
|
->with(NotifierInterface::MESSAGE_INFORMATION)
|
||||||
->will($this->returnValue(null));
|
->will($this->returnValue(null));
|
||||||
|
|
||||||
$live = new LiveInformation($this->createStatusMock(TaskManagerStatus::STATUS_STARTED), $notifier);
|
$live = new LiveInformation($this->createStatusMock(TaskManagerStatus::STATUS_STARTED), $notifier);
|
||||||
@@ -109,8 +109,6 @@ class LiveInformationTest extends \PhraseanetTestCase
|
|||||||
|
|
||||||
private function createNotifierMock()
|
private function createNotifierMock()
|
||||||
{
|
{
|
||||||
return $this->getMockBuilder('Alchemy\Phrasea\TaskManager\Notifier')
|
return $this->getMock(NotifierInterface::class);
|
||||||
->disableOriginalConstructor()
|
|
||||||
->getMock();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,27 +2,51 @@
|
|||||||
|
|
||||||
namespace Alchemy\Tests\Phrasea\TaskManager;
|
namespace Alchemy\Tests\Phrasea\TaskManager;
|
||||||
|
|
||||||
use Alchemy\TaskManager\TaskManager;
|
use Alchemy\Phrasea\Exception\RuntimeException;
|
||||||
use Alchemy\Phrasea\TaskManager\Notifier;
|
use Alchemy\Phrasea\TaskManager\Notifier;
|
||||||
|
use Alchemy\Phrasea\TaskManager\NotifierInterface;
|
||||||
|
use Alchemy\TaskManager\TaskManager;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
class NotifierTest extends \PhraseanetTestCase
|
class NotifierTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
/** @var \ZMQSocket|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
private $socket;
|
||||||
|
/** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
private $logger;
|
||||||
|
|
||||||
|
/** @var Notifier */
|
||||||
|
private $sut;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->socket = $this->createSocketMock();
|
||||||
|
$this->logger = $this->getMock(LoggerInterface::class);
|
||||||
|
|
||||||
|
$this->sut = new Notifier($this->socket, $this->logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItImplementsNotifierInterface()
|
||||||
|
{
|
||||||
|
$this->assertInstanceOf(NotifierInterface::class, $this->sut);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider provideMessagesData
|
* @dataProvider provideMessagesData
|
||||||
*/
|
*/
|
||||||
public function testNotify($message, $expectedCommand, $result, $expectedReturnValue)
|
public function testNotify($message, $expectedCommand, $result, $expectedReturnValue)
|
||||||
{
|
{
|
||||||
$socket = $this->createSocketMock();
|
$this->socket
|
||||||
$socket->expects($this->once())
|
->expects($this->once())
|
||||||
->method('send')
|
->method('send')
|
||||||
->with($expectedCommand);
|
->with($expectedCommand);
|
||||||
|
|
||||||
$socket->expects($this->once())
|
$this->socket
|
||||||
->method('recv')
|
->expects($this->once())
|
||||||
->will($this->returnValue($result));
|
->method('recv')
|
||||||
|
->will($this->returnValue($result));
|
||||||
|
|
||||||
$notifier = new Notifier($socket, $this->getMock('Psr\Log\LoggerInterface'));
|
$this->assertEquals($expectedReturnValue, $this->sut->notify($message));
|
||||||
$this->assertEquals($expectedReturnValue, $notifier->notify($message));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideMessagesData()
|
public function provideMessagesData()
|
||||||
@@ -33,66 +57,62 @@ class NotifierTest extends \PhraseanetTestCase
|
|||||||
[Notifier::MESSAGE_CREATE, TaskManager::MESSAGE_PROCESS_UPDATE, json_encode(['request' => TaskManager::MESSAGE_PROCESS_UPDATE, 'reply' => TaskManager::RESPONSE_OK]), TaskManager::RESPONSE_OK],
|
[Notifier::MESSAGE_CREATE, TaskManager::MESSAGE_PROCESS_UPDATE, json_encode(['request' => TaskManager::MESSAGE_PROCESS_UPDATE, 'reply' => TaskManager::RESPONSE_OK]), TaskManager::RESPONSE_OK],
|
||||||
[Notifier::MESSAGE_DELETE, TaskManager::MESSAGE_PROCESS_UPDATE, json_encode(['request' => TaskManager::MESSAGE_PROCESS_UPDATE, 'reply' => TaskManager::RESPONSE_OK]), TaskManager::RESPONSE_OK],
|
[Notifier::MESSAGE_DELETE, TaskManager::MESSAGE_PROCESS_UPDATE, json_encode(['request' => TaskManager::MESSAGE_PROCESS_UPDATE, 'reply' => TaskManager::RESPONSE_OK]), TaskManager::RESPONSE_OK],
|
||||||
[Notifier::MESSAGE_UPDATE, TaskManager::MESSAGE_PROCESS_UPDATE, json_encode(['request' => TaskManager::MESSAGE_PROCESS_UPDATE, 'reply' => TaskManager::RESPONSE_OK]), TaskManager::RESPONSE_OK],
|
[Notifier::MESSAGE_UPDATE, TaskManager::MESSAGE_PROCESS_UPDATE, json_encode(['request' => TaskManager::MESSAGE_PROCESS_UPDATE, 'reply' => TaskManager::RESPONSE_OK]), TaskManager::RESPONSE_OK],
|
||||||
[Notifier::MESSAGE_INFORMATIONS, TaskManager::MESSAGE_STATE, json_encode(['request' => TaskManager::MESSAGE_STATE, 'reply' => $managerData]), $managerData],
|
[Notifier::MESSAGE_INFORMATION, TaskManager::MESSAGE_STATE, json_encode(['request' => TaskManager::MESSAGE_STATE, 'reply' => $managerData]), $managerData],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNoresultsThrowsException()
|
public function testNoresultsThrowsException()
|
||||||
{
|
{
|
||||||
$socket = $this->createSocketMock();
|
$this->socket
|
||||||
|
->expects($this->any())
|
||||||
|
->method('recv')
|
||||||
|
->will($this->returnValue(false));
|
||||||
|
|
||||||
$socket->expects($this->any())
|
$this->setExpectedException(RuntimeException::class, 'Unable to retrieve information.');
|
||||||
->method('recv')
|
$this->sut->notify(Notifier::MESSAGE_CREATE);
|
||||||
->will($this->returnValue(false));
|
|
||||||
|
|
||||||
$notifier = new Notifier($socket, $this->getMock('Psr\Log\LoggerInterface'));
|
|
||||||
$this->setExpectedException('Alchemy\Phrasea\Exception\RuntimeException', 'Unable to retrieve information.');
|
|
||||||
$notifier->notify(Notifier::MESSAGE_CREATE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWrongJsonReturnNull()
|
public function testWrongJsonReturnNull()
|
||||||
{
|
{
|
||||||
$socket = $this->createSocketMock();
|
$this->socket
|
||||||
|
->expects($this->once())
|
||||||
|
->method('recv')
|
||||||
|
->will($this->returnValue('wrong json'));
|
||||||
|
|
||||||
$socket->expects($this->once())
|
$this->setExpectedException(RuntimeException::class, 'Invalid task manager response : invalid JSON.');
|
||||||
->method('recv')
|
$this->sut->notify(Notifier::MESSAGE_CREATE);
|
||||||
->will($this->returnValue('wrong json'));
|
|
||||||
|
|
||||||
$notifier = new Notifier($socket, $this->getMock('Psr\Log\LoggerInterface'));
|
|
||||||
$this->setExpectedException('Alchemy\Phrasea\Exception\RuntimeException', 'Invalid task manager response : invalid JSON.');
|
|
||||||
$notifier->notify(Notifier::MESSAGE_CREATE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWrongReplyReturnNull()
|
public function testWrongReplyReturnNull()
|
||||||
{
|
{
|
||||||
$socket = $this->createSocketMock();
|
$this->socket
|
||||||
$socket->expects($this->once())
|
->expects($this->once())
|
||||||
->method('send')
|
->method('send')
|
||||||
->with(TaskManager::MESSAGE_PROCESS_UPDATE);
|
->with(TaskManager::MESSAGE_PROCESS_UPDATE);
|
||||||
|
|
||||||
$socket->expects($this->once())
|
$this->socket
|
||||||
->method('recv')
|
->expects($this->once())
|
||||||
->will($this->returnValue(json_encode(['request' => 'popo', 'reply' => []])));
|
->method('recv')
|
||||||
|
->will($this->returnValue(json_encode(['request' => 'popo', 'reply' => []])));
|
||||||
|
|
||||||
$notifier = new Notifier($socket, $this->getMock('Psr\Log\LoggerInterface'));
|
$this->setExpectedException(RuntimeException::class, 'Invalid task manager response : missing fields.');
|
||||||
$this->setExpectedException('Alchemy\Phrasea\Exception\RuntimeException', 'Invalid task manager response : missing fields.');
|
$this->sut->notify(Notifier::MESSAGE_CREATE);
|
||||||
$notifier->notify(Notifier::MESSAGE_CREATE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMissingRequestReturnNull()
|
public function testMissingRequestReturnNull()
|
||||||
{
|
{
|
||||||
$socket = $this->createSocketMock();
|
$this->socket
|
||||||
$socket->expects($this->once())
|
->expects($this->once())
|
||||||
->method('send')
|
->method('send')
|
||||||
->with(TaskManager::MESSAGE_PROCESS_UPDATE);
|
->with(TaskManager::MESSAGE_PROCESS_UPDATE);
|
||||||
|
|
||||||
$socket->expects($this->once())
|
$this->socket
|
||||||
->method('recv')
|
->expects($this->once())
|
||||||
->will($this->returnValue(json_encode(['request' => TaskManager::MESSAGE_PROCESS_UPDATE])));
|
->method('recv')
|
||||||
|
->will($this->returnValue(json_encode(['request' => TaskManager::MESSAGE_PROCESS_UPDATE])));
|
||||||
|
|
||||||
$notifier = new Notifier($socket, $this->getMock('Psr\Log\LoggerInterface'));
|
$this->setExpectedException(RuntimeException::class, 'Invalid task manager response : missing fields.');
|
||||||
$this->setExpectedException('Alchemy\Phrasea\Exception\RuntimeException', 'Invalid task manager response : missing fields.');
|
$this->sut->notify(Notifier::MESSAGE_CREATE);
|
||||||
$notifier->notify(Notifier::MESSAGE_CREATE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createSocketMock()
|
private function createSocketMock()
|
||||||
|
|||||||
22
tests/Alchemy/Tests/Phrasea/TaskManager/NullNotifierTest.php
Normal file
22
tests/Alchemy/Tests/Phrasea/TaskManager/NullNotifierTest.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Phraseanet
|
||||||
|
*
|
||||||
|
* (c) 2005-2015 Alchemy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Alchemy\Tests\Phrasea\TaskManager;
|
||||||
|
|
||||||
|
use Alchemy\Phrasea\TaskManager\NotifierInterface;
|
||||||
|
use Alchemy\Phrasea\TaskManager\NullNotifier;
|
||||||
|
|
||||||
|
class NullNotifierTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testItImplementsNotifierInterface()
|
||||||
|
{
|
||||||
|
$this->assertInstanceOf(NotifierInterface::class, new NullNotifier());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user