<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\AdminBundle\Tests\Admin;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Admin\AdminHelper;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
use Sonata\AdminBundle\Tests\Fixtures\Entity\Bar;
use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PropertyAccess\PropertyAccess;

final class AdminHelperTest extends TestCase
{
    protected AdminHelper $helper;

    protected function setUp(): void
    {
        $this->helper = new AdminHelper(PropertyAccess::createPropertyAccessor());
    }

    public function testGetChildFormBuilder(): void
    {
        $formFactory = $this->createStub(FormFactoryInterface::class);
        $eventDispatcher = $this->createStub(EventDispatcherInterface::class);

        $formBuilder = new FormBuilder('test', \stdClass::class, $eventDispatcher, $formFactory);

        $childFormBuilder = new FormBuilder('elementId', \stdClass::class, $eventDispatcher, $formFactory);
        $formBuilder->add($childFormBuilder);

        static::assertNull($this->helper->getChildFormBuilder($formBuilder, 'foo'));
        static::assertInstanceOf(FormBuilder::class, $this->helper->getChildFormBuilder($formBuilder, 'test_elementId'));
    }

    public function testGetGrandChildFormBuilder(): void
    {
        $formFactory = $this->createStub(FormFactoryInterface::class);
        $eventDispatcher = $this->createStub(EventDispatcherInterface::class);

        $formBuilder = new FormBuilder('parent', \stdClass::class, $eventDispatcher, $formFactory);
        $childFormBuilder = new FormBuilder('child', \stdClass::class, $eventDispatcher, $formFactory);
        $grandchildFormBuilder = new FormBuilder('grandchild', \stdClass::class, $eventDispatcher, $formFactory);

        $formBuilder->add($childFormBuilder);
        $childFormBuilder->add($grandchildFormBuilder);

        static::assertInstanceOf(FormBuilder::class, $this->helper->getChildFormBuilder($formBuilder, 'parent_child_grandchild'));
    }

    public function testGetChildFormView(): void
    {
        $formView = new FormView();
        $formView->vars['id'] = 'test';
        $child = new FormView($formView);
        $formView->children['child'] = $child;
        $child->vars['id'] = 'test_elementId';

        static::assertNull($this->helper->getChildFormView($formView, 'foo'));
        static::assertInstanceOf(FormView::class, $this->helper->getChildFormView($formView, 'test_elementId'));
    }

    public function testGetElementAccessPath(): void
    {
        $object = $this->getMockBuilder(\stdClass::class)
            ->addMethods(['getPathToObject'])
            ->getMock();
        $subObject = $this->getMockBuilder(\stdClass::class)
            ->addMethods(['getAnother'])
            ->getMock();
        $sub2Object = $this->getMockBuilder(\stdClass::class)
            ->addMethods(['getMoreThings'])
            ->getMock();

        $object->expects(static::atLeastOnce())->method('getPathToObject')->willReturn([$subObject]);
        $subObject->expects(static::atLeastOnce())->method('getAnother')->willReturn($sub2Object);
        $sub2Object->expects(static::atLeastOnce())->method('getMoreThings')->willReturn('Value');

        $path = $this->getMethodAsPublic('getElementAccessPath')->invoke(
            $this->helper,
            'uniquePartOfId_path_to_object_0_another_more_things',
            $object
        );

        static::assertSame('path_to_object[0].another.more_things', $path);
    }

    public function testItThrowsExceptionWhenDoesNotFindTheFullPath(): void
    {
        $path = 'uniquePartOfId_path_to_object_0_more_calls';
        $object = $this->getMockBuilder(\stdClass::class)
            ->addMethods(['getPathToObject'])
            ->getMock();
        $subObject = $this->getMockBuilder(\stdClass::class)
            ->addMethods(['getMore'])
            ->getMock();

        $object->expects(static::atLeastOnce())->method('getPathToObject')->willReturn([$subObject]);
        $subObject->expects(static::atLeastOnce())->method('getMore')->willReturn('Value');

        $this->expectException(\Exception::class);
        $this->expectExceptionMessage(sprintf('Could not get element id from %s Failing part: calls', $path));

        $this->getMethodAsPublic('getElementAccessPath')->invoke(
            $this->helper,
            $path,
            $object
        );
    }

    public function testAppendFormFieldElement(): void
    {
        $admin = $this->createMock(AdminInterface::class);
        $admin
            ->method('getClass')
            ->willReturn(Foo::class);

        $associationAdmin = $this->createMock(AdminInterface::class);
        $associationAdmin
            ->method('getClass')
            ->willReturn(Bar::class);

        $associationMapping = [
            'fieldName' => 'bar',
            'targetEntity' => Foo::class,
            'sourceEntity' => Foo::class,
            'isOwningSide' => false,
        ];

        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
        $fieldDescription->method('getName')->willReturn('bar');
        $fieldDescription->method('getAssociationAdmin')->willReturn($associationAdmin);
        $fieldDescription->method('getAssociationMapping')->willReturn($associationMapping);
        $fieldDescription->method('getParentAssociationMappings')->willReturn([]);
        $fieldDescription->method('hasAdmin')->willReturn(true);
        $fieldDescription->method('getAdmin')->willReturn($admin);

        $admin->method('hasFormFieldDescription')->with('bar')->willReturn(true);
        $admin->method('getFormFieldDescription')->with('bar')->willReturn($fieldDescription);
<commit_msg>Minor change in test<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\AdminBundle\Tests\Admin;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Admin\AdminHelper;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
use Sonata\AdminBundle\Tests\Fixtures\Entity\Bar;
use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PropertyAccess\PropertyAccess;

final class AdminHelperTest extends TestCase
{
    protected AdminHelper $helper;

    protected function setUp(): void
    {
        $this->helper = new AdminHelper(PropertyAccess::createPropertyAccessor());
    }

    public function testGetChildFormBuilder(): void
    {
        $formFactory = $this->createStub(FormFactoryInterface::class);
        $eventDispatcher = $this->createStub(EventDispatcherInterface::class);

        $formBuilder = new FormBuilder('test', \stdClass::class, $eventDispatcher, $formFactory);

        $childFormBuilder = new FormBuilder('elementId', \stdClass::class, $eventDispatcher, $formFactory);
        $formBuilder->add($childFormBuilder);

        static::assertNull($this->helper->getChildFormBuilder($formBuilder, 'foo'));
        static::assertInstanceOf(FormBuilder::class, $this->helper->getChildFormBuilder($formBuilder, 'test_elementId'));
    }

    public function testGetGrandChildFormBuilder(): void
    {
        $formFactory = $this->createStub(FormFactoryInterface::class);
        $eventDispatcher = $this->createStub(EventDispatcherInterface::class);

        $formBuilder = new FormBuilder('parent', \stdClass::class, $eventDispatcher, $formFactory);
        $childFormBuilder = new FormBuilder('child', \stdClass::class, $eventDispatcher, $formFactory);
        $grandchildFormBuilder = new FormBuilder('grandchild', \stdClass::class, $eventDispatcher, $formFactory);

        $formBuilder->add($childFormBuilder);
        $childFormBuilder->add($grandchildFormBuilder);

        static::assertInstanceOf(FormBuilder::class, $this->helper->getChildFormBuilder($formBuilder, 'parent_child_grandchild'));
    }

    public function testGetChildFormView(): void
    {
        $formView = new FormView();
        $formView->vars['id'] = 'test';
        $child = new FormView($formView);
        $formView->children['child'] = $child;
        $child->vars['id'] = 'test_elementId';

        static::assertNull($this->helper->getChildFormView($formView, 'foo'));
        static::assertInstanceOf(FormView::class, $this->helper->getChildFormView($formView, 'test_elementId'));
    }

    public function testGetElementAccessPath(): void
    {
        $object = $this->getMockBuilder(\stdClass::class)
            ->addMethods(['getPathToObject'])
            ->getMock();
        $subObject = $this->getMockBuilder(\stdClass::class)
            ->addMethods(['getAnother'])
            ->getMock();
        $sub2Object = $this->getMockBuilder(\stdClass::class)
            ->addMethods(['getMoreThings'])
            ->getMock();

        $object->expects(static::atLeastOnce())->method('getPathToObject')->willReturn([$subObject]);
        $subObject->expects(static::atLeastOnce())->method('getAnother')->willReturn($sub2Object);
        $sub2Object->expects(static::atLeastOnce())->method('getMoreThings')->willReturn('Value');

        $path = $this->getMethodAsPublic('getElementAccessPath')->invoke(
            $this->helper,
            'uniquePartOfId_path_to_object_0_another_more_things',
            $object
        );

        static::assertSame('path_to_object[0].another.more_things', $path);
    }

    public function testItThrowsExceptionWhenDoesNotFindTheFullPath(): void
    {
        $path = 'uniquePartOfId_path_to_object_0_more_calls';
        $object = $this->getMockBuilder(\stdClass::class)
            ->addMethods(['getPathToObject'])
            ->getMock();
        $subObject = $this->getMockBuilder(\stdClass::class)
            ->addMethods(['getMore'])
            ->getMock();

        $object->expects(static::atLeastOnce())->method('getPathToObject')->willReturn([$subObject]);
        $subObject->expects(static::atLeastOnce())->method('getMore')->willReturn('Value');

        $this->expectException(\Exception::class);
        $this->expectExceptionMessage(sprintf('Could not get element id from %s Failing part: calls', $path));

        $this->getMethodAsPublic('getElementAccessPath')->invoke(
            $this->helper,
            $path,
            $object
        );
    }

    public function testAppendFormFieldElement(): void
    {
        $admin = $this->createMock(AdminInterface::class);
        $admin
            ->method('getClass')
            ->willReturn(Foo::class);

        $associationAdmin = $this->createMock(AdminInterface::class);
        $associationAdmin
            ->method('getClass')
            ->willReturn(Bar::class);

        $associationMapping = [
            'fieldName' => 'bar',
            'targetEntity' => Bar::class,
            'sourceEntity' => Foo::class,
            'isOwningSide' => false,
        ];

        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
        $fieldDescription->method('getName')->willReturn('bar');
        $fieldDescription->method('getAssociationAdmin')->willReturn($associationAdmin);
        $fieldDescription->method('getAssociationMapping')->willReturn($associationMapping);
        $fieldDescription->method('getParentAssociationMappings')->willReturn([]);
        $fieldDescription->method('hasAdmin')->willReturn(true);
        $fieldDescription->method('getAdmin')->willReturn($admin);

        $admin->method('hasFormFieldDescription')->with('bar')->willReturn(true);
        $admin->method('getFormFieldDescription')->with('bar')->willReturn($fieldDescription);

{'fraction_non_alphanumeric': 0.14336972287960914, 'fraction_numerical': 0.001068783876631804, 'mean_word_length': 3.924812030075188, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 185, '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': '4129059', 'n_tokens_mistral': 3562, 'n_tokens_neox': 3476, 'n_words': 509}