Skip to content

Commit 5d11fdc

Browse files
committed
Cover Unit test
1 parent 1bd7d1e commit 5d11fdc

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Test\Unit\Model\ForgotPasswordToken;
9+
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Customer\Api\Data\CustomerSearchResultsInterface;
13+
use Magento\Customer\Model\ForgotPasswordToken\GetCustomerByToken;
14+
use Magento\Framework\Api\SearchCriteria;
15+
use Magento\Framework\Api\SearchCriteriaBuilder;
16+
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Framework\Exception\State\ExpiredException;
18+
use Magento\Framework\Phrase;
19+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* Test GetCustomerByToken class
25+
*/
26+
class GetCustomerByTokenTest extends TestCase
27+
{
28+
protected const RESET_PASSWORD = 'resetPassword';
29+
30+
/**
31+
* @var SearchCriteriaBuilder|MockObject
32+
*/
33+
protected $searchCriteriaBuilderMock;
34+
35+
/**
36+
* @var SearchCriteria|MockObject
37+
*/
38+
protected $searchCriteriaMock;
39+
40+
/**
41+
* @var CustomerRepositoryInterface|MockObject
42+
*/
43+
protected $customerRepositoryMock;
44+
45+
/**
46+
* @var CustomerSearchResultsInterface|MockObject
47+
*/
48+
protected $searchResultMock;
49+
50+
/**
51+
* @var CustomerInterface|MockObject
52+
*/
53+
protected $customerMock;
54+
55+
/**
56+
* @var GetCustomerByToken;
57+
*/
58+
protected $model;
59+
60+
/**
61+
* @inheritDoc
62+
*/
63+
public function setUp()
64+
{
65+
$this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
66+
$this->searchCriteriaMock = $this->createMock(SearchCriteria::class);
67+
$this->searchResultMock = $this->createMock(CustomerSearchResultsInterface::class);
68+
$this->customerRepositoryMock = $this->createMock(CustomerRepositoryInterface::class);
69+
$this->customerMock = $this->getMockForAbstractClass(CustomerInterface::class);
70+
71+
$objectManagerHelper = new ObjectManagerHelper($this);
72+
$this->model = $objectManagerHelper->getObject(
73+
GetCustomerByToken::class,
74+
[
75+
'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
76+
'customerRepository' => $this->customerRepositoryMock
77+
]
78+
);
79+
80+
$this->searchCriteriaBuilderMock->expects($this->once())
81+
->method('create')
82+
->willReturn($this->searchCriteriaMock);
83+
$this->customerRepositoryMock->expects($this->once())
84+
->method('getList')
85+
->with($this->searchCriteriaMock)
86+
->willReturn($this->searchResultMock);
87+
}
88+
89+
/**
90+
* @return void
91+
*/
92+
public function testExecute(): void
93+
{
94+
$totalCount = 1;
95+
$this->searchResultMock->expects($this->any())
96+
->method('getTotalCount')
97+
->willReturn($totalCount);
98+
$this->searchResultMock->expects($this->once())
99+
->method('getItems')
100+
->willReturn([$this->customerMock]);
101+
102+
$this->assertInstanceOf(
103+
CustomerInterface::class,
104+
$this->model->execute(self::RESET_PASSWORD)
105+
);
106+
}
107+
108+
/**
109+
* @return void
110+
*/
111+
public function testExecuteWithNoSuchEntityException(): void
112+
{
113+
$totalCount = 0;
114+
$this->searchResultMock->expects($this->any())
115+
->method('getTotalCount')
116+
->willReturn($totalCount);
117+
$this->expectExceptionObject(new NoSuchEntityException(
118+
new Phrase(
119+
'No such entity with rp_token = %value',
120+
['value' => self::RESET_PASSWORD]
121+
)
122+
));
123+
124+
$this->model->execute(self::RESET_PASSWORD);
125+
}
126+
127+
/**
128+
* @return void
129+
*/
130+
public function testExecuteWithExpireException(): void
131+
{
132+
$totalCount = 2;
133+
$this->searchResultMock->expects($this->any())
134+
->method('getTotalCount')
135+
->willReturn($totalCount);
136+
137+
$this->expectExceptionObject(new ExpiredException(
138+
new Phrase(
139+
'Reset password token expired.'
140+
)
141+
));
142+
143+
$this->model->execute(self::RESET_PASSWORD);
144+
}
145+
}

0 commit comments

Comments
 (0)