如何修复cakephp2和phpunit4中的模拟身份验证错误?

如何修复cakephp2和phpunit4中的模拟身份验证错误?,php,phpunit,cakephp-2.5,Php,Phpunit,Cakephp 2.5,我发现,静态方法“user”不能在mock对象上调用,甚至没有mockauth->user Im使用php7、cakephp2.x和phpunit4+ 根据cakephp2文件: https://book.cakephp.org/2.0/en/development/testing.html If you are using PHPUnit 4 or 5, staticExpects() does not exist anymore. Instead, you should insert the

我发现,静态方法“user”不能在mock对象上调用,甚至没有mock
auth->user

Im使用php7、cakephp2.x和phpunit4+ 根据cakephp2文件:

https://book.cakephp.org/2.0/en/development/testing.html
If you are using PHPUnit 4 or 5, staticExpects() does not exist anymore. Instead, you should insert the necessary data into the session with CakeSession::write('Auth.User', $user) before calling the action.
我尝试使用:

$user = [
  'id' => 1,
  'username' => 'mark'
];

CakeSession::write('Auth.User', $user);
但打电话时:

$user = $this->Controller->Auth->user();
我仍然得到:
无法对模拟对象调用静态方法“user”
错误

我肯定错过了什么,但我不知道是什么。你能帮我找到正确的方法吗

谢谢

我正在跑步:

  • PHP7.3.9
  • CakePHP 2.10.16
  • PHPUnit 5.7.27
我通过模拟
会话
Auth
组件解决了这个问题。我正在测试
设置中创建控制器,如下所示:

<?php
App::uses('CakeSession', 'Model/Datasource');

class MyIntegrationTestCase extends ControllerTestCase
{
  protected $ctrl;

  public function setUp()
  {
    parent::setUp();

    // https://api.cakephp.org/2.10/class-ControllerTestCase.html#_generate
    $mocks = [
      'components' => [
        'Session',
        'Auth',
      ]
    ];
    $this->ctrl = $this->generate('Quotes', $mocks);

    $user = [
      'id' => 1,
      'username' => 'alistaircol',
    ];
    CakeSession::write('Auth.User', $user);
  }
}