3

私はPHPプロジェクトに取り組んでおり、PHPUnitとDoctrine2を使用しています。最新バージョンを使用しています。私はすべてのクラス、関数、動作をテストするためにたくさんのテストを書きました。テストは常に実行されています。すべてがうまくいきました。Doctrineを使用しているので、ベストプラクティスのヒントを適用し、コンストラクターでArrayCollectionを初期化しました。

public function __construct($username, $eMail, $password1, $password2) {
    $this->quiz = new ArrayCollection();
    $this->sessions = new ArrayCollection();
    $this->setUsername($username);
    $this->setEMail($eMail);
    $this->setPassword('', $password1, $password2);
}

ArrayCollectionのインクルードは次のとおりです。

use Doctrine\Common\Collections\ArrayCollection;

この時点以降、PHPUnitテストは実行されなくなります。

初期化の行にコメントすると、すべてのテストが再度実行されます。メンバーのクイズとセッションは非公開です。アプリケーションは一般的に機能します。

Doctrine2とPHPUnitは初めてです。私はこれまで、テストケースなどのさまざまなインクルードのように多くのことを試してきました。しかし、何も役に立ちませんでした。たぶん私はテストケースで何かを忘れました。ArrayCollectionのステップ初期化と初期化なしの間で、テストケースで何も変更していません。

テストケースは次のようになります。

namespace Model;

require_once dirname(__FILE__) . '/../../Model/User.php';

/**
 * Test class for User.
 * Generated by PHPUnit on 2012-04-03 at 14:48:39.
 */
class UserTest extends \PHPUnit_Framework_TestCase {

    /**
     * @var User
     */
    protected $user;

    // constructor of the test suite
    function UserTest($name) {
        $this->PHPUnit_TestCase($name);
    }

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp() {
        $username = 'musteruser';
        $eMail = 'must@muster.ch';
        $pw = 'muster.muster';
        $this->user = new User($username, $eMail, $pw, $pw);
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown() {
        unset($this->user);
    }

今のところ、何が問題なのか本当にわかりません。たぶん、誰かがすでに同じことを経験したか、何が問題になる可能性があるかについて何か考えを持っています。

助けてくれてありがとう。

4

2 に答える 2

0

PHPUnitを呼び出すときに、Doctrine\Commonフォルダーがテストケースのインクルードパス上にあることを確認する必要がある場合があります。

テストでも同じことを行う必要があるため、アプリケーション用にクラスローダーをカスタマイズしていますか。

于 2012-04-10T08:15:17.907 に答える
0

私はその問題の解決策を見つけました。次の内容のbootstrap.phpという名前のファイルを作成しました。

// Import the ClassLoader
use Doctrine\Common\ClassLoader;

// Autoloader for Doctrine
require '/../../php/PEAR/Doctrine/Common/ClassLoader.php';

// Autoloading for Doctrine
$doctrineClassLoader = new ClassLoader('Doctrine', realpath(__DIR__ . '/../../php/PEAR'));
$doctrineClassLoader->register();

このファイルは、NetBeansプロジェクトの単体テスト用の部分であるテストファイルのホームディレクトリに配置されます。重要なことは、それはテストケースで編集するものではないということです。

于 2012-04-11T12:38:51.417 に答える