1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Test\Unit\Model;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
/**
* Class AuthTest
*/
class AuthTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Backend\Model\Auth
*/
protected $_model;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $_eventManagerMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $_credentialStorage;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $_modelFactoryMock;
protected function setUp()
{
$this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
$this->_credentialStorage = $this->getMockBuilder(
\Magento\Backend\Model\Auth\Credential\StorageInterface::class
)
->setMethods(['getId'])
->getMockForAbstractClass();
$this->_modelFactoryMock = $this->createMock(\Magento\Framework\Data\Collection\ModelFactory::class);
$objectManager = new ObjectManager($this);
$this->_model = $objectManager->getObject(
\Magento\Backend\Model\Auth::class,
[
'eventManager' => $this->_eventManagerMock,
'credentialStorage' => $this->_credentialStorage,
'modelFactory' => $this->_modelFactoryMock
]
);
}
/**
* @expectedException \Magento\Framework\Exception\AuthenticationException
*/
public function testLoginFailed()
{
$this->_modelFactoryMock
->expects($this->once())
->method('create')
->with(\Magento\Backend\Model\Auth\Credential\StorageInterface::class)
->will($this->returnValue($this->_credentialStorage));
$exceptionMock = new \Magento\Framework\Exception\LocalizedException(
__(
'The account sign-in was incorrect or your account is disabled temporarily. '
. 'Please wait and try again later.'
)
);
$this->_credentialStorage
->expects($this->once())
->method('login')
->with('username', 'password')
->will($this->throwException($exceptionMock));
$this->_credentialStorage->expects($this->never())->method('getId');
$this->_eventManagerMock->expects($this->once())->method('dispatch')->with('backend_auth_user_login_failed');
$this->_model->login('username', 'password');
$this->expectExceptionMessage(
'The account sign-in was incorrect or your account is disabled temporarily. '
. 'Please wait and try again later.'
);
}
}