Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctine2 Unittest mock up entity in relationship
    text
    copied!<p>I am trying to create unittest of a service class - class which has EntityManager injected and is responsible for loading and creating entities.</p> <p>The problem is in ::create(array) method for entities which creates entity and sets all required related entities. So I thought it would be advisable to use mock object for this, but when I use following code (also seen here: <a href="https://stackoverflow.com/questions/3575902/how-to-create-a-mock-object-of-a-doctrine-entity">How to create a mock object of a doctrine entity?</a>)</p> <pre><code>&lt;?php public function testCreate($email, $password) { $role = $this-&gt;getMock('Role'); $this-&gt;service-&gt;create( array('email' =&gt; $email, 'password' =&gt; $password, 'role' =&gt; $role)); } // service (simplified) public function create(array $values) { $user = new User(); $user-&gt;setEmail($values['email']) -&gt;setPassword($values['password']) -&gt;setRole($values['role']); $this-&gt;getDatabaseManager()-&gt;persist($user)-&gt;flush(); return $user; } ?&gt; </code></pre> <p>It triggers: <strong>A new entity was found through a relationship that was not configured to cascade persist operations</strong></p> <p>Using:</p> <pre><code>$this-&gt;service-&gt;getDatabaseManager()-&gt;persist($role); </code></pre> <p>in the test triggers: <strong>Class Mock_Role_c64eda12 is not a valid entity or mapped super class.</strong></p> <p>How can I either fix this or what better approach can I use? (don't know which one applies more here...if the whole 'service creates and persists entites' or whatever else is a bad idea please let me know)</p> <p>Thank you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload