Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AuthTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class AuthTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $_model;
19 
23  protected $_eventManagerMock;
24 
29 
33  protected $_modelFactoryMock;
34 
35  protected function setUp()
36  {
37  $this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
38  $this->_credentialStorage = $this->getMockBuilder(
39  \Magento\Backend\Model\Auth\Credential\StorageInterface::class
40  )
41  ->setMethods(['getId'])
42  ->getMockForAbstractClass();
43  $this->_modelFactoryMock = $this->createMock(\Magento\Framework\Data\Collection\ModelFactory::class);
44  $objectManager = new ObjectManager($this);
45  $this->_model = $objectManager->getObject(
46  \Magento\Backend\Model\Auth::class,
47  [
48  'eventManager' => $this->_eventManagerMock,
49  'credentialStorage' => $this->_credentialStorage,
50  'modelFactory' => $this->_modelFactoryMock
51  ]
52  );
53  }
54 
58  public function testLoginFailed()
59  {
60  $this->_modelFactoryMock
61  ->expects($this->once())
62  ->method('create')
63  ->with(\Magento\Backend\Model\Auth\Credential\StorageInterface::class)
64  ->will($this->returnValue($this->_credentialStorage));
65  $exceptionMock = new \Magento\Framework\Exception\LocalizedException(
66  __(
67  'The account sign-in was incorrect or your account is disabled temporarily. '
68  . 'Please wait and try again later.'
69  )
70  );
71  $this->_credentialStorage
72  ->expects($this->once())
73  ->method('login')
74  ->with('username', 'password')
75  ->will($this->throwException($exceptionMock));
76  $this->_credentialStorage->expects($this->never())->method('getId');
77  $this->_eventManagerMock->expects($this->once())->method('dispatch')->with('backend_auth_user_login_failed');
78  $this->_model->login('username', 'password');
79 
80  $this->expectExceptionMessage(
81  'The account sign-in was incorrect or your account is disabled temporarily. '
82  . 'Please wait and try again later.'
83  );
84  }
85 }
$objectManager
Definition: bootstrap.php:17
__()
Definition: __.php:13