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

I cant use unit-test, "Cannot bind an instance to a static closure" happens.

I would like to unit-test some of my codes. Im using the newest Phpunit. My phpunit.xml:

<?php<phpunit  bootstrap="../bootstrap.php" backupGlobals="false" backupStaticAttributes="false" verbose="true" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="true">
  <testsuites>
    <testsuite name="sdf">
      <file>cxvcxvcxxcv.php</file>
    </testsuite>
  </testsuites>
</phpunit>

that bootstrap.php:

<?php
\Phalcon\Di::reset();
\Phalcon\Di::setDefault(new \Phalcon\DI\FactoryDefault());

\Phalcon\Di::getDefault()->setShared('t', function() {
  ...
});

and now if I would like to use this from a static method:

<?php
\Phalcon\Di::getDefault()->get('t');

it dies:

Cannot bind an instance to a static closure

what it can be?

its Php 5.6.16 and Phalcon 2.1.0b



9.3k
Accepted
answer
edited Mar '16

Try to work around this documentation

https://docs.phalcon.io/en/latest/reference/unit-testing.html

I am using unit tests with phalcon with my own UnitTestCase, which supports internal DI object. You can completely omit DI section in your bootstrap and use only DI object from UnitTestCase, set it as default and dynamicaly register services which are needed for particular unit test.



28.2k

yea, right, I didnt see the documentation havin unit-test chapter. I was using it as the old fashion way.