|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Wearesho\Yii\Tests\Models; |
|
6
|
|
|
|
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
|
|
9
|
|
|
use Wearesho\Yii\Models\Token; |
|
10
|
|
|
use Wearesho\Yii\Queries\TokenQuery; |
|
11
|
|
|
use Wearesho\Yii\Tests\AbstractTestCase; |
|
12
|
|
|
|
|
13
|
|
|
use Wearesho\Yii\Tests\Mocks\TokenRecordMock; |
|
14
|
|
|
use yii\db\ActiveRecord; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class TokenTest |
|
18
|
|
|
* @package Wearesho\Yii\Tests\Models |
|
19
|
|
|
* |
|
20
|
|
|
* @internal |
|
21
|
|
|
*/ |
|
22
|
|
|
class TokenTest extends AbstractTestCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var Token */ |
|
25
|
|
|
protected $model; |
|
26
|
|
|
|
|
27
|
|
|
protected function setUp(): void |
|
28
|
|
|
{ |
|
29
|
|
|
parent::setUp(); |
|
30
|
|
|
$this->model = new TokenRecordMock(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testDefaultTimestamp(): void |
|
34
|
|
|
{ |
|
35
|
|
|
Carbon::setTestNow($now = Carbon::now()); |
|
36
|
|
|
$this->model->trigger(ActiveRecord::EVENT_BEFORE_INSERT); |
|
37
|
|
|
$this->assertEquals( |
|
38
|
|
|
$this->model->created_at, |
|
39
|
|
|
$now->toDateTimeString(), |
|
40
|
|
|
"created_at should be filled from now on insert" |
|
41
|
|
|
); |
|
42
|
|
|
Carbon::setTestNow(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testRecipient(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$recipient = mt_rand(); |
|
48
|
|
|
$this->model->setRecipient((string)$recipient); |
|
49
|
|
|
$this->assertEquals( |
|
50
|
|
|
$recipient, |
|
51
|
|
|
$this->model->getRecipient() |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testToken(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$token = mt_rand(); |
|
58
|
|
|
$this->model->setToken((string)$token); |
|
59
|
|
|
$this->assertEquals( |
|
60
|
|
|
$token, |
|
61
|
|
|
$this->model->getToken() |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testData(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$data = [ |
|
68
|
|
|
mt_rand() => mt_rand(), |
|
69
|
|
|
]; |
|
70
|
|
|
$this->model->setData($data); |
|
71
|
|
|
$this->assertEquals( |
|
72
|
|
|
$data, |
|
73
|
|
|
$this->model->getData() |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testDeliveryCount(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->assertEquals( |
|
80
|
|
|
0, |
|
81
|
|
|
$this->model->getDeliveryCount(), |
|
82
|
|
|
"Delivery count should be 0 by default" |
|
83
|
|
|
); |
|
84
|
|
|
$this->model->increaseDeliveryCount(); |
|
85
|
|
|
$this->assertEquals( |
|
86
|
|
|
1, |
|
87
|
|
|
$this->model->getDeliveryCount() |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testVerifyCount(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$this->assertEquals( |
|
94
|
|
|
0, |
|
95
|
|
|
$this->model->getVerifyCount(), |
|
96
|
|
|
"Verify count should be 0 by default" |
|
97
|
|
|
); |
|
98
|
|
|
$this->model->increaseVerifyCount(); |
|
99
|
|
|
$this->assertEquals( |
|
100
|
|
|
1, |
|
101
|
|
|
$this->model->getVerifyCount() |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testFind(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$this->assertInstanceOf( |
|
108
|
|
|
TokenQuery::class, |
|
109
|
|
|
$this->model->find() |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testValidation(): void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->assertFalse( |
|
116
|
|
|
$this->model->validate() |
|
117
|
|
|
); |
|
118
|
|
|
$this->assertTrue( |
|
119
|
|
|
$this->model->hasErrors('token') |
|
120
|
|
|
); |
|
121
|
|
|
$this->assertTrue( |
|
122
|
|
|
$this->model->hasErrors('recipient') |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
$this->model->setRecipient((string)mt_rand()); |
|
126
|
|
|
$this->model->setToken((string)mt_rand()); |
|
127
|
|
|
$this->assertTrue( |
|
128
|
|
|
$this->model->validate(), |
|
129
|
|
|
"Only token and recipient should by required attributes" |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testToStringConversion(): void |
|
134
|
|
|
{ |
|
135
|
|
|
$token = 'test'; |
|
136
|
|
|
|
|
137
|
|
|
$this->model->setToken($token); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertEquals($token, $this->model->__toString()); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|