无法让 Zend Studio 和 PHPunit 一起工作

发布于 2024-12-15 20:45:40 字数 4171 浏览 3 评论 0 原文

我创建了一个简单的doctrine2/zend 骨架项目,并尝试使用 zend studio 进行单元测试。

这些测试可以通过 PHPunit CLI 完美运行,但我无法让它们在 zend studio 中运行。

它出现一个错误:“没有执行测试”,调试窗口中输出以下内容:

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 />

测试如下:

<?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('[email protected]');
    $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();


  }





}

引导程序:

<?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';

这是按照 Gordon 建议将 PHP Executable 设置为 5.3 后出现的新错误:

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 />

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('[email protected]');
    $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 />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初吻给了烟 2024-12-22 20:45:40

第一个问题是 IDE 设置为使用 PHP 5.2,但 cod 是 PHP 5.3。

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

通常会暗示这个问题。

修复后另一个错误是找不到类。那是因为 PHP 找不到所需的类。 Zend Framework 自动加载器可能未按应有的方式设置。

如果在测试时发生这种情况,请确保您的 phpunit.xml 包含初始化自动加载器的 条目。

ZF 文档中记录了它的工作原理,而 phpunit 无法为您解决问题:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文