The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

reset environment before each phpunit test executes

al.s's Avatar

al.s

17 Sep, 2015 09:00 AM

i wrote some tests using phpunit and each one ended with success after i run it, but when i tried running all of the tests together i got a failure. the problem was that each of my test classes had this method:

public function testGetDbh_dbhIsNull()
{
    $fixture = new testedClass();

    $app = new Slim();
    $DB = $this->getMockBuilder('DB')
               ->disableOriginalConstructor()
               ->getMock();
    $DB->method('getConnection')->willReturn('connection');
    $app->db = $DB;

    $this->assertEquals($fixture->getDbh(), 'connection');
}
as you can see i call getDbh in the last line and this function looks like this:

 function getDbh() {
    if ($this->dbh === null){
      $this->dbh = Slim::getInstance()->db->getConnection();
    }
    return $this->dbh;
  }
and the failure i get is this:

1) userssTest::testGetDbh_dbhIsNull
Failed asserting that 'connection' matches expected null.
2) classesTest::testGetDbh_dbhIsNull
Failed asserting that 'connection' matches expected null.
the failure appears for each class except for the first one.

i thought about using setUp or tearDown to destroy the Slim instance after each test ends/new test starts but i dont know whats the right way to do it...any help please?

  1. 1 Posted by John on 27 Sep, 2015 12:22 PM

    John's Avatar

    I haven't done this before, but there's a setting in PHPUnit where you can specify a bootstrap file to execute code before running all tests.

    http://php-and-symfony.matthiasnoback.nl/2011/10/symfony2-use-a-bootstrap-file-for-your-phpunit-tests-and-run-some-console-commands/

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac