微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在表单上使用 Symfony4 进行 Functionnals 测试EntityType 输入不起作用

如何解决在表单上使用 Symfony4 进行 Functionnals 测试EntityType 输入不起作用

我有一个产品实体的表格

public function buildForm(FormBuilderInterface $builder,array $options)
{
    if ($options['edit'] === false) {
        $builder
            ->add('code',TextType::class,[
            'label' => 'Code',])
            ->add('name',[
            'label' => 'Nom',]);
    }

    $builder
        ->add('competence',EntityType::class,[
            'label' => 'Compétence','choice_label' => 'name','class' => Competence::class,'required' => false,]);
    }

它有效,但是当我想编写功能测试时它不起作用。当我的单元测试工作时。 为了测试,我有一个数据库和 database_test。为此,我使用 .env.local 和 .env.test.local 以及正确的 DATABASE_URL(主要和测试)。

public function testAdd()
{
    // Instance du client HTTP
    $client = static::createHttpbrowserClient();

    // Construction de la requête GET sur la page product
    $crawler = $client->request(
        'GET',getenv('APP_TEST_URL') . '/product/new'
        );
        
        $form = $crawler->selectButton('Ajouter')->form([
            'product[name]' => 'Product Test','product[code]' => 'ABC_DEF','product[competence]' => $this->entityManager->getRepository(\App\Entity\Competence::class)->findOneBy(['name' => 'BVB'])->getId(),]);

        $client->submit($form);

        $crawler = $client->followRedirect();

        $this->assertSame(1,$crawler->filter('div.alert.alert-success')->count());
    }

我如何设置:

    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $entityManager;

    public function setUp(): void
    {
        $kernel = self::bootKernel();

        $this->entityManager = $kernel->getContainer()
            ->get('doctrine')
            ->getManager();
    }

    public function tearDown(): void
    {
        $client = static::createHttpbrowserClient();
        $client->restart();
    }

我的问题是关于 EntityType Competence。我必须输入我想要的 ID,但对于 Database 和 Database_URL 中的相同能力,ID 不相同。

所以我收到这个错误

  1. App\Tests\E2e\Controller\ProductControllerTest::testAdd invalidargumentexception:输入“product[competence]”不能将“3”作为值(可能的值:“”、“258”、“259”、“260”、“261”、“262”、“263”、“264” ","265","266","267","268","269","270","271","272","273","274","275","276",“277”、“278”、“279”)。

例如,即使我输入“258”,也会出现以下错误: “LogicException:请求未被重定向。”在 followRedirect() 上。

我不知道我是否走对了路,但我现在迷路了。我尝试了很多使用 dotric.yaml 和 .env.*

的东西

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。