我遇到了麻烦:我需要模拟一些对象来使用 Sf4.1 测试我的登录 WS。我已经安装了 codeception 和 AspectMock。不幸的是,我一个接一个地错误配置aspectmock。
首先,我试图简单地嘲笑我的经理:
我的登录中心:
public function postLoginWithLoginForm()
{
$this->apiTester->wantTo('test login with standard login form');
$tokenMock = Test::double(
new LoginManager(),
LoginHelper::loginFromFormLogin(
['_username' => 'FOO', '_password' => 'BAR']
)
);
$this->apiTester->sendPOST(
$this->router->generate(
static::MY_ROUTES['form_login']),
['_username' => 'FOO', '_password' => 'BAR']
);
$tokenMock->verifyInvokedOnce('loginFromFormLogin');
$this->apiTester->seeResponseIsJson();
$this->apiTester->seeResponseCodeIs(Response::HTTP_OK);
$response = json_decode($this->apiTester->grabResponse(), true);
$this->apiTester->assertArrayHasKey('token', $response);
}
登录助手:
class LoginHelper
{
public function __construct()
{
Test::clean();
}
/**
* @param array $postParameters
*
* @return string
*/
public static function loginFromFormLogin(array $postParameters):
string
{
if (
!array_key_exists('_username', $postParameters)
|| !array_key_exists('_password', $postParameters)
) {
throw new BadRequestHttpException();
}
return '16T3KLDN?LK';
}
}
我的路线控制器:
/**
* @Rest\Post("/form/login", name="post_form_login")
*
* @param Request $request
*
* @return JsonResponse
*/
public function postFormLogin(Request $request): JsonResponse
{
$postParameters = $request->request->all();
$token = $this->loginManager->loginFromFormLogin($postParameters);
return new JsonResponse(['token' => $token]);
}
有了这个,我在测试执行上有一个错误:
[Error] Call to a member function registerObject() on null
此外,我已经或多或少地为我的问题添加了这个线程:AspectMock GitHub
所以我在我的测试/目录下设置了一个引导文件,如下所示:
<?php
$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
'appDir' => __DIR__ . '/../../',
'cacheDir' => '/tmp/myapp',
'includePaths' => [__DIR__.'/../src', __DIR__.'/Helper'],
'excludePaths' => [__DIR__] // tests dir should be excluded
]);
但是现在,我总是这个错误:
[Symfony\Component\Debug\Exception\FatalErrorException]
Compile Error: Redefinition of parameter $_
你能帮我设法模拟我的测试吗?
预先感谢,