Note that there are some explanatory texts on larger screens.

plurals
  1. PO$this->assertEquals error : Failed asserting that null matches expected
    primarykey
    data
    text
    <p>I'm doing test unit of my entity: </p> <pre class="lang-php prettyprint-override"><code>namespace PathtomyBundle\Tests; require_once dirname(__DIR__).'/../../../app/AppKernel.php'; use Doctrine\ORM\Tools\SchemaTool; abstract class TestCase extends \PHPUnit_Framework_TestCase { /** * @var Symfony\Component\HttpKernel\AppKernel */ protected $kernel; /** * @var Doctrine\ORM\EntityManager */ protected $entityManager; /** * @var Symfony\Component\DependencyInjection\Container */ protected $container; public function setUp() { // Boot the AppKernel in the test environment and with the debug. $this-&gt;kernel = new \AppKernel('test', true); $this-&gt;kernel-&gt;boot(); // Store the container and the entity manager in test case properties $this-&gt;container = $this-&gt;kernel-&gt;getContainer(); $this-&gt;entityManager = $this-&gt;container-&gt;get('doctrine')-&gt;getEntityManager(); // Build the schema for sqlite //$this-&gt;generateSchema(); parent::setUp(); } public function tearDown() { // Shutdown the kernel. $this-&gt;kernel-&gt;shutdown(); parent::tearDown(); } protected function generateSchema() { // Get the metadatas of the application to create the schema. $metadatas = $this-&gt;getMetadatas(); if ( ! empty($metadatas)) { // Create SchemaTool $tool = new SchemaTool($this-&gt;entityManager); $tool-&gt;createSchema($metadatas); } else { throw new Doctrine\DBAL\Schema\SchemaException('No Metadata Classes to process.'); } } /** * Overwrite this method to get specific metadatas. * * @return Array */ protected function getMetadatas() { return $this-&gt;entityManager-&gt;getMetadataFactory()-&gt;getAllMetadata(); } } </code></pre> <p>and also: </p> <pre class="lang-php prettyprint-override"><code> namespace pathtomybundle\Tests\Entity; use pathtomybundle\Tests\TestCase; use pathtomybundle\Entity\Calendars; require_once dirname(__DIR__).'/TestCase.php'; class CalendarsDbTest extends TestCase { protected $Calendars; public function setUp() { parent::setUp(); $this-&gt;Calendars = new Calendars(); } public function testGenerateCalendars() { $this-&gt;Calendars-&gt;setBeginDate(new \DateTime('now')); $this-&gt;Calendars-&gt;setDescription('Description'); $this-&gt;Calendars-&gt;setEndDate(new \DateTime('now')); $this-&gt;Calendars-&gt;setType('sur titre'); // Save the ExCalendars $this-&gt;entityManager-&gt;persist($this-&gt;Calendars); $this-&gt;entityManager-&gt;flush(); } public function testUser(){ $this-&gt;assertEquals('Description', $this-&gt;Calendars-&gt;getDescription() ); } </code></pre> <p>So my questions are: </p> <ul> <li><p>Why does it raise this error "Failed asserting that null matches expected"?</p></li> <li><p>Why <code>getDescription()</code> returns <code>NULL</code>?</p></li> <li><p>How to test two table with One-to-Many relationship for example my Table Calendars with another table in database?</p></li> </ul> <p><strong>Edit</strong></p> <p>For the third question :</p> <p>For example I have two Tables Job and Calenders with Many-to-One relationship so I will have a Job_Id field in Calendars Table,so how I will do my test Unit with a foreign key "job_id"</p> <p>In Calendars Entity : </p> <pre><code> /** * @var Job * * @ORM\ManyToOne(targetEntity="Job") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="job_id", referencedColumnName="job_id") * }) */ private $jobId; </code></pre> <p><strong>Edit-2-</strong></p> <p>when I run my phpunit test "phpunit -c app" to test setters function and persist in database so I have a with every test a new data insered in databse, my question is it possible to do a lot of test but I insert data in database just for one time because actually I must remove data from database with every test.</p> <p>2 - another question : to create a database_test i use "$this->generateSchema(); " so after create a database for the first time and when the test call "TestCase"class (the code above) again so he tried to create the database_test again then I must remove the line after the first time and it's not good,so what I can do to run this line just for one time in the first time when i run my test?</p> <p><strong>Edit-3</strong></p> <pre><code> /** * @var Job * * @ORM\ManyToOne(targetEntity="Job") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="job_id", referencedColumnName="id") * }) */ private $job; </code></pre> <p>it's normal?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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