1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace IonGhitun\MysqlEncryption\Tests\Models; |
5
|
|
|
|
6
|
|
|
use IonGhitun\MysqlEncryption\Models\BaseModel; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class BaseModelTest |
12
|
|
|
* |
13
|
|
|
* @package IonGhitun\MysqlEncryption\Tests\Models |
14
|
|
|
*/ |
15
|
|
|
class BaseModelTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Test getAnonymizable |
19
|
|
|
*/ |
20
|
|
|
public function testGetAnonymizable() |
21
|
|
|
{ |
22
|
|
|
$model = new BaseModel(); |
23
|
|
|
|
24
|
|
|
$this->assertEquals([], $model->getAnonymizable()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Test getEncrypted |
29
|
|
|
*/ |
30
|
|
|
public function testGetEncrypted() |
31
|
|
|
{ |
32
|
|
|
$model = new BaseModel(); |
33
|
|
|
|
34
|
|
|
$this->assertEquals([], $model->getEncrypted()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Test getAttribute |
39
|
|
|
*/ |
40
|
|
|
public function tesBaseGetAttribute() |
41
|
|
|
{ |
42
|
|
|
$model = new BaseModel(); |
43
|
|
|
|
44
|
|
|
$model->name = 'Test'; |
|
|
|
|
45
|
|
|
|
46
|
|
|
$this->assertEquals('Test', $model->name); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Test getAttribute |
51
|
|
|
* |
52
|
|
|
* @throws \ReflectionException |
53
|
|
|
*/ |
54
|
|
|
public function testEncryptedGetAttribute() |
55
|
|
|
{ |
56
|
|
|
$model = new BaseModel(); |
57
|
|
|
|
58
|
|
|
$reflection = new ReflectionClass($model); |
59
|
|
|
|
60
|
|
|
$encrypted = $reflection->getProperty('encrypted'); |
61
|
|
|
$encrypted->setAccessible(true); |
62
|
|
|
|
63
|
|
|
$encrypted->setValue($model, ['name']); |
64
|
|
|
|
65
|
|
|
$model->name = 'Test'; |
|
|
|
|
66
|
|
|
|
67
|
|
|
$this->assertEquals('Test', $model->name); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Test toArray |
72
|
|
|
*/ |
73
|
|
|
public function testToArray() |
74
|
|
|
{ |
75
|
|
|
$model = new BaseModel(); |
76
|
|
|
|
77
|
|
|
$model->name = 'Test'; |
|
|
|
|
78
|
|
|
|
79
|
|
|
$this->assertEquals(['name' => 'Test'], $model->toArray()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Test toArray |
84
|
|
|
* |
85
|
|
|
* @throws \ReflectionException |
86
|
|
|
*/ |
87
|
|
|
public function testEncryptedToArray() |
88
|
|
|
{ |
89
|
|
|
$model = new BaseModel(); |
90
|
|
|
|
91
|
|
|
$reflection = new ReflectionClass($model); |
92
|
|
|
|
93
|
|
|
$encrypted = $reflection->getProperty('encrypted'); |
94
|
|
|
$encrypted->setAccessible(true); |
95
|
|
|
|
96
|
|
|
$encrypted->setValue($model, ['name']); |
97
|
|
|
|
98
|
|
|
$model->name = 'Test'; |
|
|
|
|
99
|
|
|
|
100
|
|
|
$this->assertEquals(['name' => 'Test'], $model->toArray()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Test getOriginal |
105
|
|
|
*/ |
106
|
|
|
public function testGetOriginal() |
107
|
|
|
{ |
108
|
|
|
$model = new BaseModel(); |
109
|
|
|
|
110
|
|
|
$this->assertEquals([], $model->getOriginal()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Test anonymize |
115
|
|
|
* |
116
|
|
|
* @throws \ReflectionException |
117
|
|
|
*/ |
118
|
|
|
public function testAnonymize() |
119
|
|
|
{ |
120
|
|
|
$model = new BaseModel(); |
121
|
|
|
|
122
|
|
|
$reflection = new ReflectionClass($model); |
123
|
|
|
|
124
|
|
|
$anonymize = $reflection->getProperty('anonymizable'); |
125
|
|
|
$anonymize->setAccessible(true); |
126
|
|
|
|
127
|
|
|
$anonymize->setValue($model, ['name' => ['text'], 'gender' => ['shuffle', 'male', 'female']]); |
128
|
|
|
|
129
|
|
|
$attributes = $reflection->getProperty('attributes'); |
130
|
|
|
$attributes->setAccessible(true); |
131
|
|
|
|
132
|
|
|
$attributes->setValue($model, ['name', 'gender']); |
133
|
|
|
|
134
|
|
|
$model->name = 'Test'; |
|
|
|
|
135
|
|
|
$model->gender = 'male'; |
|
|
|
|
136
|
|
|
|
137
|
|
|
$model->anonymize(); |
138
|
|
|
|
139
|
|
|
$this->assertNotEquals('Test', $model->name); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.