Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Testings Types from the Service Container doesn't find dependencies #36462

Closed
@rgrassian

Description

@rgrassian

Symfony version(s) affected: 4.4.7

Description
In my case I can't test a type with a dependency, I get the following error :

ArgumentCountError: Too few arguments to function Symfony\Bridge\Doctrine\Form\Type\DoctrineType::__construct()

How to reproduce

class ContractCommercialGestureNewTypeTest extends TypeTestCase
{
    /**
     * @var CommercialGestureRepository|MockObject
     */
    protected $commercialGestureRepository;

    /**
     * @return void
     */
    protected function setUp(): void
    {
        $this->commercialGestureRepository = $this->createMock(CommercialGestureRepository::class);

        parent::setUp();
    }

    /**
     * @return void
     */
    public function testSubmitForm(): void
    {
        $contractCommercialGestureToCompare = new ContractCommercialGesture();
        $form = $this->factory->create(ContractCommercialGestureNewType::class, $contractCommercialGestureToCompare);
    }

    /**
     * @return array<FormExtensionInterface>
     */
    protected function getExtensions(): array
    {
        $type = new ContractCommercialGestureNewType($this->commercialGestureRepository);

        return [
            new PreloadedExtension([$type], []),
        ];
    }
}

Possible Solution

Additional context
The construct of my type :

    public function __construct(CommercialGestureRepository $commercialGestureRepository)
    {
        $this->commercialGestureRepository = $commercialGestureRepository;
    }

The link of the documentation :
https://symfony.com/doc/current/form/unit_testing.html#testings-types-from-the-service-container

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions