phpphpunitmigrationphinx

Fatal error: Call to a member function run() on null in {path to the test file}


I am using PHPUnit 5.7.23 and phinx 0.9.1,

public function setUp(){
        $this->phinx = new PhinxApplication;
        $this->phinx->setAutoExit(false);
        $this->phinx->run(new StringInput('migrate'), new NullOutput);
        $this->phinx->run(new StringInput('seed:run'), new NullOutput);
    }


 public function tearDown(){

    $this->phinx->run(new StringInput('rollback -t 0'), new NullOutput);

}

This it the code of my test file , every time when i add

 public function testExampleFunction(){


        $this->assertTrue(true);
    }

it fails on the line :

$this->phinx->run(new StringInput('rollback -t 0'), new NullOutput);

with the errror :

Fatal error: Call to a member function run() on null in {path to the test file}

when i change this:

 public function tearDown()
    {

        if (!empty($this->phinx)) {
            $this->phinx->run(new StringInput('rollback -t 0'), new NullOutput);
        }

    } 

it passes but since there wasn't rollback, my next tests had crashed


Solution

  • Are you using namespacing (PSR-4) ? If so, you should have this line of code:

    use Phinx\Console\PhinxApplication;