<commit_before><?php

/*
 * This file is part of the Integrated package.
 *
 * (c) e-Active B.V. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Integrated\Tests\MongoDB\Solr\Indexer;

use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
use Doctrine\ODM\MongoDB\Events;

use Integrated\Common\Content\ContentInterface;
use Integrated\Common\Queue\QueueInterface;

use Integrated\MongoDB\Solr\Indexer\QueueSubscriber;

use Symfony\Component\Serializer\SerializerInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
class QueueSubscriberTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var QueueInterface | \PHPUnit_Framework_MockObject_MockObject
     */
    private $queue;

    /**
     * @var SerializerInterface | \PHPUnit_Framework_MockObject_MockObject
     */
    private $serializer;

    /**
     * @var QueueSubscriber
     */
    private $subscriber;

    protected function setUp()
    {
        $this->queue = $this->getMock('Integrated\\Common\\Queue\\QueueInterface');
        $this->serializer = $this->getMock('Symfony\\Component\\Serializer\\SerializerInterface');

        $this->subscriber = new QueueSubscriber($this->queue, $this->serializer);
    }

    public function testInterface()
    {
        $this->assertInstanceOf('Doctrine\\Common\\EventSubscriber', $this->subscriber);
        $this->assertInstanceOf('Integrated\\Common\\Queue\\QueueAwareInterface', $this->subscriber);
        $this->assertInstanceOf('Symfony\\Component\\Serializer\\SerializerAwareInterface', $this->subscriber);
    }

    public function testSetAndGetQueue()
    {
        $this->assertSame($this->queue, $this->subscriber->getQueue());

        $mock = $this->getMock('Integrated\\Common\\Queue\\QueueInterface');
        $this->subscriber->setQueue($mock);

        $this->assertSame($mock, $this->subscriber->getQueue());
    }

    public function testSetAndGetSerializer()
    {
        $this->assertSame($this->serializer, $this->subscriber->getSerializer());

        $mock = $this->getMock('Symfony\\Component\\Serializer\\SerializerInterface');
        $this->subscriber->setSerializer($mock);

        $this->assertSame($mock, $this->subscriber->getSerializer());
    }

    public function testSetAndGetSerializerFormat()
    {
        $this->subscriber->setSerializerFormat('format');
        $this->assertEquals('format', $this->subscriber->getSerializerFormat());
    }

    public function testGetDefaultSerializerFormat()
    {
        $this->assertEquals('json', $this->subscriber->getSerializerFormat());
    }

    public function testSetAndGetPriority()
    {
        $this->assertSame(0, $this->subscriber->getPriority());

        $this->subscriber->setPriority(42);

        $this->assertSame(42, $this->subscriber->getPriority());
    }

    public function testGetSubscribedEvents()
    {
        $this->assertEquals([Events::postPersist, Events::postUpdate, Events::postRemove],
                $this->subscriber->getSubscribedEvents());
    }

    public function testPostPersist()
    {
        $document = $this->getDocument('this-is-the-id', 'this-is-the-type');
        $event = $this->getEvent($document);

        $this->serializer->expects($this->atLeastOnce())
            ->method('serialize')
            ->with($this->identicalTo($document), $this->identicalTo('json'))
            ->willReturn('this-is-the-data');

        $callback = function ($value) use ($document) {
            return $value instanceof \Integrated\Common\Solr\Indexer\JobInterface
                && strtolower($value->getAction()) === 'add'
                && $value->getOption('document.id') === 'this-is-the-type-this-is-the-id'
                && $value->getOption('document.data') === 'this-is-the-data'
                && $value->getOption('document.class') === get_class($document)
                && $value->getOption('document.format') === 'json';
        };

        $this->queue->expects($this->once())
            ->method('push')
            ->with($this->callback($callback), $this->identicalTo(0), $this->identicalTo(0));

        $this->subscriber->postPersist($event);
    }

    public function testPostUpdate()
    {
        $document = $this->getDocument('this-is-the-id', 'this-is-the-type');
        $event = $this->getEvent($document);

        $this->serializer->expects($this->atLeastOnce())
            ->method('serialize')
            ->with($this->identicalTo($document), $this->identicalTo('json'))
            ->willReturn('this-is-the-data');

        $callback = function ($value) use ($document) {
            return $value instanceof \Integrated\Common\Solr\Indexer\JobInterface
                && strtolower($value->getAction()) === 'add'
                && $value->getOption('document.id') === 'this-is-the-type-this-is-the-id'
                && $value->getOption('document.data') === 'this-is-the-data'
                && $value->getOption('document.class') === get_class($document)
                && $value->getOption('document.format') === 'json';
        };

        $this->queue->expects($this->once())
            ->method('push')
            ->with($this->callback($callback), $this->identicalTo(0), $this->identicalTo(0));

        $this->subscriber->postUpdate($event);
    }

    public function testPostRemove()
    {
        $event = $this->getEvent($this->getDocument('this-is-the-id', 'this-is-the-type'));

        $callback = function ($value) {
            return $value instanceof \Integrated\Common\Solr\Indexer\JobInterface
                && strtolower($value->getAction()) === 'delete'
                && $value->getOption('id') === 'this-is-the-type-this-is-the-id';
        };

        $this->queue->expects($this->once())
            ->method('push')
            ->with($this->callback($callback), $this->identicalTo(0), $this->identicalTo(0));

        $this->subscriber->postRemove($event);
    }

    public function testPriority()
    {
        $event = $this->getEvent($this->getDocument('this-is-the-id', 'this-is-the-type'));

        $this->queue->expects($this->once())
            ->method('push')
            ->with($this->anything(), $this->identicalTo(0), $this->identicalTo(42));<commit_msg>If the priority is higher then 5 the push is called two times<commit_after><?php

/*
 * This file is part of the Integrated package.
 *
 * (c) e-Active B.V. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Integrated\Tests\MongoDB\Solr\Indexer;

use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
use Doctrine\ODM\MongoDB\Events;

use Integrated\Common\Content\ContentInterface;
use Integrated\Common\Queue\QueueInterface;

use Integrated\MongoDB\Solr\Indexer\QueueSubscriber;

use Symfony\Component\Serializer\SerializerInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
class QueueSubscriberTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var QueueInterface | \PHPUnit_Framework_MockObject_MockObject
     */
    private $queue;

    /**
     * @var SerializerInterface | \PHPUnit_Framework_MockObject_MockObject
     */
    private $serializer;

    /**
     * @var QueueSubscriber
     */
    private $subscriber;

    protected function setUp()
    {
        $this->queue = $this->getMock('Integrated\\Common\\Queue\\QueueInterface');
        $this->serializer = $this->getMock('Symfony\\Component\\Serializer\\SerializerInterface');

        $this->subscriber = new QueueSubscriber($this->queue, $this->serializer);
    }

    public function testInterface()
    {
        $this->assertInstanceOf('Doctrine\\Common\\EventSubscriber', $this->subscriber);
        $this->assertInstanceOf('Integrated\\Common\\Queue\\QueueAwareInterface', $this->subscriber);
        $this->assertInstanceOf('Symfony\\Component\\Serializer\\SerializerAwareInterface', $this->subscriber);
    }

    public function testSetAndGetQueue()
    {
        $this->assertSame($this->queue, $this->subscriber->getQueue());

        $mock = $this->getMock('Integrated\\Common\\Queue\\QueueInterface');
        $this->subscriber->setQueue($mock);

        $this->assertSame($mock, $this->subscriber->getQueue());
    }

    public function testSetAndGetSerializer()
    {
        $this->assertSame($this->serializer, $this->subscriber->getSerializer());

        $mock = $this->getMock('Symfony\\Component\\Serializer\\SerializerInterface');
        $this->subscriber->setSerializer($mock);

        $this->assertSame($mock, $this->subscriber->getSerializer());
    }

    public function testSetAndGetSerializerFormat()
    {
        $this->subscriber->setSerializerFormat('format');
        $this->assertEquals('format', $this->subscriber->getSerializerFormat());
    }

    public function testGetDefaultSerializerFormat()
    {
        $this->assertEquals('json', $this->subscriber->getSerializerFormat());
    }

    public function testSetAndGetPriority()
    {
        $this->assertSame(0, $this->subscriber->getPriority());

        $this->subscriber->setPriority(42);

        $this->assertSame(42, $this->subscriber->getPriority());
    }

    public function testGetSubscribedEvents()
    {
        $this->assertEquals([Events::postPersist, Events::postUpdate, Events::postRemove],
                $this->subscriber->getSubscribedEvents());
    }

    public function testPostPersist()
    {
        $document = $this->getDocument('this-is-the-id', 'this-is-the-type');
        $event = $this->getEvent($document);

        $this->serializer->expects($this->atLeastOnce())
            ->method('serialize')
            ->with($this->identicalTo($document), $this->identicalTo('json'))
            ->willReturn('this-is-the-data');

        $callback = function ($value) use ($document) {
            return $value instanceof \Integrated\Common\Solr\Indexer\JobInterface
                && strtolower($value->getAction()) === 'add'
                && $value->getOption('document.id') === 'this-is-the-type-this-is-the-id'
                && $value->getOption('document.data') === 'this-is-the-data'
                && $value->getOption('document.class') === get_class($document)
                && $value->getOption('document.format') === 'json';
        };

        $this->queue->expects($this->once())
            ->method('push')
            ->with($this->callback($callback), $this->identicalTo(0), $this->identicalTo(0));

        $this->subscriber->postPersist($event);
    }

    public function testPostUpdate()
    {
        $document = $this->getDocument('this-is-the-id', 'this-is-the-type');
        $event = $this->getEvent($document);

        $this->serializer->expects($this->atLeastOnce())
            ->method('serialize')
            ->with($this->identicalTo($document), $this->identicalTo('json'))
            ->willReturn('this-is-the-data');

        $callback = function ($value) use ($document) {
            return $value instanceof \Integrated\Common\Solr\Indexer\JobInterface
                && strtolower($value->getAction()) === 'add'
                && $value->getOption('document.id') === 'this-is-the-type-this-is-the-id'
                && $value->getOption('document.data') === 'this-is-the-data'
                && $value->getOption('document.class') === get_class($document)
                && $value->getOption('document.format') === 'json';
        };

        $this->queue->expects($this->once())
            ->method('push')
            ->with($this->callback($callback), $this->identicalTo(0), $this->identicalTo(0));

        $this->subscriber->postUpdate($event);
    }

    public function testPostRemove()
    {
        $event = $this->getEvent($this->getDocument('this-is-the-id', 'this-is-the-type'));

        $callback = function ($value) {
            return $value instanceof \Integrated\Common\Solr\Indexer\JobInterface
                && strtolower($value->getAction()) === 'delete'
                && $value->getOption('id') === 'this-is-the-type-this-is-the-id';
        };

        $this->queue->expects($this->once())
            ->method('push')
            ->with($this->callback($callback), $this->identicalTo(0), $this->identicalTo(0));

        $this->subscriber->postRemove($event);
    }

    public function testPriority()
    {
        $event = $this->getEvent($this->getDocument('this-is-the-id', 'this-is-the-type'));

        $this->queue->expects($this->exactly(2))
            ->method('push')
            ->with($this->anything(), $this->identicalTo(0), $this->identicalTo(42));
{'fraction_non_alphanumeric': 0.1756799493991145, 'fraction_numerical': 0.0023719165085388993, 'mean_word_length': 2.910046367851623, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 265, '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': '3316373', 'n_tokens_mistral': 3701, 'n_tokens_neox': 3581, 'n_words': 497}