Symfony4,代码接收和AspectMock的模拟错误

时间:2018-10-18 12:11:11

标签: symfony4 codeception goaop

我有麻烦了: 我需要模拟一些对象以使用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);
}

LoginHelper:

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

所以我已经在我的tests /目录下设置了一个引导文件:

<?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 $_

可以帮我模拟一下我的测试吗?

先谢谢了

0 个答案:

没有答案