문제

Zend_test를 설정하는 성공이 있습니까? 방법/접근 방식은 무엇이며 테스트/테스트 스위트를 어떻게 실행합니까?

이미 PHPUNIT가 설치되고 작동했습니다. 이제 간단한 컨트롤러 테스트를 작성하려고합니다. Zend 프레임 워크 문서는 자동로드가 설정되었다고 가정합니다. 어떤 방법이 적절한 파일을 자동로드하는 데 사용하십니까? 나는 일반적인 부트 스트랩 파일로 그렇게하지만, 많은 포함과 경로를 설정하여 테스트를 막고 싶지 않습니다. 추상 컨트롤러 테스트 케이스 클래스가 갈 길이가 될까요?

문서가 사용하는 것과 같은 부트 스트랩 플러그인은 어떻습니까? 테스트를 부트 스트랩하는 방법입니까, 아니면 다른 방식으로 수행하고 싶습니까? 일반 부트 스트랩 파일을 가능한 한 많이 재사용하고 싶습니다. 테스트 및 정상적인 사용을 위해 부트 스트랩을 어떻게 건조시켜야합니까?

지금까지 내 테스트는 다음과 같습니다.

<?php

class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    public function setUp()
    {
        $this->bootstrap = array($this, 'appBootstrap');
        parent::setUp();
    }

    public function appBootstrap()
    {
        $this->frontController->registerPlugin(new Bootstrapper('testing'));
    }

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

}

//straight from the documentation
class Bootstrapper extends Zend_Controller_Plugin_Abstract
{
    /**
     * @var Zend_Config
     */
    protected static $_config;

    /**
     * @var string Current environment
     */
    protected $_env;

    /**
     * @var Zend_Controller_Front
     */
    protected $_front;

    /**
     * @var string Path to application root
     */
    protected $_root;

    /**
     * Constructor
     *
     * Initialize environment, root path, and configuration.
     *
     * @param  string $env
     * @param  string|null $root
     * @return void
     */
    public function __construct($env, $root = null)
    {
        $this->_setEnv($env);
        if (null === $root) {
            $root = realpath(dirname(__FILE__) . '/../../../');
        }
        $this->_root = $root;

        $this->initPhpConfig();

        $this->_front = Zend_Controller_Front::getInstance();
    }

    /**
     * Route startup
     *
     * @return void
     */
    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
        $this->initDb();
        $this->initHelpers();
        $this->initView();
        $this->initPlugins();
        $this->initRoutes();
        $this->initControllers();
    }

    // definition of methods would follow...
}

어떻게해야합니까?

도움이 되었습니까?

해결책

내가 작동하기 위해 내가 한 일은 다음과 같습니다.

먼저 자동 로딩 문제를 해결해야합니다. 모든 테스트에 포함될 파일을 만들어 테스트 디렉토리에 넣어서이를 수행하겠습니다. 참고 : 나는이 모든 것을 /public/index.php에서 거의 복사했습니다.

# /tests/loader.php

<?php

define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application/'));

set_include_path( APPLICATION_PATH . '/../library' . PATH_SEPARATOR .
                  APPLICATION_PATH . '/models' . PATH_SEPARATOR .
                  APPLICATION_PATH . '/forms' . PATH_SEPARATOR .
                  get_include_path() );

require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();

둘째,이 파일을 테스트에 포함시켜야합니다. 테스트 파일은/테스트/애플리케이션/컨트롤러에 있습니다. 부트 스트랩 파일과 같은 방식으로 작동하기 때문에 내 부트 스트랩을 플러그인으로 사용하지 않을 것입니다. QuickStart 튜토리얼. 위치를 공개 $ 부트 스트랩으로 설정하여 연결하겠습니다. 언제 Zend_Test_PHPUnit_ControllerTestCase Constructs, 여기에서 설정 한 부트 스트랩 파일을 찾습니다.

<?php

require_once '../../loader.php';

class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    public $bootstrap = '../../../application/bootstrap.php';

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

}

그리고 그게 다야! 내 부트 스트랩 파일이 이미 플러그인이라면 조금 더 복잡 할 수 있지만 그렇지 않기 때문에 매우 쉽습니다.

다른 팁

사용하는 대신

require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();

변경하십시오

require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

RegisterAutoLoad ()는 1.8.0 기준으로 더 이상 사용되지 않습니다

기본 초기화 작업을 수행하는 간단한 TestHelper.php를 항상 사용합니다. 이 파일은 모든 테스트 사례 파일에 포함되어 있습니다. 내가하는 일 중 하나는 특히 필터, 유효성 검사기 및 양식을 사용할 때 주요 종속성 문제를 경험하기 때문에 Zend Framework Autoloader를 등록하는 것입니다. 필요한 모든 파일을 추적하고 테스트 케이스에 수동으로 포함시키는 것은 거의 불가능합니다.

이 절차는 실제 응용 프로그램에 대해 동일해야하므로 자동로드 초기화와 포함 경로 설정을 부트 스트랩 플러그인으로 이동할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top