We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

PhpUnit and SetUp function

How i should declare function setUp in my test, because when i use this

class IndexControllerTest extends Phalcon\Test\UnitTestCase {
    protected $_account;

    public function setUp(){
        $this->_account = new IndexController();
    }

    public function testBalanceIsZeroWhenOpenAccount(){
        $balance = $this->_account->getBalance();

        $this->assertEquals(0,$balance,'Balance not zero !');
    }
}

I recive this: Declaration of IndexControllerTest::setUp() should be compatible with Phalcon\Test\UnitTestCase::setUp

I want to tets one of my controller i use this PHPunit xml config:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./TestHelper.php"
         backupGlobals="false"
         backupStaticAttributes="false"
         verbose="true"
         colors="false"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="true">
    <testsuite name="Phalcon - Testsuite">
        <directory>./</directory>
    </testsuite>
</phpunit>


4.0k
edited Jul '15

Try:

class IndexControllerTest extends Phalcon\Test\UnitTestCase {
    protected $_account;

    public function setUp(Phalcon\DiInterface $di = NULL, Phalcon\Config $config = NULL){
        $this->_account = new IndexController();
    }

    public function testBalanceIsZeroWhenOpenAccount(){
        $balance = $this->_account->getBalance();

        $this->assertEquals(0,$balance,'Balance not zero !');
    }
}

Manual



8.7k

Nope.

Now i recive this:

Fatal error: Class name must be a valid object or a string in C:\server\www\phalcon\vendor\phalcon\incubator\Library\Phalcon\Test\UnitTestCase.php on line 167


4.0k

Try:

class IndexControllerTest extends Phalcon\Test\UnitTestCase {
    protected $_account;

    public function setUp(Phalcon\DiInterface $di = NULL, Phalcon\Config $config = NULL){

        // Load any additional services that might be required during testing
        $di = DI::getDefault();

        // get any DI components here. If you have a config, be sure to pass it to the parent
        parent::setUp($di);

        $this->_loaded = true;

        $this->_account = new IndexController();
    }

    public function testBalanceIsZeroWhenOpenAccount(){
        $balance = $this->_account->getBalance();

        $this->assertEquals(0,$balance,'Balance not zero !');
    }
}