1
votes

I'm working with an existing phpunit test suite, and trying to incorporate dbunit. In particular, I want to use the dataSet abstraction to load fixture data and clean up after me. I've added the PHPUnit_Extensions_Database_TestCase_Trait trait to the test case, and implemented functions getConnection and getDataSet. However, those methods only get called by the trait's default setUp and tearDown methods. Many of my tests have their own setUp and tearDown methods defined. Is there someplace different I should be putting this existing setUp/tearDown code so that I don't have to override setUp and tearDown from the trait? reference code:

  class FooTest extends \PHPUnit_Framework_TestCase {

    use PHPUnit_Extensions_Database_TestCase_Trait;

    static private $pdo = null;
    private $conn = null;

    public function testTrueIsTrue() {
      $foo = true;
      $this->assertTrue($foo);
    }

    public function setUp() {
      error_log("in setUp");
    }

    public function tearDown() {
      error_log("in tearDown");
    }

    public function getConnection() {
      error_log("in getConnection");
      return $this->createDefaultDBConnection();
    }

    /**
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
     */
    public function getDataSet() {
      error_log("in getDataSet");
      return new PHPUnit_Extensions_Database_DataSet_DefaultDataSet();
    }
  }
1
So run the parent method from the override method - RiggsFolly
parent::setUp(); as the first line of your setUp() method etc etc - RiggsFolly
I'm wanting to call the setUp and tearDown methods that were added by the trait, not those inherited from the parent. - Fred Willmore

1 Answers

2
votes

OK this turns out to be a specialized case of this:

How to override trait function and call it from the overridden function?

so, I've modified my use statement:

use PHPUnit_Extensions_Database_TestCase_Trait {
  setUp as protected defaultSetUp;
  tearDown as protected defaultTearDown;
}

and added the calls to the default methods:

public function setUp() {
  $this->defaultSetUp();
  error_log("in setUp");
}

public function tearDown() {
  error_log("in tearDown");
  $this->defaultTearDown();
}