unit testing - PHPUnit 3.7.19 and Symfony2 broken tests -


i'm developing test symfony2.0 project , running them phpunit.

on pc works fine trying them in other environments tests fails. thought problem php version after run them in differents environments i'm lost.

  • my environment ubuntu 12.04 , php 5.3.10 => works fine.

  • 2 pc ubuntu 12.10 , php 5.4.6:

    fatal error:  call member function get() on non-object 

this error on class extends symfony\bundle\frameworkbundle\test\webtestcase overwritten setup() , teardown() methods.

public function setup() {     $this->client = static::createclient();     $this->client->followredirects(true);      $crawler = $this->client->request('get', '/admin/login');      $loginform = $crawler->selectbutton('save')->form(array(         '_username' => 'user',         '_password' => 'pass'         ));     $this->client->submit($loginform);      $this->container = $this->client->getcontainer();     parent::setup(); }  public function teardown() {     //here get() on non-object, $this->container doesn't exists     $this->container->get('doctrine.odm.mongodb.document_manager')->getconnection()->close();     parent::teardown(); } 
  • 2 pc, 1 ubuntu 12.10 , php 5.4.6 , other windows 7 , php 5.3.8:

    php fatal error:  call member function getsite() on non-object 

this error on class extends above class has teardown() method wrong in case class works , error different although related $this->container:

//in case, user doesn't exists $site = $this->container->get('security.context')         ->gettoken()->getuser()->getsite(); 

the problem don't know why this. if related php, phpunit(all of have same version), symfony2.0 or so.

edit:

ok, problems solved.

first:

fatal error:  call member function get() on non-object 

had wrong line of code in class has setup() , teardown() methods. line that:

$link = $crawler->filter('a:contains("test")')->eq(1)->link(); 

i had line commented, sorry :). don't know why phpunit show me error , not error of link method.

second:

php fatal error:  call member function getsite() on non-object 

in others environments, test database had not been deployed.

this question not me try new things. thanks!

it exected behavior phpunit.

i managed reproduce error following code:

final class exceptionerrortest extends phpunit_framework_testcase {     /**      * @var myobject      */     private $object;      public function setup() {         throw new \runtimeexception();         $this->object = new myobject();     }      public function testitshouldneverbecalled()     {         var_dump('never called');     }      public function teardown()     {         $this->object->nevercalled();     } }  class myobject {     public function nevercalled() {         return true;     } }  // ouput php fatal error:  call member function nevercalled() on non-object in exceptionerrortest.php on line 22 

to explain more clearly, phpunit catch exceptions triggered in setup method (in order printers show @ end of execution).

after can see teardown() called finish test, since initialization of attribute never reached, php error issued, , phpunit never reach code shows exceptions occurred.

that why did not exception. fix this, need make sure code can throw exceptions in setup wrapped in try() catch () statement this:

    public function setup() {         try {             throw new \runtimeexception('my message');         } catch (\exception $e) {             echo 'an error occured in setup: ' $e->getmessage();             die;         }          $this->object = new myobject();     } 

hope helps in future.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -