Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8480223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:14:15+00:00 2026-06-10T19:14:15+00:00

I’m doing test unit of my entity: namespace PathtomyBundle\Tests; require_once dirname(__DIR__).’/../../../app/AppKernel.php’; use Doctrine\ORM\Tools\SchemaTool; abstract

  • 0

I’m doing test unit of my entity:

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->kernel = new \AppKernel('test', true);
    $this->kernel->boot();

    // Store the container and the entity manager in test case properties
    $this->container = $this->kernel->getContainer();
    $this->entityManager = $this->container->get('doctrine')->getEntityManager();

    // Build the schema for sqlite
    //$this->generateSchema();

    parent::setUp();
}

public function tearDown()
{
    // Shutdown the kernel.
    $this->kernel->shutdown();

    parent::tearDown();
}

protected function generateSchema()
{
    // Get the metadatas of the application to create the schema.
    $metadatas = $this->getMetadatas();

    if ( ! empty($metadatas)) {
        // Create SchemaTool
        $tool = new SchemaTool($this->entityManager);
        $tool->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->entityManager->getMetadataFactory()->getAllMetadata();
}
}

and also:

 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->Calendars = new Calendars();
        }


    public function testGenerateCalendars()
    {

        $this->Calendars->setBeginDate(new \DateTime('now'));
        $this->Calendars->setDescription('Description');
        $this->Calendars->setEndDate(new \DateTime('now'));
        $this->Calendars->setType('sur titre');

        // Save the ExCalendars
        $this->entityManager->persist($this->Calendars);
        $this->entityManager->flush();



    }

       public function testUser(){

     $this->assertEquals('Description', $this->Calendars->getDescription() );

    }

So my questions are:

  • Why does it raise this error “Failed asserting that null matches expected”?

  • Why getDescription() returns NULL?

  • How to test two table with One-to-Many relationship for example my Table Calendars with another table in database?

Edit

For the third question :

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”

In Calendars Entity :

 /**
 * @var Job
 *
 * @ORM\ManyToOne(targetEntity="Job")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="job_id", referencedColumnName="job_id")
 * })
 */
private $jobId;

Edit-2-

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.

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?

Edit-3

    /**
 * @var Job
 *
 * @ORM\ManyToOne(targetEntity="Job")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="job_id", referencedColumnName="id")
 * })
 */
private $job;

it’s normal?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:14:17+00:00Added an answer on June 10, 2026 at 7:14 pm
    1. Every test in test case creates his own CalendarsDbTest object. So, in fact, $this->Calendar is different object in each test (if you want share it between tests you need create it in setUp method)

    2. Is the same as above (there is null because you never call setDescription with $this->Calendars – it’s different object than it is in first test)

    3. I’m not sure what exactly you mean. Can you show more precise (for example method you want test) what you mean?

    edit:

    The answer is: you don’t test it. Why? Because unit test is UNIT test – you should test here only your entity. Persistence, keeping relations etc. are Doctrine resposibility and should be tested there – you don’t worry about it.

    The only thing you should test is setter/getter for $jobId property (btw. it should be “$job” rather than “$jobId” because it’s object of Job class – not an integer), eg.:

    class CalendarTest extends \PHPUnit_Framework_TestCase
    {
         (...)
    
         public function testSetGetJob()
         {
             $job = new Job();
             $job->setSomeProperty('someValue');  
    
             $expectedJob = clone $job; // you clone this because in setter you pass object by reference  
    
             $calendar = new Calendar();
             $calendar->setJob($job);
    
             $this->assertEquals($expectedJob, $calendar->getJob());
         }
    
         (...)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.