diff --git a/lib/Alchemy/Phrasea/Core/Provider/ManipulatorServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/ManipulatorServiceProvider.php index 1508a8f1e3..b75c97ea6f 100644 --- a/lib/Alchemy/Phrasea/Core/Provider/ManipulatorServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/Provider/ManipulatorServiceProvider.php @@ -34,7 +34,7 @@ class ManipulatorServiceProvider implements ServiceProviderInterface public function register(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) { diff --git a/lib/Alchemy/Phrasea/Model/Manipulator/TaskManipulator.php b/lib/Alchemy/Phrasea/Model/Manipulator/TaskManipulator.php index 0d5d02e5cd..5b2a78ffd6 100644 --- a/lib/Alchemy/Phrasea/Model/Manipulator/TaskManipulator.php +++ b/lib/Alchemy/Phrasea/Model/Manipulator/TaskManipulator.php @@ -14,25 +14,31 @@ namespace Alchemy\Phrasea\Model\Manipulator; use Alchemy\Phrasea\Exception\RuntimeException; use Alchemy\Phrasea\Model\Entities\Task; 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\ORM\EntityRepository; use Symfony\Component\Translation\TranslatorInterface; class TaskManipulator implements ManipulatorInterface { - /** @var Notifier */ + /** @var NotifierInterface */ private $notifier; /** @var ObjectManager */ private $om; /** @var TranslatorInterface */ 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->notifier = $notifier; $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->flush(); - $this->notify(Notifier::MESSAGE_CREATE); + $this->notify(NotifierInterface::MESSAGE_CREATE); return $task; } @@ -83,7 +89,7 @@ class TaskManipulator implements ManipulatorInterface $this->om->persist($task); $this->om->flush(); - $this->notify(Notifier::MESSAGE_CREATE); + $this->notify(NotifierInterface::MESSAGE_CREATE); return $task; } @@ -100,7 +106,7 @@ class TaskManipulator implements ManipulatorInterface $this->om->persist($task); $this->om->flush(); - $this->notify(Notifier::MESSAGE_UPDATE); + $this->notify(NotifierInterface::MESSAGE_UPDATE); return $task; } @@ -115,7 +121,7 @@ class TaskManipulator implements ManipulatorInterface $this->om->remove($task); $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->flush(); - $this->notify(Notifier::MESSAGE_UPDATE); + $this->notify(NotifierInterface::MESSAGE_UPDATE); return $task; } @@ -151,7 +157,7 @@ class TaskManipulator implements ManipulatorInterface $this->om->persist($task); $this->om->flush(); - $this->notify(Notifier::MESSAGE_UPDATE); + $this->notify(NotifierInterface::MESSAGE_UPDATE); return $task; } @@ -170,7 +176,7 @@ class TaskManipulator implements ManipulatorInterface $this->om->persist($task); $this->om->flush(); - $this->notify(Notifier::MESSAGE_UPDATE); + $this->notify(NotifierInterface::MESSAGE_UPDATE); return $task; } diff --git a/lib/Alchemy/Phrasea/TaskManager/LiveInformation.php b/lib/Alchemy/Phrasea/TaskManager/LiveInformation.php index 459034255d..192bf5660b 100644 --- a/lib/Alchemy/Phrasea/TaskManager/LiveInformation.php +++ b/lib/Alchemy/Phrasea/TaskManager/LiveInformation.php @@ -19,7 +19,7 @@ class LiveInformation private $status; private $notifier; - public function __construct(TaskManagerStatus $status, Notifier $notifier) + public function __construct(TaskManagerStatus $status, NotifierInterface $notifier) { $this->status = $status; $this->notifier = $notifier; @@ -87,7 +87,7 @@ class LiveInformation private function query($throwException) { try { - return $this->notifier->notify(Notifier::MESSAGE_INFORMATIONS); + return $this->notifier->notify(NotifierInterface::MESSAGE_INFORMATION); } catch (RuntimeException $e) { if ($throwException) { throw $e; diff --git a/lib/Alchemy/Phrasea/TaskManager/Notifier.php b/lib/Alchemy/Phrasea/TaskManager/Notifier.php index e60f4eaf0c..ef3816e8ad 100644 --- a/lib/Alchemy/Phrasea/TaskManager/Notifier.php +++ b/lib/Alchemy/Phrasea/TaskManager/Notifier.php @@ -16,17 +16,8 @@ use Alchemy\Phrasea\Exception\RuntimeException; use Alchemy\Phrasea\Exception\InvalidArgumentException; 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 */ private $socket; @@ -98,7 +89,7 @@ class Notifier case static::MESSAGE_UPDATE: case static::MESSAGE_DELETE: return TaskManager::MESSAGE_PROCESS_UPDATE; - case static::MESSAGE_INFORMATIONS: + case static::MESSAGE_INFORMATION: return TaskManager::MESSAGE_STATE; default: throw new InvalidArgumentException(sprintf('Unable to understand %s message notification', $message)); diff --git a/lib/Alchemy/Phrasea/TaskManager/NotifierInterface.php b/lib/Alchemy/Phrasea/TaskManager/NotifierInterface.php new file mode 100644 index 0000000000..4808a727c9 --- /dev/null +++ b/lib/Alchemy/Phrasea/TaskManager/NotifierInterface.php @@ -0,0 +1,33 @@ +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() @@ -28,7 +28,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase $this->notifier ->expects($this->once()) ->method('notify') - ->with(Notifier::MESSAGE_CREATE); + ->with(NotifierInterface::MESSAGE_CREATE); $this->assertCount(2, $this->findAllTasks()); @@ -49,7 +49,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase $this->notifier ->expects($this->once()) ->method('notify') - ->with(Notifier::MESSAGE_UPDATE); + ->with(NotifierInterface::MESSAGE_UPDATE); $task = $this->loadTask(); $task->setName('new name'); @@ -64,7 +64,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase $this->notifier ->expects($this->once()) ->method('notify') - ->with(Notifier::MESSAGE_DELETE); + ->with(NotifierInterface::MESSAGE_DELETE); $task = $this->loadTask(); $this->sut->delete($task); @@ -76,7 +76,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase $this->notifier ->expects($this->once()) ->method('notify') - ->with(Notifier::MESSAGE_UPDATE); + ->with(NotifierInterface::MESSAGE_UPDATE); $task = $this->loadTask(); $task->setStatus(Task::STATUS_STOPPED); @@ -92,7 +92,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase $this->notifier ->expects($this->once()) ->method('notify') - ->with(Notifier::MESSAGE_UPDATE); + ->with(NotifierInterface::MESSAGE_UPDATE); $task = $this->loadTask(); $task->setStatus(Task::STATUS_STARTED); @@ -109,7 +109,7 @@ class TaskManipulatorTest extends \PhraseanetTestCase $this->notifier ->expects($this->once()) ->method('notify') - ->with(Notifier::MESSAGE_UPDATE); + ->with(NotifierInterface::MESSAGE_UPDATE); $task = $this->loadTask(); $task->setCrashed(42); @@ -137,6 +137,14 @@ class TaskManipulatorTest extends \PhraseanetTestCase $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[] */ @@ -162,8 +170,6 @@ class TaskManipulatorTest extends \PhraseanetTestCase */ private function createNotifierMock() { - return $this->getMockBuilder(Notifier::class) - ->disableOriginalConstructor() - ->getMock(); + return $this->getMockBuilder(NotifierInterface::class)->getMock(); } } diff --git a/tests/Alchemy/Tests/Phrasea/TaskManager/LiveInformationTest.php b/tests/Alchemy/Tests/Phrasea/TaskManager/LiveInformationTest.php index 81ef0d4365..9c31666524 100644 --- a/tests/Alchemy/Tests/Phrasea/TaskManager/LiveInformationTest.php +++ b/tests/Alchemy/Tests/Phrasea/TaskManager/LiveInformationTest.php @@ -3,7 +3,7 @@ namespace Alchemy\Tests\Phrasea\TaskManager; use Alchemy\Phrasea\TaskManager\LiveInformation; -use Alchemy\Phrasea\TaskManager\Notifier; +use Alchemy\Phrasea\TaskManager\NotifierInterface; use Alchemy\Phrasea\TaskManager\TaskManagerStatus; use Alchemy\Phrasea\Model\Entities\Task; @@ -14,7 +14,7 @@ class LiveInformationTest extends \PhraseanetTestCase $notifier = $this->createNotifierMock(); $notifier->expects($this->once()) ->method('notify') - ->with(Notifier::MESSAGE_INFORMATIONS) + ->with(NotifierInterface::MESSAGE_INFORMATION) ->will($this->returnValue([ 'manager' => [ 'process-id' => 1234, @@ -35,7 +35,7 @@ class LiveInformationTest extends \PhraseanetTestCase $notifier = $this->createNotifierMock(); $notifier->expects($this->once()) ->method('notify') - ->with(Notifier::MESSAGE_INFORMATIONS) + ->with(NotifierInterface::MESSAGE_INFORMATION) ->will($this->returnValue(null)); $live = new LiveInformation($this->createStatusMock(TaskManagerStatus::STATUS_STARTED), $notifier); @@ -54,7 +54,7 @@ class LiveInformationTest extends \PhraseanetTestCase $notifier = $this->createNotifierMock(); $notifier->expects($this->once()) ->method('notify') - ->with(Notifier::MESSAGE_INFORMATIONS) + ->with(NotifierInterface::MESSAGE_INFORMATION) ->will($this->returnValue([ 'manager' => [ 'process-id' => 1234, @@ -83,7 +83,7 @@ class LiveInformationTest extends \PhraseanetTestCase $notifier = $this->createNotifierMock(); $notifier->expects($this->once()) ->method('notify') - ->with(Notifier::MESSAGE_INFORMATIONS) + ->with(NotifierInterface::MESSAGE_INFORMATION) ->will($this->returnValue(null)); $live = new LiveInformation($this->createStatusMock(TaskManagerStatus::STATUS_STARTED), $notifier); @@ -109,8 +109,6 @@ class LiveInformationTest extends \PhraseanetTestCase private function createNotifierMock() { - return $this->getMockBuilder('Alchemy\Phrasea\TaskManager\Notifier') - ->disableOriginalConstructor() - ->getMock(); + return $this->getMock(NotifierInterface::class); } } diff --git a/tests/Alchemy/Tests/Phrasea/TaskManager/NotifierTest.php b/tests/Alchemy/Tests/Phrasea/TaskManager/NotifierTest.php index ad213d8207..290d90ae50 100644 --- a/tests/Alchemy/Tests/Phrasea/TaskManager/NotifierTest.php +++ b/tests/Alchemy/Tests/Phrasea/TaskManager/NotifierTest.php @@ -2,27 +2,51 @@ namespace Alchemy\Tests\Phrasea\TaskManager; -use Alchemy\TaskManager\TaskManager; +use Alchemy\Phrasea\Exception\RuntimeException; 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 */ public function testNotify($message, $expectedCommand, $result, $expectedReturnValue) { - $socket = $this->createSocketMock(); - $socket->expects($this->once()) - ->method('send') - ->with($expectedCommand); + $this->socket + ->expects($this->once()) + ->method('send') + ->with($expectedCommand); - $socket->expects($this->once()) - ->method('recv') - ->will($this->returnValue($result)); + $this->socket + ->expects($this->once()) + ->method('recv') + ->will($this->returnValue($result)); - $notifier = new Notifier($socket, $this->getMock('Psr\Log\LoggerInterface')); - $this->assertEquals($expectedReturnValue, $notifier->notify($message)); + $this->assertEquals($expectedReturnValue, $this->sut->notify($message)); } 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_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_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() { - $socket = $this->createSocketMock(); + $this->socket + ->expects($this->any()) + ->method('recv') + ->will($this->returnValue(false)); - $socket->expects($this->any()) - ->method('recv') - ->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); + $this->setExpectedException(RuntimeException::class, 'Unable to retrieve information.'); + $this->sut->notify(Notifier::MESSAGE_CREATE); } public function testWrongJsonReturnNull() { - $socket = $this->createSocketMock(); + $this->socket + ->expects($this->once()) + ->method('recv') + ->will($this->returnValue('wrong json')); - $socket->expects($this->once()) - ->method('recv') - ->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); + $this->setExpectedException(RuntimeException::class, 'Invalid task manager response : invalid JSON.'); + $this->sut->notify(Notifier::MESSAGE_CREATE); } public function testWrongReplyReturnNull() { - $socket = $this->createSocketMock(); - $socket->expects($this->once()) - ->method('send') - ->with(TaskManager::MESSAGE_PROCESS_UPDATE); + $this->socket + ->expects($this->once()) + ->method('send') + ->with(TaskManager::MESSAGE_PROCESS_UPDATE); - $socket->expects($this->once()) - ->method('recv') - ->will($this->returnValue(json_encode(['request' => 'popo', 'reply' => []]))); + $this->socket + ->expects($this->once()) + ->method('recv') + ->will($this->returnValue(json_encode(['request' => 'popo', 'reply' => []]))); - $notifier = new Notifier($socket, $this->getMock('Psr\Log\LoggerInterface')); - $this->setExpectedException('Alchemy\Phrasea\Exception\RuntimeException', 'Invalid task manager response : missing fields.'); - $notifier->notify(Notifier::MESSAGE_CREATE); + $this->setExpectedException(RuntimeException::class, 'Invalid task manager response : missing fields.'); + $this->sut->notify(Notifier::MESSAGE_CREATE); } public function testMissingRequestReturnNull() { - $socket = $this->createSocketMock(); - $socket->expects($this->once()) - ->method('send') - ->with(TaskManager::MESSAGE_PROCESS_UPDATE); + $this->socket + ->expects($this->once()) + ->method('send') + ->with(TaskManager::MESSAGE_PROCESS_UPDATE); - $socket->expects($this->once()) - ->method('recv') - ->will($this->returnValue(json_encode(['request' => TaskManager::MESSAGE_PROCESS_UPDATE]))); + $this->socket + ->expects($this->once()) + ->method('recv') + ->will($this->returnValue(json_encode(['request' => TaskManager::MESSAGE_PROCESS_UPDATE]))); - $notifier = new Notifier($socket, $this->getMock('Psr\Log\LoggerInterface')); - $this->setExpectedException('Alchemy\Phrasea\Exception\RuntimeException', 'Invalid task manager response : missing fields.'); - $notifier->notify(Notifier::MESSAGE_CREATE); + $this->setExpectedException(RuntimeException::class, 'Invalid task manager response : missing fields.'); + $this->sut->notify(Notifier::MESSAGE_CREATE); } private function createSocketMock() diff --git a/tests/Alchemy/Tests/Phrasea/TaskManager/NullNotifierTest.php b/tests/Alchemy/Tests/Phrasea/TaskManager/NullNotifierTest.php new file mode 100644 index 0000000000..5d12a1a6c9 --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/TaskManager/NullNotifierTest.php @@ -0,0 +1,22 @@ +assertInstanceOf(NotifierInterface::class, new NullNotifier()); + } +}