|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wearesho\Yii\Tests\Validators; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\CarbonInterval; |
|
6
|
|
|
|
|
7
|
|
|
use Wearesho\Delivery; |
|
8
|
|
|
|
|
9
|
|
|
use Horat1us\Yii\Exceptions\ModelException; |
|
10
|
|
|
use Wearesho\Yii\Interfaces\TokenRepositoryInterface; |
|
11
|
|
|
use Wearesho\Yii\Repositories\TokenRepository; |
|
12
|
|
|
|
|
13
|
|
|
use Wearesho\Yii\Tests\AbstractTestCase; |
|
14
|
|
|
use Wearesho\Yii\Tests\Mocks\TokenCheckModelMock; |
|
15
|
|
|
use Wearesho\Yii\Tests\Mocks\TokenGeneratorMock; |
|
16
|
|
|
use Wearesho\Yii\Tests\Mocks\TokenRecordMock; |
|
17
|
|
|
use Wearesho\Yii\Tests\Mocks\TokenRepositoryConfigMock; |
|
18
|
|
|
use Wearesho\Yii\Validators\TokenValidator; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class TokenValidatorTest |
|
22
|
|
|
* @package Wearesho\Yii\Tests\Validators |
|
23
|
|
|
* |
|
24
|
|
|
* @internal |
|
25
|
|
|
*/ |
|
26
|
|
|
class TokenValidatorTest extends AbstractTestCase |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var TokenRepository */ |
|
29
|
|
|
protected $repository; |
|
30
|
|
|
|
|
31
|
|
|
/** @var TokenCheckModelMock */ |
|
32
|
|
|
protected $model; |
|
33
|
|
|
|
|
34
|
|
|
protected function setUp() |
|
35
|
|
|
{ |
|
36
|
|
|
parent::setUp(); |
|
37
|
|
|
$this->repository = new TokenRepository( |
|
38
|
|
|
new TokenRecordMock(), |
|
39
|
|
|
$config = new TokenRepositoryConfigMock, |
|
40
|
|
|
new TokenGeneratorMock, |
|
41
|
|
|
new Delivery\ServiceMock() |
|
42
|
|
|
); |
|
43
|
|
|
$config->setExpirePeriod(CarbonInterval::years(5)); |
|
44
|
|
|
|
|
45
|
|
|
\Yii::$container->set( |
|
46
|
|
|
TokenRepositoryInterface::class, |
|
47
|
|
|
function () { |
|
48
|
|
|
return $this->repository; |
|
49
|
|
|
} |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$this->model = new TokenCheckModelMock; |
|
53
|
|
|
|
|
54
|
|
|
$token = new TokenRecordMock([ |
|
55
|
|
|
'id' => 1, |
|
56
|
|
|
'recipient' => "380500000001", |
|
57
|
|
|
'token' => "000001", |
|
58
|
|
|
]); |
|
59
|
|
|
ModelException::saveOrThrow($token); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testInvalidRecipient() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->model->recipient = "380500000000"; |
|
65
|
|
|
$this->model->token = "000001"; |
|
66
|
|
|
$this->model->validate(); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertTrue( |
|
69
|
|
|
$this->model->hasErrors('token') |
|
70
|
|
|
); |
|
71
|
|
|
$this->assertFalse( |
|
72
|
|
|
$this->model->hasErrors('recipient') |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testInvalidToken() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->model->recipient = "380500000001"; |
|
79
|
|
|
$this->model->token = "111111"; |
|
80
|
|
|
$this->model->validate(); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertTrue( |
|
83
|
|
|
$this->model->hasErrors('token') |
|
84
|
|
|
); |
|
85
|
|
|
$this->assertFalse( |
|
86
|
|
|
$this->model->hasErrors('recipient') |
|
87
|
|
|
); |
|
88
|
|
|
$this->assertEquals('Token is invalid.', $this->model->errors['token'][0]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testValid() |
|
92
|
|
|
{ |
|
93
|
|
|
$this->model->recipient = "380500000001"; |
|
94
|
|
|
$this->model->token = "000001"; |
|
95
|
|
|
$this->model->validate(); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertFalse($this->model->hasErrors()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testCustomMessage(): void |
|
101
|
|
|
{ |
|
102
|
|
|
$this->model->errorMessage = 'invalid token'; |
|
103
|
|
|
$this->model->recipient = "380500000001"; |
|
104
|
|
|
$this->model->token = "111111"; |
|
105
|
|
|
$this->model->validate(); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertEquals('invalid token', $this->model->errors['token'][0]); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testCustomLimitReached(): void |
|
111
|
|
|
{ |
|
112
|
|
|
$validator = new TokenValidator($this->createMock(TokenRepository::class)); |
|
|
|
|
|
|
113
|
|
|
$this->assertEquals('Limit of messages is reached', $validator->limitReachedMessage); |
|
114
|
|
|
$validator = new TokenValidator($this->createMock(TokenRepository::class), [ |
|
|
|
|
|
|
115
|
|
|
'limitReachedMessage' => 'custom message', |
|
116
|
|
|
]); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertEquals('custom message', $validator->limitReachedMessage); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: