<commit_before><?php

namespace Oro\Bundle\CalendarBundle\Tests\Unit\Autocomplete;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;

use Oro\Bundle\CalendarBundle\Autocomplete\AttendeeSearchHandler;
use Oro\Bundle\CalendarBundle\Tests\Unit\Fixtures\Entity\Attendee;
use Oro\Bundle\CalendarBundle\Tests\Unit\Fixtures\Entity\User;
use Oro\Bundle\CalendarBundle\Manager\AttendeeManager;
use Oro\Bundle\EntityExtendBundle\Tests\Unit\Fixtures\TestEnumValue;
use Oro\Bundle\SearchBundle\Engine\Indexer;
use Oro\Bundle\SearchBundle\Query\Query;
use Oro\Bundle\SearchBundle\Query\Result;
use Oro\Bundle\SearchBundle\Query\Result\Item;

class AttendeeSearchHandlerTest extends \PHPUnit_Framework_TestCase
{
    /** @var \PHPUnit_Framework_MockObject_MockObject|Indexer */
    protected $indexer;

    /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager */
    protected $om;

    /** @var EntityRepository */
    protected $entityRepository;

    /** @var \PHPUnit_Framework_MockObject_MockObject|AttendeeManager */
    protected $attendeeManager;

    /** @var AttendeeSearchHandler */
    protected $attendeeSearchHandler;

    public function setUp()
    {
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
        $translator->expects($this->any())
            ->method('trans')
            ->will(
                $this->returnCallback(
                    function ($id) {
                        return $id;
                    }
                )
            );

        $this->indexer = $this->getMockBuilder('Oro\Bundle\SearchBundle\Engine\Indexer')
            ->disableOriginalConstructor()
            ->getMock();

        $activityManager = $this->getMockBuilder('Oro\Bundle\ActivityBundle\Manager\ActivityManager')
            ->disableOriginalConstructor()
            ->getMock();

        $configManager = $this->getMockBuilder('Oro\Bundle\EntityConfigBundle\Config\ConfigManager')
            ->disableOriginalConstructor()
            ->getMock();

        $entityClassNameHelper = $this->getMockBuilder('Oro\Bundle\EntityBundle\Tools\EntityClassNameHelper')
            ->disableOriginalConstructor()
            ->getMock();

        $this->om = $this->createMock('Doctrine\Common\Persistence\ObjectManager');

        $nameResolver = $this->getMockBuilder('Oro\Bundle\EntityBundle\Provider\EntityNameResolver')
            ->disableOriginalConstructor()
            ->getMock();

        $dispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');

        $this->attendeeManager = $this
            ->getMockBuilder('Oro\Bundle\CalendarBundle\Manager\AttendeeManager')
            ->disableOriginalConstructor()
            ->getMock();

        $this->entityRepository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
            ->setMethods(['findById'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->om->expects($this->any())
            ->method('getRepository')
            ->with('entity')
            ->will($this->returnValue($this->entityRepository));

        $this->attendeeSearchHandler = new AttendeeSearchHandler(
            new TokenStorage(),
            $translator,
            $this->indexer,
            $activityManager,
            $configManager,
            $entityClassNameHelper,
            $this->om,
            $nameResolver,
            $dispatcher
        );

        $this->attendeeSearchHandler->setAttendeeManager($this->attendeeManager);
    }

    public function testSearch()
    {
        $items = [
            new Item($this->om, 'entity', 1, 'record1'),
            new Item($this->om, 'entity', 2, 'record2'),
        ];

        $users = [
            (new User(1))
                ->setEmail('<EMAIL>')
                ->setFirstName('user1'),
            (new User(2))
                ->setEmail('<EMAIL>')
                ->setFirstName('user2'),
        ];

        $this->indexer->expects($this->once())
            ->method('simpleSearch')
            ->with('query', 0, 101, ['oro_user'], 1)
            ->will($this->returnValue(new Result(new Query(), $items)));

        $this->entityRepository->expects($this->once())
            ->method('findById')
            ->with([1, 2])
            ->will($this->returnValue($users));

        $this->attendeeManager->expects($this->exactly(2))
            ->method('createAttendee')
            ->withConsecutive(
                [$users[0]],
                [$users[1]]
            )
            ->will(
                $this->returnCallback(
                    function (User $user) {
                        return (new Attendee)
                            ->setUser($user)
                            ->setDisplayName($user->getFirstName())
                            ->setEmail($user->getEmail())
                            ->setStatus(new TestEnumValue('test', 'test'))
                            ->setType(new TestEnumValue('test', 'test'));
                    }
                )
            );

        $result = $this->attendeeSearchHandler->search('query', 1, 100);

        $this->assertEquals(
            [
                'results' => [
                    [
                        'id'          => json_encode(
                            [
                                'entityClass' => 'Oro\Bundle\CalendarBundle\Tests\Unit\Fixtures\Entity\User',
                                'entityId'    => 1,
                            ]
                        ),
                        'text'        => 'user1',
                        'displayName' => 'user1',
                        'email'       => '<EMAIL>',
                        'status'      => 'test',
                        'type'        => 'test',
                        'userId'      => 1,
                    ],
                    [
                        'id'          => json_encode(
                            [
                                'entityClass' => 'Oro\Bundle\CalendarBundle\Tests\Unit\Fixtures\Entity\User',
                                'entityId'    => 2,
                            ]
                        ),
                        'text'        => 'user2',
                        'displayName' => 'user2',
                        'email'       => '<EMAIL>',
                        'status'      => 'test',
                        'type'        => 'test',
                        'userId'      => 2,
                    ],
                ],
                'more'    => false,
            ],
            $result
        );
    }
}
<commit_msg>BAP-12591: Remove entity manager dependency from Item model   (#5537)<commit_after><?php

namespace Oro\Bundle\CalendarBundle\Tests\Unit\Autocomplete;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;

use Oro\Bundle\CalendarBundle\Autocomplete\AttendeeSearchHandler;
use Oro\Bundle\CalendarBundle\Tests\Unit\Fixtures\Entity\Attendee;
use Oro\Bundle\CalendarBundle\Tests\Unit\Fixtures\Entity\User;
use Oro\Bundle\CalendarBundle\Manager\AttendeeManager;
use Oro\Bundle\EntityExtendBundle\Tests\Unit\Fixtures\TestEnumValue;
use Oro\Bundle\SearchBundle\Engine\Indexer;
use Oro\Bundle\SearchBundle\Query\Query;
use Oro\Bundle\SearchBundle\Query\Result;
use Oro\Bundle\SearchBundle\Query\Result\Item;

class AttendeeSearchHandlerTest extends \PHPUnit_Framework_TestCase
{
    /** @var \PHPUnit_Framework_MockObject_MockObject|Indexer */
    protected $indexer;

    /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager */
    protected $om;

    /** @var EntityRepository */
    protected $entityRepository;

    /** @var \PHPUnit_Framework_MockObject_MockObject|AttendeeManager */
    protected $attendeeManager;

    /** @var AttendeeSearchHandler */
    protected $attendeeSearchHandler;

    public function setUp()
    {
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
        $translator->expects($this->any())
            ->method('trans')
            ->will(
                $this->returnCallback(
                    function ($id) {
                        return $id;
                    }
                )
            );

        $this->indexer = $this->getMockBuilder('Oro\Bundle\SearchBundle\Engine\Indexer')
            ->disableOriginalConstructor()
            ->getMock();

        $activityManager = $this->getMockBuilder('Oro\Bundle\ActivityBundle\Manager\ActivityManager')
            ->disableOriginalConstructor()
            ->getMock();

        $configManager = $this->getMockBuilder('Oro\Bundle\EntityConfigBundle\Config\ConfigManager')
            ->disableOriginalConstructor()
            ->getMock();

        $entityClassNameHelper = $this->getMockBuilder('Oro\Bundle\EntityBundle\Tools\EntityClassNameHelper')
            ->disableOriginalConstructor()
            ->getMock();

        $this->om = $this->createMock('Doctrine\Common\Persistence\ObjectManager');

        $nameResolver = $this->getMockBuilder('Oro\Bundle\EntityBundle\Provider\EntityNameResolver')
            ->disableOriginalConstructor()
            ->getMock();

        $dispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');

        $this->attendeeManager = $this
            ->getMockBuilder('Oro\Bundle\CalendarBundle\Manager\AttendeeManager')
            ->disableOriginalConstructor()
            ->getMock();

        $this->entityRepository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
            ->setMethods(['findById'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->om->expects($this->any())
            ->method('getRepository')
            ->with('entity')
            ->will($this->returnValue($this->entityRepository));

        $this->attendeeSearchHandler = new AttendeeSearchHandler(
            new TokenStorage(),
            $translator,
            $this->indexer,
            $activityManager,
            $configManager,
            $entityClassNameHelper,
            $this->om,
            $nameResolver,
            $dispatcher
        );

        $this->attendeeSearchHandler->setAttendeeManager($this->attendeeManager);
    }

    public function testSearch()
    {
        $items = [
            new Item('entity', 1, 'record1'),
            new Item('entity', 2, 'record2'),
        ];

        $users = [
            (new User(1))
                ->setEmail('<EMAIL>')
                ->setFirstName('user1'),
            (new User(2))
                ->setEmail('<EMAIL>')
                ->setFirstName('user2'),
        ];

        $this->indexer->expects($this->once())
            ->method('simpleSearch')
            ->with('query', 0, 101, ['oro_user'], 1)
            ->will($this->returnValue(new Result(new Query(), $items)));

        $this->entityRepository->expects($this->once())
            ->method('findById')
            ->with([1, 2])
            ->will($this->returnValue($users));

        $this->attendeeManager->expects($this->exactly(2))
            ->method('createAttendee')
            ->withConsecutive(
                [$users[0]],
                [$users[1]]
            )
            ->will(
                $this->returnCallback(
                    function (User $user) {
                        return (new Attendee)
                            ->setUser($user)
                            ->setDisplayName($user->getFirstName())
                            ->setEmail($user->getEmail())
                            ->setStatus(new TestEnumValue('test', 'test'))
                            ->setType(new TestEnumValue('test', 'test'));
                    }
                )
            );

        $result = $this->attendeeSearchHandler->search('query', 1, 100);

        $this->assertEquals(
            [
                'results' => [
                    [
                        'id'          => json_encode(
                            [
                                'entityClass' => 'Oro\Bundle\CalendarBundle\Tests\Unit\Fixtures\Entity\User',
                                'entityId'    => 1,
                            ]
                        ),
                        'text'        => 'user1',
                        'displayName' => 'user1',
                        'email'       => '<EMAIL>',
                        'status'      => 'test',
                        'type'        => 'test',
                        'userId'      => 1,
                    ],
                    [
                        'id'          => json_encode(
                            [
                                'entityClass' => 'Oro\Bundle\CalendarBundle\Tests\Unit\Fixtures\Entity\User',
                                'entityId'    => 2,
                            ]
                        ),
                        'text'        => 'user2',
                        'displayName' => 'user2',
                        'email'       => '<EMAIL>',
                        'status'      => 'test',
                        'type'        => 'test',
                        'userId'      => 2,
                    ],
                ],
                'more'    => false,
            ],
            $result
        );
    }
}

{'fraction_non_alphanumeric': 0.13170767862437, 'fraction_numerical': 0.005114141713608064, 'mean_word_length': 1.533420953811491, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 219, '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': '2976047', 'n_tokens_mistral': 3524, 'n_tokens_neox': 3358, 'n_words': 450}