Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

[Doctrine] ManyToMany Relationships not persisted in database with haveInRepository nested entity creation

オープン
#16 コメント 7 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
php
領域
database, testing

調査の方向性

tests/functional/ManytoManyEntitiesCest.php と functional.suite.yml の Doctrine2 設定から始め、haveInRepository がネストされた entityB データをどのように処理するかを追跡します。報告されたデータベースの状態に照らして、想定される ManyToMany マッピングとアサーション構文を確認します。エンティティが作成され、結合テーブルにリレーションシップが含まれ、最後の seeInRepository アサーションが成功すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

What are you trying to achieve?

I am running a functional test on a Symfony 4 + Doctrine 2 with two entities with a ManyToMany relationship. I create the entities using haveInRepository nested functionality and I expect to have the two entities created on the database and related on the join table.

What do you get instead?

The entities are created but relationships in join tables are not, so the test fails due to missing information on database.

Provide console output if related. Use -vvv mode for more details.

vagrant@symfonyvm:/var/www/mtm$ php vendor/bin/codecept run
Codeception PHP Testing Framework v3.1.2
Powered by PHPUnit 8.5.0 by Sebastian Bergmann and contributors.
Running with seed: 


App\Tests.acceptance Tests (0) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

App\Tests.functional Tests (1) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Testing App\Tests.functional
✖ ManytoManyEntitiesCest: Try to test (0.07s)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

App\Tests.unit Tests (0) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Time: 326 ms, Memory: 28.25 MB

There was 1 failure:

---------
1) ManytoManyEntitiesCest: Try to test
 Test  tests/functional/ManytoManyEntitiesCest.php:tryToTest
 Step  See in repository "App\Entity\EntityA",{"entityB":{"name":"Name B"}}
 Fail  App\Entity\EntityA with {"entityB":{"name":"Name B"}}
Failed asserting that false is true.

Scenario Steps:

 4. $I->seeInRepository("App\Entity\EntityA",{"entityB":{"name":"Name B"}}) at tests/functional/ManytoManyEntitiesCest.php:24
 3. $I->seeInRepository("App\Entity\EntityA",{"name":"Name A"}) at tests/functional/ManytoManyEntitiesCest.php:23
 2. $I->seeInRepository("App\Entity\EntityB",{"name":"Name B"}) at tests/functional/ManytoManyEntitiesCest.php:22
 1. $I->haveInRepository("App\Entity\EntityA",{"name":"Name A","entityB":[{"name":"Name B"}]}) at tests/functional/ManytoManyEntitiesCest.php:19


FAILURES!
Tests: 1, Assertions: 3, Failures: 1.

Provide test source code if related

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\EntityARepository")
 */
class EntityA
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\EntityB", inversedBy="entityA")
     */
    private $entityB;
}
<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\EntityBRepository")
 */
class EntityB
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\EntityA", mappedBy="entityB")
     */
    private $entityA;
}
public function cannotSeeManyToManyRelationships(FunctionalTester $I)
{
    $I->haveInRepository(
        EntityA::class,
        [
            'name' => 'Name A',
            'entityB' =>[[ 'name' => 'Name B' ]],
        ]);
    $I->seeInRepository(EntityB::class, ['name' => 'Name B']);
    $I->seeInRepository(EntityA::class, ['name' => 'Name A']);
    $I->seeInRepository(EntityA::class, ['entityB' => ['name' => 'Name B']]);
}
Details

With that setup, the two first seeInRepository pass but the third does not. It could be due to the syntax of the array not being correct on the last assertion as it should really be an array, but if you look into the database you can see that the join table has not been filled.

It may be due a syntax problem, but in that case it should be an issue with docs as it doesn't specify how to work with ManyToMany relationships.

I have created a complete example in this repo: https://github.com/Pryrios/test-manytomany

  • Codeception version: Codeception PHP Testing Framework v3.1.2
  • PHP Version: 7.2
  • Operating System: Ubuntu 18.04
  • Installation type: Composer

functional.suite.yml

actor: FunctionalTester
modules:
    enabled:
        - Symfony:
            app_path: 'src'
            environment: 'test'
        - Doctrine2:
           depends: Symfony
           cleanup: true
        - \App\Tests\Helper\Functional
主要言語
PHP
スター
4
フォーク
2
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトの環境構築ファイルはまだ確認していません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Codeception/module-doctrine のほかの issue

Codeception/module-doctrine の issue をすべて見る

似ている issue

PHP の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。