Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use existing entities with DataFactory #4

Open
CJDennis opened this issue Dec 5, 2019 · 2 comments
Open

Can't use existing entities with DataFactory #4

CJDennis opened this issue Dec 5, 2019 · 2 comments

Comments

@CJDennis
Copy link

CJDennis commented Dec 5, 2019

What are you trying to achieve?

According to https://codeception.com/docs/modules/DataFactory.html you can create a new entity which is related to an existing entity. I can't get this functionality to work.

What do you get instead?

  [Doctrine\ORM\ORMInvalidArgumentException] A new entity was found through the relationship 'HMRX\CoreBundle\Entity\AccountHolder#accountRole' that was not configured to cascade persist operations for entity: HMRX\CoreBundle\Entity\AccountRole@000000004c9582ff000000009565e70e. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist  this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'HMRX\CoreBundle\Entity\AccountRole#__toString()' to get a clue.

Provide test source code if related

  public function shouldUseAFakeAccountHolder(AcceptanceTester $I) {
    $I->have(AccountHolder::class);
  }
class Factories extends \Codeception\Module {
  public function _beforeSuite($settings = []) {
    /** @var EntityManager $em */
    $em = $this->getModule('Doctrine2')->_getEntityManager();

    $factory = $this->getModule('DataFactory');

    $factory->_define(AccountHolder::class, [
      'accountRole' => $em->getRepository(AccountRole::class)->findOneBy(['name' => 'ROLE_PHARMACIST']),
    ]);
  }
}

Details

# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.

actor: AcceptanceTester
modules:
  enabled:
    - WebDriver:
        url: 'https://localhost/'
        browser: chrome # 'chrome' or 'firefox'
        capabilities:
          chromeOptions:
            w3c: false
    - \Helper\Acceptance
    - Doctrine2:
        depends: Symfony
    - DataFactory:
        depends: Doctrine2
        factories: tests/_support/Factories
    - \Helper\Factories
@Liiva
Copy link

Liiva commented Jun 9, 2020

I don't know why exactly but wrapping the $em->getRepository(...) call in a closure seems to fix this issue for me, e.g.:

'accountRole' => function() use ($em) {
    return $em->getRepository(AccountRole::class)->findOneBy(['name' => 'ROLE_PHARMACIST']);
}

I'm guessing the closure is executed at a different point in the execution flow.

@raph007
Copy link

raph007 commented Dec 2, 2020

@Liiva Thank you! You saved me so much time. :) I've been struggling with this for over 2 hours.

@DavertMik Maybe it is good idea to add this to docs?

@Naktibalda Naktibalda transferred this issue from Codeception/Codeception Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants