1

I have a created a simple doctrine2/zend skeleton project and am trying to get unit testing working with zend studio.

The tests work perfectly through the PHPunit CLI but I just can't get them to work in zend studio.

It comes up with an error saying : 'No Tests was executed' and the following output in the debug window :

X-Powered-By: PHP/5.2.14 ZendServer/5.0
Set-Cookie: ZendDebuggerCookie=127.0.0.1%3A10137%3A0||084|77742D65|1016; path=/
Content-type: text/html

<br />
<b>Warning</b>:  Unexpected character in input:  '\' (ASCII=92) state=1 in <b>/var/www/z2d2/tests/application/models/UserModelTest.php</b> on line <b>8</b><br />
<br />
<b>Warning</b>:  Unexpected character in input:  '\' (ASCII=92) state=1 in <b>/var/www/z2d2/tests/application/models/UserModelTest.php</b> on line <b>8</b><br />
<br />
<b>Parse error</b>:  syntax error, unexpected T_STRING in <b>/var/www/z2d2/tests/application/models/UserModelTest.php</b> on line <b>8</b><br />

The test is as follows:

<?php
require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{

    public function setUp()
    {

      $this->bootstrap = new Zend_Application(
        'testing',
        APPLICATION_PATH . '/configs/application.ini'
      );



        parent::setUp();
    }

    public function tearDown()
    {
      parent::tearDown();
    }

}



<?php

class IndexControllerTest extends ControllerTestCase
{

    public function testDoesHomePageExist() 
    {
        $this->dispatch('/');
        $this->assertController('index');
        $this->assertAction('index');

    }   

}



<?php

class ModelTestCase extends PHPUnit_Framework_TestCase
{

  protected $em;

  public function setUp()
  {

    $application = new Zend_Application(
      'testing', 
      APPLICATION_PATH . '/configs/application.ini'
    );

    $bootstrap = $application->bootstrap()->getBootstrap();

    $this->em = $bootstrap->getResource('entityManager'); 

    parent::setUp();

  }

  public function tearDown()
  {
    parent::tearDown();
  }

}


<?php

class UserModelTest extends ModelTestCase
{

  public function testCanInstantiateUser()
  {
    $this->assertInstanceOf('\Entities\User', new \Entities\User);
  }

  public function testCanSaveAndRetrieveUser()
  {

    $user = new \Entities\User;

    $user->setFirstname('wjgilmore-test');
    $user->setemail('example@wjgilmore.com');
    $user->setpassword('jason');
    $user->setAddress1('calle san antonio');
    $user->setAddress2('albayzin');
    $user->setSurname('testman');
    $user->setConfirmed(TRUE);


    $this->em->persist($user);
    $this->em->flush();

    $user = $this->em->getRepository('Entities\User')->findOneByFirstname('wjgilmore-test');

    $this->assertEquals('wjgilmore-test', $user->getFirstname());



  }


  public function testCanDeleteUser()
  {
    $user = new \Entities\User;
    $user = $this->em->getRepository('Entities\User')->findOneByFirstname('wjgilmore-test');

    $this->em->remove($user);
    $this->em->flush();


  }





}

And the bootstrap:

<?php

define('BASE_PATH', realpath(dirname(__FILE__) . '/../../'));

define('APPLICATION_PATH', BASE_PATH . '/application');

set_include_path(
    '.'
    . PATH_SEPARATOR . BASE_PATH . '/library'
    . PATH_SEPARATOR . get_include_path()
);


require_once 'controllers/ControllerTestCase.php';
require_once 'models/ModelTestCase.php';

Here is the new error after setting PHP Executable to 5.3 as Gordon suggested:

X-Powered-By: PHP/5.3.3 ZendServer/5.0
Set-Cookie: ZendDebuggerCookie=127.0.0.1%3A10137%3A0||084|77742D65|1000; path=/
Content-type: text/html

<br />
<b>Fatal error</b>:  Class 'ModelTestCase' not found in <b>/var/www/z2d2/tests/application/models/UserModelTest.php</b> on line <b>4</b><br />
edorian
  • 38,542
  • 15
  • 125
  • 143
dimbo
  • 817
  • 1
  • 11
  • 25
  • Given that it says unexpected T_STRING in /var/www/z2d2/tests/application/models/UserModelTest.php it would be more interesting to see that file. – Gordon Nov 15 '11 at 16:47
  • sorry, I'm not sure what you mean. – dimbo Nov 15 '11 at 16:50
  • 2
    `Warning: Unexpected character in input: '\' (ASCII=92) state=1 in ...` looks like php 5.3 code (namespaces) running with a PHP 5.2 – edorian Nov 15 '11 at 17:06
  • 1
    Check Windows > Preferences > PHP > PHP Executables. Is a 5.3.x enabled? – Gordon Nov 15 '11 at 17:07
  • Quite possibly. I'm new to this. I have project....properties.....PHP interpreter set to 5.3 but maybe it is something along those lines – dimbo Nov 15 '11 at 17:20
  • You were right Gordon. I have now set 5.3 as default in PHP Executables and receive a different error which I will append to the bottom of my original post. – dimbo Nov 15 '11 at 17:44
  • 1
    Did you run the test with "Run As Script" or "Run as PHPUnit Test"? Also, is PHPUnit configured on your include path? Right click your project folder, then Configure Include Path. If PHPUnit is not listed: Add Library and pick PHPUnit. And finally make sure you have an autoloader in place for your own files. – Gordon Nov 15 '11 at 17:54
  • I run it as PHPunit test. Yes, PHPunit is configured on my include path. My main application bootstrap sorts out all the autoloading. Should I have some other autoloader in my PHPunit bootstrap? – dimbo Nov 15 '11 at 20:58

1 Answers1

0

The first problem was that the IDE was set to use PHP 5.2 but the cod was PHP 5.3.

Unexpected character in input:  '\' (ASCII=92) state=1

usually hints at that problem.

After fixing that the other error is that a class can not be found. Thats because PHP can't find the needed class. Chances are that the Zend Framework autoloader was not set up as it should be.

If that happens while testing make sure your phpunit.xml contains a <phpunit bootstrap="yourApplicationBootstrap.php" ... entry where your autoloader is initialized.

How that works out is documented in the ZF docs and nothing phpunit can work out for you :)

edorian
  • 38,542
  • 15
  • 125
  • 143
  • Thanks. Why does PHPunit work properly outside of Zend Studio. I think I have it set up as you suggested: my phpunit.xml points to my phpunit bootstrap which requires the missing ModelTestCase. Am I missing something really basic here? – dimbo Nov 15 '11 at 18:20
  • @dimbo I've got no clue if Zend Studio does care about a phpunit configuration file at all. The issue i could see and solve was the PHP version but of the extra question you might want to open another question or look in the Zend Forums.. maybe you need to specific your bootstrap file again in the Zend Studio config somewhere? I'm sorry i can only help you getting PHPUnit to run and not the IDE in detail as i don't care for it all that much and don't use it :) – edorian Nov 15 '11 at 18:29
  • Thanks edorian and Gordon. You're right, the initial question was solved. I'll open another question re: the bootstrap issue. – dimbo Nov 15 '11 at 21:03
  • Just solved the other issue as well - If you want ZS to run the PHPunit bootstrap, you have to specifically select the file PHPunit.xml and tell it to run as PHPunit test. If you just select an individual test and run as PHPunit test, the bootstrap will not be run....or so it seems to me! – dimbo Nov 15 '11 at 21:51