include plugin worker in phraseanet core

This commit is contained in:
aynsix
2020-05-12 18:06:45 +03:00
parent e4a872dfeb
commit bb2123df5a
77 changed files with 5185 additions and 68 deletions

View File

@@ -0,0 +1,45 @@
<?php
namespace Alchemy\Tests\Phrasea\WorkerManager\Subscriber;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\WorkerManager\Subscriber\ExportSubscriber;
use Alchemy\Phrasea\WorkerManager\Subscriber\RecordSubscriber;
/**
* @covers Alchemy\Phrasea\WorkerManager\Subscriber\ExportSubscriber
* @covers Alchemy\Phrasea\WorkerManager\Subscriber\RecordSubscriber
*/
class SubscriberTest extends \PhraseanetTestCase
{
public function testCallsImplements()
{
$app = new Application(Application::ENV_TEST);
$app['alchemy_worker.message.publisher'] = $this->prophesize('Alchemy\Phrasea\WorkerManager\Queue\MessagePublisher');
$sexportSubscriber = new ExportSubscriber($app['alchemy_worker.message.publisher']->reveal());
$this->assertInstanceOf('Symfony\\Component\\EventDispatcher\\EventSubscriberInterface', $sexportSubscriber);
$recordSubscriber = new RecordSubscriber($app);
$this->assertInstanceOf('Symfony\\Component\\EventDispatcher\\EventSubscriberInterface', $recordSubscriber);
}
public function testIfPublisheMessageOnSubscribeEvent()
{
$app = new Application(Application::ENV_TEST);
$app['alchemy_worker.message.publisher'] = $this->getMockBuilder('Alchemy\Phrasea\WorkerManager\Queue\MessagePublisher')
->disableOriginalConstructor()
->getMock();
$app['alchemy_worker.type_based_worker_resolver'] = $this->getMockBuilder('Alchemy\Phrasea\WorkerManager\Worker\Resolver\TypeBasedWorkerResolver')
->disableOriginalConstructor()
->getMock();
$app['alchemy_worker.message.publisher']->expects($this->atLeastOnce())->method('publishMessage');
$event = $this->prophesize('Alchemy\Phrasea\Core\Event\ExportMailEvent');
$sut = new ExportSubscriber($app['alchemy_worker.message.publisher']);
$sut->onExportMailCreate($event->reveal());
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Alchemy\Tests\Phrasea\WorkerManager\Worker\Factory;
use Alchemy\Phrasea\WorkerManager\Worker\Factory\CallableWorkerFactory;
class CallableWorkerFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testClassImplements()
{
$sut = new CallableWorkerFactory(function () {});
$this->assertInstanceOf('Alchemy\\Phrasea\\WorkerManager\\Worker\\Factory\\WorkerFactoryInterface', $sut);
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Alchemy\Tests\Phrasea\WorkerManager\Worker\Resolver;
use Alchemy\Phrasea\WorkerManager\Queue\MessagePublisher;
use Alchemy\Phrasea\WorkerManager\Worker\Factory\CallableWorkerFactory;
use Alchemy\Phrasea\WorkerManager\Worker\Factory\WorkerFactoryInterface;
use Alchemy\Phrasea\WorkerManager\Worker\Resolver\TypeBasedWorkerResolver;
use Alchemy\Phrasea\WorkerManager\Worker\WorkerInterface;
class TypeBasedWorkerResolverTest extends \PhraseanetTestCase
{
public function testClassImplements()
{
$sut = new TypeBasedWorkerResolver();
$this->assertInstanceOf('Alchemy\\Phrasea\\WorkerManager\\Worker\\Resolver\\WorkerResolverInterface', $sut);
}
public function testGetFactories()
{
$workerFactory = $this->getMockBuilder(WorkerFactoryInterface::class)
->disableOriginalConstructor()
->getMock()
;
$sut = new TypeBasedWorkerResolver();
$sut->addFactory(MessagePublisher::SUBDEF_CREATION_TYPE, $workerFactory);
$this->assertContainsOnlyInstancesOf(WorkerFactoryInterface::class, $sut->getFactories());
}
public function testGetWorkerSuccess()
{
$worker = $this->getMockBuilder(WorkerInterface::class)
->disableOriginalConstructor()
->getMock();
$workerFactory = $this->getMockBuilder(CallableWorkerFactory::class)
->disableOriginalConstructor()
->getMock();
$workerFactory->method('createWorker')->will($this->returnValue($worker));
$sut = new TypeBasedWorkerResolver();
$sut->addFactory(MessagePublisher::SUBDEF_CREATION_TYPE, $workerFactory);
$this->assertInstanceOf('Alchemy\\Phrasea\\WorkerManager\\Worker\\WorkerInterface',
$sut->getWorker(MessagePublisher::SUBDEF_CREATION_TYPE, ['mock-message']));
}
public function testGetWorkerWrongTypeThrowException()
{
$worker = $this->getMockBuilder(WorkerInterface::class)
->disableOriginalConstructor()
->getMock();
$workerFactory = $this->getMockBuilder(CallableWorkerFactory::class)
->disableOriginalConstructor()
->getMock();
$workerFactory->method('createWorker')->will($this->returnValue($worker));
$sut = new TypeBasedWorkerResolver();
$sut->addFactory(MessagePublisher::SUBDEF_CREATION_TYPE, $workerFactory);
$this->expectException(\RuntimeException::class);
$sut->getWorker(MessagePublisher::WRITE_METADATAS_TYPE, ['mock-message']);
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace Alchemy\Tests\Phrasea\WorkerManager\Worker;
use Alchemy\Phrasea\WorkerManager\Queue\MessagePublisher;
use Alchemy\Phrasea\WorkerManager\Worker\ProcessPool;
use Alchemy\Phrasea\WorkerManager\Worker\WorkerInvoker;
use Symfony\Component\Process\Exception\RuntimeException as ProcessRuntimeException;
use Symfony\Component\Process\Process;
class WorkerInvokerTest extends \PhraseanetTestCase
{
public function testClassImplements()
{
$processPool = $this->prophesize(ProcessPool::class);
$sut = new WorkerInvoker($processPool->reveal());
$this->assertInstanceOf('Psr\\Log\\LoggerAwareInterface', $sut);
}
public function testInvokeWorkerSuccess()
{
$process = $this->getMockBuilder(Process::class)
->disableOriginalConstructor()
->getMock()
;
$process ->expects($this->exactly(1))
->method('start')
;
$processPool = $this->getMockBuilder(ProcessPool::class)
->disableOriginalConstructor()
->getMock();
$processPool->method('getWorkerProcess')->will($this->returnValue($process));
$sut = new WorkerInvoker($processPool);
$sut->invokeWorker(MessagePublisher::SUBDEF_CREATION_TYPE, json_encode(['mock-payload']));
}
public function testInvokeWorkerWhenThrowException()
{
$process = $this->getMockBuilder(Process::class)
->disableOriginalConstructor()
->getMock()
;
$process ->expects($this->exactly(1))
->method('start')
->will($this->throwException(new ProcessRuntimeException()))
;
$processPool = $this->getMockBuilder(ProcessPool::class)
->disableOriginalConstructor()
->getMock();
$processPool->method('getWorkerProcess')->will($this->returnValue($process));
$sut = new WorkerInvoker($processPool);
try {
$sut->invokeWorker(MessagePublisher::SUBDEF_CREATION_TYPE, json_encode(['mock-payload']));
$this->fail('Should have raised an exception');
} catch (\Exception $e) {
}
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Alchemy\Tests\Phrasea\WorkerManager\Worker;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\WorkerManager\Worker\AssetsIngestWorker;
use Alchemy\Phrasea\WorkerManager\Worker\CreateRecordWorker;
use Alchemy\Phrasea\WorkerManager\Worker\ExportMailWorker;
use Alchemy\Phrasea\WorkerManager\Worker\SubdefCreationWorker;
use Alchemy\Phrasea\WorkerManager\Worker\WriteMetadatasWorker;
/**
* @covers Alchemy\Phrasea\WorkerManager\Worker\ExportMailWorker
* @covers Alchemy\Phrasea\WorkerManager\Worker\SubdefCreationWorker
* @covers Alchemy\Phrasea\WorkerManager\Worker\WriteMetadatasWorker
*/
class WorkerServiceTest extends \PHPUnit_Framework_TestCase
{
public function testImplementationClass()
{
$app = new Application(Application::ENV_TEST);
$exportMailWorker = new ExportMailWorker($app);
$this->assertInstanceOf('Alchemy\\Phrasea\\WorkerManager\\Worker\\WorkerInterface', $exportMailWorker);
$app['subdef.generator'] = $this->prophesize('Alchemy\Phrasea\Media\SubdefGenerator')->reveal();
$app['alchemy_worker.message.publisher'] = $this->prophesize('Alchemy\Phrasea\WorkerManager\Queue\MessagePublisher')->reveal();
$app['alchemy_worker.logger'] = $this->prophesize("Monolog\Logger")->reveal();
$app['dispatcher'] = $this->prophesize('Symfony\Component\EventDispatcher\EventDispatcherInterface')->reveal();
$app['phraseanet.filesystem'] = $this->prophesize('Alchemy\Phrasea\Filesystem\FilesystemService')->reveal();
$app['repo.worker-running-job'] = $this->prophesize('Alchemy\Phrasea\Model\Repositories\WorkerRunningJobRepository')->reveal();
$writer = $this->prophesize('PHPExiftool\Writer')->reveal();
$subdefCreationWorker = new SubdefCreationWorker(
$app['subdef.generator'],
$app['alchemy_worker.message.publisher'],
$app['alchemy_worker.logger'],
$app['dispatcher'],
$app['phraseanet.filesystem'],
$app['repo.worker-running-job']
);
$this->assertInstanceOf('Alchemy\\Phrasea\\WorkerManager\\Worker\\WorkerInterface', $subdefCreationWorker);
$writemetadatasWorker = new WriteMetadatasWorker(
$writer,
$app['alchemy_service.logger'],
$app['alchemy_service.message.publisher'],
$app['repo.worker-running-job']
);
$this->assertInstanceOf('Alchemy\\Phrasea\\WorkerManager\\Worker\\WorkerInterface', $writemetadatasWorker);
$assetsWorker = new AssetsIngestWorker($app);
$this->assertInstanceOf('Alchemy\\Phrasea\\WorkerManager\\Worker\\WorkerInterface', $assetsWorker);
$createRecordWorker = new CreateRecordWorker($app);
$this->assertInstanceOf('Alchemy\\Phrasea\\WorkerManager\\Worker\\WorkerInterface', $createRecordWorker);
}
}