0

Zend Framework 2 で独自のモジュールに対して PHPUnit テストを実行する際に、少し問題があるようです。

OS: Mac OS 10.8.3
Zend Framework 2.1.4
PHPUnit バージョン 3.7.19 (pear 経由でインストール)
PHP バージョン 5.3.15 (xdebug 有効、バージョン 2.1.3)

Zendガイドラインの指示に従って、モジュール「アルバム」を作成しました(http://framework.zend.com/manual/2.1/en/user-guide/modules.html

単体テストをモジュール フォルダーに入れる代わりに、すべてを 1 つの集中フォルダーに入れたいと考えています。これは私のアプリケーションの基本構造です:

-config
-data
-module -public -tests
--Modules --- Album ----AlbumTest.php --Bootstrap.php --phpunit.xml -vendor ...(ライセンス、readme、composers および init_autoloader.php)







-> モジュール アルバムのテストは、"tests" 内の "Modules/Album" フォルダにあります。フォルダー「tests」には、Bootstrap.php と phpunit.xml も含まれています。

ファイルの内容は次のとおりです。

Bootstrap.php

<?php

/*
 * Set error reporting to the level to which Zend Framework code must comply.
 */
error_reporting( E_ALL | E_STRICT );

/*
 * The first run required me to fix some constants
 */
defined('TESTS_ZEND_FORM_RECAPTCHA_PUBLIC_KEY') || define('TESTS_ZEND_FORM_RECAPTCHA_PUBLIC_KEY', 'public key');
defined('TESTS_ZEND_FORM_RECAPTCHA_PRIVATE_KEY') || define('TESTS_ZEND_FORM_RECAPTCHA_PRIVATE_KEY', 'private key');
defined('TESTS_ZEND_LDAP_PRINCIPAL_NAME') || define('TESTS_ZEND_LDAP_PRINCIPAL_NAME', 'someUser@example.com');
defined('TESTS_ZEND_LDAP_ALT_USERNAME') || define('TESTS_ZEND_LDAP_ALT_USERNAME', 'anotherUser');

chdir(dirname(__DIR__));

/*
 * autoload the application
 */
include __DIR__ . '/../init_autoloader.php';

Zend\Mvc\Application::init(include 'config/test.config.php');

phpunit.xml

<phpunit bootstrap="./Bootstrap.php" colors="true">
    <testsuites>
        <testsuite name="Zend Module Tests">
            <directory>./Modules</directory>
        </testsuite>
    </testsuites>

</phpunit>

AlbumTest.php

<?php

namespace AlbumTest\Model;

use Album\Model\Album;
use PHPUnit_Framework_TestCase;

/**
 * @category   Module
 * @package    Album
 * @subpackage UnitTests
 * @group      Module_Album
 */
class AlbumTest extends PHPUnit_Framework_TestCase {

    public function testAlbumInitialState() {
        $album = new Album();

        $this->assertNull($album->artist, '"artist" should initially be null');
        $this->assertNull($album->id, '"id" should initially be null');
        $this->assertNull($album->title, '"title" should initially be null');
    }

    public function testExchangeArraySetsPropertiesCorrectly() {
        $album = new Album();
        $data = array('artist' => 'some artist',
            'id' => 123,
            'title' => 'some title');

        $album->exchangeArray($data);

        $this->assertSame($data['artist'], $album->artist, '"artist" was not set correctly');
        $this->assertSame($data['id'], $album->id, '"id" was not set correctly');
        $this->assertSame($data['title'], $album->title, '"title" was not set correctly');
    }

    public function testExchangeArraySetsPropertiesToNullIfKeysAreNotPresent() {
        $album = new Album();

        $album->exchangeArray(array('artist' => 'some artist',
            'id' => 123,
            'title' => 'some title'));
        $album->exchangeArray(array());

        $this->assertNull($album->artist, '"artist" should have defaulted to null');
        $this->assertNull($album->id, '"id" should have defaulted to null');
        $this->assertNull($album->title, '"title" should have defaulted to null');
    }

    public function testGetArrayCopyReturnsAnArrayWithPropertyValues() {
        $album = new Album();
        $data = array('artist' => 'some artist',
            'id' => 123,
            'title' => 'some title');

        $album->exchangeArray($data);
        $copyArray = $album->getArrayCopy();

        $this->assertSame($data['artist'], $copyArray['artist'], '"artist" was not set correctly');
        $this->assertSame($data['id'], $copyArray['id'], '"id" was not set correctly');
        $this->assertSame($data['title'], $copyArray['title'], '"title" was not set correctly');
    }

    public function testInputFiltersAreSetCorrectly() {
        $album = new Album();

        $inputFilter = $album->getInputFilter();

        $this->assertSame(3, $inputFilter->count());
        $this->assertTrue($inputFilter->has('artist'));
        $this->assertTrue($inputFilter->has('id'));
        $this->assertTrue($inputFilter->has('title'));
    }

}

NetBeans は、phpunit バイナリの場所を知っています: (ここに画像を投稿したかったのですが、評判はありません :)、説明しようとします) へのパスは、オプションで構成されます - php - unit testing
/usr /local/pear/bin/phpunit
および /usr/local/pear/bin/phpunit-skelgen

プロジェクトのプロパティで、「XML 構成を使用」を設定し、パスを phpunit.xml に貼り付けました。また、「テストを実行する前にテストグループを要求する」をチェックしました-グループ「Module_Album」の上にテストを与えました-このように、PHPUnitが正しいテストを見つけることを確認します。

プロジェクトを右クリックして「テスト」を選択すると、グループ「Module_Album」が表示されます。それをチェックして「OK」をクリックします。それは何かを実行し、正しいphpunit.xmlを見つけたことを教えてくれます(「FULL_PATH/tests/phpunit.xmlから読み取った構成」)。実行後、テストを実行しなかったことがわかります。

これは、NetBeans での完全な出力です。

PHPUnit 3.7.19 by Sebastian Bergmann.

Configuration read from FULL_PATH/tests/phpunit.xml



Time: 9 seconds, Memory: 348.25Mb

[2KNo tests executed!
[2K
Generating code coverage report in Clover XML format ... done

とにかく、シェル(ターミナル)を介して同じことを成功させることができます。テスト ディレクトリを直接マウントして phpunit を実行する場合は問題ありません。phpunit バイナリへのフル パスを使用して (別のバージョンを使用していないことを確認してください)、phpunit.xml のフル パスを指定します。等々。以下にいくつかのサンプルを示します。

phpunit
phpunit -c FULL_PATH/tests/phpunit.xml
/usr/local/pear/bin/phpunit -c FULL_PATH/tests/phpunit.xml

これらのコマンドはすべて、私が期待するものを与えてくれます:

PHPUnit 3.7.19 by Sebastian Bergmann.

Configuration read from FULL_PATH/tests/phpunit.xml

.....

Time: 0 seconds, Memory: 12.75Mb

OK (5 tests, 16 assertions)
  • これがシェルでは機能するのに NetBeans 経由では機能しない理由がわかりません
  • NetBeans は 350MB を使用しますが、シェルでは 12.75MB しか使用しません
  • 「テストを実行する前にテストグループを要求する」オプションを削除すると、すべての ZendTest を実行しようとするようです。これがどのように可能かわかりません.phpunit.xmlからはそれらを見つけるべきではありません.

どんな助けでも大歓迎です、ありがとう!

4

1 に答える 1