phpunit db extensions not calling setup methods

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
MrJingles
Forum Newbie
Posts: 4
Joined: Sat Jun 25, 2011 8:57 am

phpunit db extensions not calling setup methods

Post by MrJingles »

Hi there

I'm pretty new to php testing involving databases, and I'm kind of stuck here. The code below should in my opinion prepare the given database in a certain state and then run the test, however it does not prepare the database, moreover the getDataset etc methods are never called, and I can't figure out why.

Here's the code:

Code: Select all

class PublicTest extends PHPUnit_Extensions_Database_TestCase
{
  protected $pdo;
  protected $config;
	
  protected $CUT;
	
  public function __construct() {
    $this->config = new TestConfigurator();
    $this->pdo = new PDO(	'mysql:host='.$this->config->getMYSQL('host').';dbname='.$this->config->getMYSQL('db'), 
                                        $this->config->getMYSQL('usr'), 
                                        $this->config->getMYSQL('pass'));
  }
	
  protected function getConnection()
  {
    echo "getConnection\n";
    return $this->createDefaultDBConnection($this->pdo, $this->config->getMYSQL('db'));
  }
	
  protected function getDataSet()
  {
    echo "getDataSet\n";
    return $this->createXMLDataSet(dirname(__FILE__).'/datasets/UpdateCustomerPasswordStatePre.xml');
  }
	
  protected function getSetUpOperation()
  {
    return $this->getOperations()->CLEAN_INSERT();        
  }
  
  public function testConstructor() {
    //do something, throws exception, if database is not in expected state
  }
	
  protected function setUp() {
    //...
  }
	
  protected function tearDown() {
    //...
  }
}
After running this in the console by > phpunit PublicTest.php, the database remains unchanged, and the test throws the exception I mention in the comment. The getDataSet and getConnection methods are never called.

I'd highly appreciate it if someone could point out my mistake !

Thanks a lot in advance
Cheers
Mischa
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: phpunit db extensions not calling setup methods

Post by Weirdan »

Database_TestCase calls those in setUp / tearDown, and since you overrided those methods they obviously stopped doing that. You need to call parent::setUp() / parent::tearDown() in the corresponding methods.
MrJingles
Forum Newbie
Posts: 4
Joined: Sat Jun 25, 2011 8:57 am

Re: phpunit db extensions not calling setup methods

Post by MrJingles »

Thank you so much! You just made my day :)
Post Reply