<commit_before><?php
namespace Oro\Bundle\WorkflowBundle\Tests\Unit\Async;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\WorkflowBundle\Async\ExecuteProcessJobProcessor;
use Oro\Bundle\WorkflowBundle\Async\Topics;
use Oro\Bundle\WorkflowBundle\Entity\ProcessJob;
use Oro\Bundle\WorkflowBundle\Model\ProcessHandler;
use Oro\Component\MessageQueue\Client\TopicSubscriberInterface;
use Oro\Component\MessageQueue\Consumption\MessageProcessorInterface;
use Oro\Component\MessageQueue\Transport\Null\NullMessage;
use Oro\Component\MessageQueue\Transport\Null\NullSession;
use Oro\Component\MessageQueue\Util\JSON;
use Oro\Component\Testing\ClassExtensionTrait;
use Psr\Log\LoggerInterface;

class ExecuteProcessJobProcessorTest extends \PHPUnit_Framework_TestCase
{
    use ClassExtensionTrait;

    public function testShouldImplementMessageProcessorInterface()
    {
        $this->assertClassImplements(MessageProcessorInterface::class, ExecuteProcessJobProcessor::class);
    }

    public function testShouldImplementTopicSubscriberInterface()
    {
        $this->assertClassImplements(TopicSubscriberInterface::class, ExecuteProcessJobProcessor::class);
    }

    public function testShouldSubscribeOnExecuteProcessJobTopic()
    {
        self::assertEquals([Topics::EXECUTE_PROCESS_JOB], ExecuteProcessJobProcessor::getSubscribedTopics());
    }

    public function testCouldBeConstructedWithDoctrineHelperAndProcessHandler()
    {
        new ExecuteProcessJobProcessor(
            $this->createDoctrineHelperStub(),
            $this->createProcessHandlerMock(),
            $this->createLoggerMock()
        );
    }

    public function testThrowIfMessageBodyIsNotValidJson()
    {
        $processor = new ExecuteProcessJobProcessor(
            $this->createDoctrineHelperStub(),
            $this->createProcessHandlerMock(),
            $this->createLoggerMock()
        );

        $message = new NullMessage();
        $message->setBody('{]');

        $this->setExpectedException(\InvalidArgumentException::class, 'The malformed json given.');
        $processor->process($message, new NullSession());
    }

    public function testShouldRejectMessageIfProcessJobIdNotSet()
    {
        $logger = $this->createLoggerMock();

        $logger
            ->expects(self::once())
            ->method('critical')
        ;

        $processor = new ExecuteProcessJobProcessor(
            $this->createDoctrineHelperStub(),
            $this->createProcessHandlerMock(),
            $logger
        );

        $message = new NullMessage();
        $message->setBody('{}');

        $status = $processor->process($message, new NullSession());

        self::assertEquals(MessageProcessorInterface::REJECT, $status);
    }

    public function testShouldRejectMessageIfProcessJobIdWithSuchIdNotFound()
    {
        $entityManager = $this->createEntityManagerMock();

        $entityRepository = $this->createEntityRepositoryMock();
        $entityRepository
            ->expects($this->once())
            ->method('find')
            ->with('theProcessJobId')
            ->willReturn(null)
        ;

        $doctrineHelper = $this->createDoctrineHelperStub($entityManager, $entityRepository);

        $logger = $this->createLoggerMock();

        $logger
            ->expects(self::once())
            ->method('critical')
        ;

        $processor = new ExecuteProcessJobProcessor(
            $doctrineHelper,
            $this->createProcessHandlerMock(),
            $logger
        );

        $message = new NullMessage();
        $message->setBody(JSON::encode(['process_job_id' => 'theProcessJobId']));

        $status = $processor->process($message, new NullSession());

        self::assertEquals(MessageProcessorInterface::REJECT, $status);
    }

    public function testShouldRejectMessageIfEntityManagerNotExist()
    {
        $doctrineHelper = $this->createDoctrineHelperStub();

        $processHandle = $this->createProcessHandlerMock();

        $logger = $this->createLoggerMock();

        $logger
            ->expects(self::once())
            ->method('critical')
         ;

        $processor = new ExecuteProcessJobProcessor(
            $doctrineHelper,
            $processHandle,
            $logger
        );

        $message = new NullMessage();
        $message->setBody(JSON::encode(['process_job_id' => 'theProcessJobId']));

        $status = $processor->process($message, new NullSession());

        self::assertEquals(MessageProcessorInterface::REJECT, $status);
    }

    public function testShouldExecuteProcessJob()
    {
        $entityManager = $this->createEntityManagerMock();
        $entityManager
            ->expects(self::once())
            ->method('transactional')
        ;

        $processJob = new ProcessJob();

        $entityRepository = $this->createEntityRepositoryMock();
        $entityRepository
            ->expects($this->once())
            ->method('find')
            ->with('theProcessJobId')
            ->willReturn($processJob)
        ;

        $processHandle = $this->createProcessHandlerMock();
        $processHandle
            ->expects($this->once())
            ->method('handleJob')
            ->with(self::identicalTo($processJob))
        ;
        $processHandle
            ->expects($this->once())
            ->method('finishJob')
            ->with(self::identicalTo($processJob))<commit_msg>BAP-11535: migrate workflow bundle logic to mq<commit_after><?php
namespace Oro\Bundle\WorkflowBundle\Tests\Unit\Async;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\WorkflowBundle\Async\ExecuteProcessJobProcessor;
use Oro\Bundle\WorkflowBundle\Async\Topics;
use Oro\Bundle\WorkflowBundle\Entity\ProcessJob;
use Oro\Bundle\WorkflowBundle\Model\ProcessHandler;
use Oro\Component\MessageQueue\Client\TopicSubscriberInterface;
use Oro\Component\MessageQueue\Consumption\MessageProcessorInterface;
use Oro\Component\MessageQueue\Transport\Null\NullMessage;
use Oro\Component\MessageQueue\Transport\Null\NullSession;
use Oro\Component\MessageQueue\Util\JSON;
use Oro\Component\Testing\ClassExtensionTrait;
use Psr\Log\LoggerInterface;

class ExecuteProcessJobProcessorTest extends \PHPUnit_Framework_TestCase
{
    use ClassExtensionTrait;

    public function testShouldImplementMessageProcessorInterface()
    {
        $this->assertClassImplements(MessageProcessorInterface::class, ExecuteProcessJobProcessor::class);
    }

    public function testShouldImplementTopicSubscriberInterface()
    {
        $this->assertClassImplements(TopicSubscriberInterface::class, ExecuteProcessJobProcessor::class);
    }

    public function testShouldSubscribeOnExecuteProcessJobTopic()
    {
        self::assertEquals([Topics::EXECUTE_PROCESS_JOB], ExecuteProcessJobProcessor::getSubscribedTopics());
    }

    public function testCouldBeConstructedWithDoctrineHelperAndProcessHandler()
    {
        new ExecuteProcessJobProcessor(
            $this->createDoctrineHelperStub(),
            $this->createProcessHandlerMock(),
            $this->createLoggerMock()
        );
    }

    public function testThrowIfMessageBodyIsNotValidJson()
    {
        $processor = new ExecuteProcessJobProcessor(
            $this->createDoctrineHelperStub(),
            $this->createProcessHandlerMock(),
            $this->createLoggerMock()
        );

        $message = new NullMessage();
        $message->setBody('{]');

        $this->setExpectedException(\InvalidArgumentException::class, 'The malformed json given.');
        $processor->process($message, new NullSession());
    }

    public function testShouldRejectMessageIfProcessJobIdNotSet()
    {
        $logger = $this->createLoggerMock();

        $logger
            ->expects(self::once())
            ->method('critical')
        ;

        $processor = new ExecuteProcessJobProcessor(
            $this->createDoctrineHelperStub(),
            $this->createProcessHandlerMock(),
            $logger
        );

        $message = new NullMessage();
        $message->setBody('{}');

        $status = $processor->process($message, new NullSession());

        self::assertEquals(MessageProcessorInterface::REJECT, $status);
    }

    public function testShouldRejectMessageIfProcessJobIdWithSuchIdNotFound()
    {
        $entityManager = $this->createEntityManagerMock();

        $entityRepository = $this->createEntityRepositoryMock();
        $entityRepository
            ->expects($this->once())
            ->method('find')
            ->with('theProcessJobId')
            ->willReturn(null)
        ;

        $doctrineHelper = $this->createDoctrineHelperStub($entityManager, $entityRepository);

        $logger = $this->createLoggerMock();

        $logger
            ->expects(self::once())
            ->method('critical')
        ;

        $processor = new ExecuteProcessJobProcessor(
            $doctrineHelper,
            $this->createProcessHandlerMock(),
            $logger
        );

        $message = new NullMessage();
        $message->setBody(JSON::encode(['process_job_id' => 'theProcessJobId']));

        $status = $processor->process($message, new NullSession());

        self::assertEquals(MessageProcessorInterface::REJECT, $status);
    }

    public function testShouldRejectMessageIfEntityManagerNotExist()
    {
        $doctrineHelper = $this->createDoctrineHelperStub();

        $processHandle = $this->createProcessHandlerMock();

        $logger = $this->createLoggerMock();

        $logger
            ->expects(self::once())
            ->method('critical')
         ;

        $processor = new ExecuteProcessJobProcessor(
            $doctrineHelper,
            $processHandle,
            $logger
        );

        $message = new NullMessage();
        $message->setBody(JSON::encode(['process_job_id' => 'theProcessJobId']));

        $status = $processor->process($message, new NullSession());

        self::assertEquals(MessageProcessorInterface::REJECT, $status);
    }

    public function testShouldExecuteProcessJob()
    {

        $processJob = new ProcessJob();

        $entityManager = $this->createEntityManagerMock();
        $entityManager
            ->expects(self::once())
            ->method('transactional')
            ->willReturnCallback(function ($callback) use ($entityManager){
                $callback($entityManager);
            })
        ;

        $entityRepository = $this->createEntityRepositoryMock();
        $entityRepository
            ->expects($this->once())
            ->method('find')
            ->with('theProcessJobId')
            ->willReturn($processJob)
        ;

        $processHandle = $this->createProcessHandlerMock();
        $processHandle
            ->expects($this->once())
            ->method('handleJob')
            ->with(self::identicalTo($processJob))
        ;
        $processHandle
            ->expects($this->once())
            ->method('finishJob')
            ->with(self::identicalTo($processJob))
{'fraction_non_alphanumeric': 0.11997498659996426, 'fraction_numerical': 0.0004466678577809541, 'mean_word_length': 2.9034170153417014, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 122, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6206145', 'n_tokens_mistral': 2895, 'n_tokens_neox': 2827, 'n_words': 408}