定制 cds-uwb/nette-presenter-tests 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cds-uwb/nette-presenter-tests

Composer 安装命令:

composer require cds-uwb/nette-presenter-tests

包简介

Library based on PHPUnit framework to test Nette presenters via simple API

README 文档

README

Nette Presenter Tests

Tests

Library based on PHPUnit framework to test Nette presenters via simple API.

The main functionality of Nette presenters is in their actions invoked by requests. The library allows to simply invoke presenter actions like as called from browser. Another functionality is handling form submission that can be also tested.

The easiest way to use the library is to extend Cds\NettePresenterTests\PresenterTestCase which provides all traits and also contains abstract method configureContainer that must be implemented to provide DI container for presenter creation.

Example of test class that extends PresenterTestCase:

final class PresenterTest extends Cds\NettePresenterTests\PresenterTestCase
{
    protected function setUp() : void
    {
        parent::setUp();

        // Initialize presenter for all following tests
        $this->presenter = $this->createPresenter(MyPresenter::class);
    }

    public function testAction(): void
    {
        // Invoke MyPresenter:default action with parameters
        $response = $this->request('default', [
            'param1' => 'value1',
        ]);

        // Check the presenter response is TextResponse with non-empty result
        self::assertNonEmptyTextResponse($response);
    }

    public function testActionRedirect(): void
    {
        // Invoke MyPresenter:redirect action
        $response = $this->request('redirect');

        // Check the presenter response is RedirectResponse with given target
        self::assertRedirectResponseTo('MyPresenter:default', $response);
    }

    public function testFormSubmit(): void
    {
        // Submit form 'testForm' via action default
        $response = $this->submitForm('default', [], [
            'key1' => 'value1',
            'key2' => [1, 2, 3],
        ], 'testForm');

        // Check the presenter response is RedirectResponse with given target
        self::assertRedirectResponseTo('MyPresenter:default', $response);
    }

    // Presenter instance is created from the Nette DI container.
    // In this function you can configure the container as needed for your tests.
    // This is the simplest example how to create the container.
    protected function configureContainer(): Container
    {
        $tempDirectory = __DIR__ . '/temp';

        $configurator = new Configurator();
        $configurator->setDebugMode(true);
        $configurator->setTempDirectory($tempDirectory);

        return $configurator->createContainer();
    }
}

If you don't want to extend Cds\NettePresenterTests\PresenterTestCase, you can use the traits directly. When using traits only you must create the DI container yourself and expose it as a protected Container $container property because PresenterCreate uses $this->container to instantiate presenters.

final class PresenterTest extends \PHPUnit\Framework\TestCase
{
    // Whole functionality is available via traits
    use \Cds\NettePresenterTests\Traits\Presenter;

    // Provide container for the traits
    protected Container $container;

    protected function setUp() : void
    {
        parent::setUp();

        $tempDirectory = __DIR__ . '/temp';

        $configurator = new Configurator();
        $configurator->setDebugMode(true);
        $configurator->setTempDirectory($tempDirectory);

        // Create and store the container so traits can use it
        $this->container = $configurator->createContainer();

        // Initialize presenter for all following tests
        $this->presenter = $this->createPresenter(MyPresenter::class);
    }

    // Tests ...
}

Available traits

  • AssertResponse - adds assertions that allow to check presenter responses.
  • ExpectErrors - adds expectations to check raising presenter error via error method.
  • Presenter - union trait that includes all other traits.
  • PresenterCreate - add method to create instance of presenter from class name via Nette DI container.
  • PresenterRequest - simple API for invoking requests on property presenter (uses PresenterRun methods).
  • PresenterRun - main request API for invoking requests on given presenter.

Presenter mapping

The library require mapping from presenter class to presenter name which is used in request. In normal application the name is built by router and then passed to presenter factory which create presenter instance. The library works directly on presenter instance, so it needs to convert presenter class to presenter name which is then passed to presenter request. Default implementation uses some predefined rules to convert class to name but if something else is needed, own implementation of buildNameFromPresenter can be implemented to provide required mapping.

final class MyTest extends \PHPUnit\Framework\TestCase
{
    use \Cds\NettePresenterTests\Traits\Presenter;

    protected function buildNameFromPresenter(string $class) : string
    {
        // All request are from TestPresenter
        return 'Test';
    }
}

It's also possible to use Nette's presenter factory to provide class to name mapping but unformatPresenterClass is marked as internal and deprecated. Alternatively nepada/presenter-mapping can be used which provides this method (for now).

More examples

File upload testing

The library also allows test file uploading as part of form submission. The definition of file upload is specified as part of form data only using $this->uploadFile method that specifies file name, and it's content. It's also possible to specify upload result code (PHP's UPLOAD_ERR_* constants).

final class PresenterTest extends \PHPUnit\Framework\TestCase
{
    // Whole functionality is available via traits
    use \Cds\NettePresenterTests\Traits\Presenter;

    protected function setUp() : void
    {
        parent::setUp();

        // Initialize presenter for all following tests
        $this->presenter = $this->createPresenter(MyPresenter::class);
    }

    public function testFileUpload(): void
    {
        $response = $this->submitForm('default', [], [
            'file1' => $this->uploadFile('file1.txt', 'Hello World!'),
            // Upload failure can be tested via error argument
            'file2' => $this->uploadFile(error: UPLOAD_ERR_CANT_WRITE),
        ], 'testForm');

        // Check the presenter response is RedirectResponse with given target
        self::assertRedirectResponseTo('MyPresenter:default', $response);
    }
}

Testing without presenter property

It's possible test multiple different presenters without presenter property. Testing is done by PresenterRequest trait and its function runPresenter that allow to pass any presenter as argument.

final class PresenterTest extends \PHPUnit\Framework\TestCase
{
    use \Cds\NettePresenterTests\Traits\PresenterRequest;
    use \Cds\NettePresenterTests\Traits\PresenterCreate;

    public function testAction(): void
    {
        $presenter = $this->createPresenter(MyPresenter::class);

        // Invoke MyPresenter:default action with parameters
        $response = $this->runPresenter($presenter, 'default', [
            'param1' => 'value1',
        ]);

        // Check the presenter response is TextResponse with non-empty result
        self::assertNonEmptyTextResponse($response);
    }

}

统计信息

  • 总下载量: 39
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固