承接 mangoweb/presenter-tester 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

mangoweb/presenter-tester

Composer 安装命令:

composer require mangoweb/presenter-tester

包简介

README 文档

README

Build Status

Testing tool for Nette presenter with easy to use API.

Installation

The recommended way to install is via Composer:

composer require mangoweb/presenter-tester

It requires PHP version 7.1.

Integration & configuration

If you are using power of Nette DI Container in your tests, you can use Presenter Tester in your current testing environment. All you need is to register PresenterTester service in .neon configuration for tests.

services:
	- Mangoweb\Tester\PresenterTester\PresenterTester(baseUrl: "http://my-app.dev")

You can also specify list of listeners:

services:
	- Mangoweb\Tester\PresenterTester\PresenterTester(
		baseUrl: "http://my-app.dev"
		listeners: [
			MyListener()
		]
	)

Other way is to use Presenter Tester together with mangoweb/tester-infrastructure. In that case you have to register DI extension in infrastructure .neon file:

extensions:
	mango.presenterTester: Mangoweb\Tester\PresenterTester\Bridges\Infrastructure\PresenterTesterExtension

In configuration of the extension you can set a base url and custom identity factory.

mango.presenterTester:
	baseUrl: http://my-app.dev
	identityFactory: MyIdentityFactory()

Usage

In Mango Tester Infrastructure environment, service PresenterTester is available in infrastructure container. When you get the service, you can start testing your presenters:

$testRequest = $presenterTester->createRequest('Admin:Article')
	->withParameters([
		'action' => 'edit',
		'id' => 1,
	]);
$testResult = $presenterTester->execute($testRequet);
$testResult->assertRenders('%A%Hello world article editation%A%');	

As you can see, you first create a TestPresenterRequest using createRequest method on PresenterTester. You pass a presenter name (without an action) and later you configure the test request. You can set additional request parameters like action or your own application parameters. There are many other things you can configure on the request, like form values or headers.

After the test request is configured, you pass it to execute method, which performs presenter execution and returns TestPresenterResult, which wraps Nette\Application\IResponse with some additional data collected during execution.

The TestPresenterResult contains many useful assert functions like render check or form validity check. In our example there is assertRenders method, which asserts that presenter returns TextResponse and that the text contains given pattern. You probably already know the pattern format from Tester\Assert::match() function.

TestPresenterRequest API

Beware that TestPresenterRequest is immutable object.

withParameters(array $parameters)

Set application request parameters.

withForm(string $formName, array $post, array $files)

Add form submission data to request. You have to specify full component tree path to in $formName.

Presenter Tester supports forms with CSRF protection, but since it uses session, it is recommended to install mangoweb/tester-http-mocks package.

withSignal(string $signal, array $componentParameters = [], string $componentClass = null)

With Presenter Tester, you can also easily test signal method. The componentClass is only required in the case you are using nextras/secured-links (which you should). It is also recommended to install mangoweb/tester-http-mocks package.

withAjax

(Not only) signals often uses AJAX, which you can enable using this method.

withMethod(string $methodName)

Change the HTTP method. The default is GET. You don't have to explicitly set a method for forms.

withHeaders(array $headers)

Pass additional HTTP headers.

withIdentity(Nette\Security\IIdentity $identity)

Change identity of User, which is executing given request. This is useful when login is required to perform the action. You can implement identity factory, which provides a default identity for each request.

withPost(array $post)

withFiles(array $files)

withRawBody(string$rawBody)

TestPresenterResult API

It is a result of test execution. It wraps Nette\Application\IResponse and adds few methods to check the response easily.

assertRenders($match)

Checks that response is TextResponse. Also, you can provide a $match parameter to check that response contains some text. You can either pass pattern or an array plain strings.

assertNotRenders($matches)

Checks that given pattern or strings were not rendered.

assertJson($expected)

Checks that response is JSON. You can optionally pass expected payload.

assertBadRequest($code)

Checks that requests terminates with bad request exception (e.g. 404 not found).

assertRedirects(string $presenterName, array $parameters)

Checks that request redirects to given presenter. You can also pass parameters to check. Extra parameters in redirect request are ignored.

assertRedirectsUrl($url)

assertFormValid($formName)

assertFormHasErrors($formName, $formErrors)

Also, there are methods like getResponse or getPresenter to access original data a perform some custom checks.

Listeners

You can hook to some events by implementing Mangoweb\Tester\PresenterTester\IPresenterTesterListener interface. Then you can e.g. modify test request or execute some implicit result checks.

To register a listener, simply register it as a service in DI container (infrastructure container if you are using Mango Tester Infrastructure).

Identity factory

Using identity factory you can implement a factory which creates a default identity. The factory is a simple PHP callback, which accepts PresenterTestRequest and returns Nette\Security\IIdentity.

mangoweb/presenter-tester 适用场景与选型建议

mangoweb/presenter-tester 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 187.05k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2017 年 08 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mangoweb/presenter-tester 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mangoweb/presenter-tester 我们能提供哪些服务?
定制开发 / 二次开发

基于 mangoweb/presenter-tester 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 187.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 19
  • Watchers: 8
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-08-26