Dataprovider can get connection from setUp

Failed to connect to database by setUp () error

class ChanTest extends PHPUnit_Framework_TestCase
{
    protected $db;

    protected function setUp()
    {
        $this->db = new Core\Database('unitest');
    }

    /**
     * @dataProvider testProvider
     */
    public function testData($a, $b, $c)
    {
        $this->assertEquals($a + $b, $c);
    }

    public function testProvider()
    {
        $this->db->query('SELECT `a`, `b`, `c` FROM `units`');

        return $this->db->rows();
    }
}

Connecting to a database by itself works

class ChanTest extends PHPUnit_Framework_TestCase
{
    protected $db;

    protected function setUp()
    {
        $this->db = new Core\Database('unitest');
    }

    public function testData($a, $b, $c)
    {
        $this->db->query('SELECT `a`, `b`, `c` FROM `units`');

        foreach ($this->db->rows() as $item) {
            $this->assertEquals($item['a'] + $item['b'], $item['c']);
        }
    }
}

If I connect the dataProvider database via setUp function, this is the answer Fatal error: Call to a member function query(), but if the connection to the database itself works, can I dataProviderget the parameter setUp function?

+4
source share
1 answer

This is by design: to determine the number of tests, PHPUnit runs dataProviders before actually running the tests (and the setUp method).

In the manual on DataProviders :

: setUpBeforeClass setUp. - , , . , PHPUnit .

singleton/instance .

+5

All Articles