helbrary / nette-tester-extension
nette tester 扩展库。
v0.1.4
2018-02-19 04:20 UTC
Requires
- php: >= 5.3.7
- nette/nette: >= 2.3.0
- nette/tester: >=1.5.6
- nette/utils: >= 2.4.0
This package is auto-updated.
Last update: 2024-09-18 21:15:43 UTC
README
简化演示者的测试
库包含 BasePresenterTester 类,提供以下方法:
- checkRequestNoError - 期望演示者的某些操作无错误
- checkRequestError - 期望演示者的某些操作抛出错误
- checkRedirectTo - 期望演示者的某些操作返回重定向
身份验证器
默认情况下,所有请求都发送给未登录用户。如果我们想测试用户登录时的行为,可以使用 "$userId" 等参数,它们提供了所有 check* 方法。我们不需要知道用户的密码。只需说明我们想以用户 id "$userId" 登录。
登录用户的默认角色是 'admin'。要更改角色,请使用参数 "$userRole"
实践中的示例
!!! 重要推荐:在开始测试之前清除缓存 !!!
对于要测试的每个演示者,必须创建单独的类
<?php require __DIR__ . '/../../../../vendor/autoload.php'; require __DIR__ . '/../../../../vendor/helbrary/nette-tester-extension/src/BasePresenterTester.php'; final class TestAccountPresenter extends \Helbrary\NetteTesterExtension\BasePresenterTester { /** * @var \Model\UserModel */ private $userModel; /** * @var bool|\Model\User */ private $testingUser; /** * TestGoodsPresenter constructor. */ public function __construct() { parent::__construct('Front:Account'); // Here is defined presenter which we want test $this->userModel = $this->container->getByType('\Model\UserModel'); $this->testingUser = $this->userModel->find()->fetch(); // get some user from db } /** * Test detail action of account presenter */ public function testDetail() { // if no user is logged in, expected redirect to Sign in $this->checkRedirectTo(array( 'action' => 'detail', 'id' => 8, // parameter id for action detail ), 'Front:Sign:in'); // if some user is logged in, expected that action detail will be render without error // after send this request is logged in user with id $this->testingUser->id $this->checkRequestNoError(array( 'action' => 'detail', ), 'GET', $this->testingUser->id); } } $testCase = new TestAccountPresenter(); $testCase->run();
一个测试中的多个演示者测试器
<?php require __DIR__ . '/../../vendor/autoload.php'; require __DIR__ . '/../../vendor/helbrary/nette-tester-extension/src/BasePresenterTester.php'; final class TestPresenterActions extends \Helbrary\NetteTesterExtension\BaseMultiPresenterTester { /** * TestGoodsPresenter constructor. */ public function __construct() { parent::__construct(); } public function testActions() { $actions = [ 'Core:Customers' => [ 'actions' => [ [ 'parameters' => ['action' => 'default'], // optional - default is empty array 'method' => 'GET', // optional - default is GET 'userId' => NULL, // optional - default is NULL 'userRole' => NULL, // optional - default is NULL 'identityData' => NULL, // optional - default is NULL ], [ 'parameters' => ['id' => 1, 'action' => 'default'], ], ] ] ]; $this->checkWihtoutErorrs($actions); } } $testCase = new TestPresenterActions(); $testCase->run();