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
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class BaseModelTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Test getAnonymizable |
17
|
|
|
*/ |
18
|
|
|
public function testGetAnonymizable(): void |
19
|
|
|
{ |
20
|
|
|
$model = new BaseModel(); |
21
|
|
|
|
22
|
|
|
$this->assertEquals([], $model->getAnonymizable()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test getEncrypted |
27
|
|
|
*/ |
28
|
|
|
public function testGetEncrypted(): void |
29
|
|
|
{ |
30
|
|
|
$model = new BaseModel(); |
31
|
|
|
|
32
|
|
|
$this->assertEquals([], $model->getEncrypted()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Test getAttribute |
37
|
|
|
*/ |
38
|
|
|
public function tesBaseGetAttribute(): void |
39
|
|
|
{ |
40
|
|
|
$model = new BaseModel(); |
41
|
|
|
|
42
|
|
|
$model->name = 'Test'; |
43
|
|
|
|
44
|
|
|
$this->assertEquals('Test', $model->name); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Test getAttribute |
49
|
|
|
*/ |
50
|
|
|
public function testEncryptedGetAttribute(): void |
51
|
|
|
{ |
52
|
|
|
$model = new BaseModel(); |
53
|
|
|
|
54
|
|
|
$reflection = new ReflectionClass($model); |
55
|
|
|
|
56
|
|
|
$encrypted = $reflection->getProperty('encrypted'); |
57
|
|
|
$encrypted->setAccessible(true); |
58
|
|
|
|
59
|
|
|
$encrypted->setValue($model, ['name']); |
60
|
|
|
|
61
|
|
|
$model->name = 'Test'; |
62
|
|
|
|
63
|
|
|
$this->assertEquals('Test', $model->name); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Test toArray |
68
|
|
|
*/ |
69
|
|
|
public function testToArray(): void |
70
|
|
|
{ |
71
|
|
|
$model = new BaseModel(); |
72
|
|
|
|
73
|
|
|
$model->name = 'Test'; |
74
|
|
|
|
75
|
|
|
$this->assertEquals(['name' => 'Test'], $model->toArray()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test toArray |
80
|
|
|
*/ |
81
|
|
|
public function testEncryptedToArray(): void |
82
|
|
|
{ |
83
|
|
|
$model = new BaseModel(); |
84
|
|
|
|
85
|
|
|
$reflection = new ReflectionClass($model); |
86
|
|
|
|
87
|
|
|
$encrypted = $reflection->getProperty('encrypted'); |
88
|
|
|
$encrypted->setAccessible(true); |
89
|
|
|
|
90
|
|
|
$encrypted->setValue($model, ['name']); |
91
|
|
|
|
92
|
|
|
$model->name = 'Test'; |
93
|
|
|
|
94
|
|
|
$this->assertEquals(['name' => 'Test'], $model->toArray()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Test getOriginal |
99
|
|
|
*/ |
100
|
|
|
public function testGetOriginal(): void |
101
|
|
|
{ |
102
|
|
|
$model = new BaseModel(); |
103
|
|
|
|
104
|
|
|
$this->assertEquals([], $model->getOriginal()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Test anonymize |
109
|
|
|
*/ |
110
|
|
|
public function testAnonymize(): void |
111
|
|
|
{ |
112
|
|
|
$model = new BaseModel(); |
113
|
|
|
|
114
|
|
|
$reflection = new ReflectionClass($model); |
115
|
|
|
|
116
|
|
|
$anonymize = $reflection->getProperty('anonymizable'); |
117
|
|
|
$anonymize->setAccessible(true); |
118
|
|
|
|
119
|
|
|
$anonymize->setValue($model, ['name' => ['text'], 'gender' => ['shuffle', 'male', 'female']]); |
120
|
|
|
|
121
|
|
|
$attributes = $reflection->getProperty('attributes'); |
122
|
|
|
$attributes->setAccessible(true); |
123
|
|
|
|
124
|
|
|
$attributes->setValue($model, ['name', 'gender']); |
125
|
|
|
|
126
|
|
|
$model->name = 'Test'; |
127
|
|
|
$model->gender = 'male'; |
128
|
|
|
|
129
|
|
|
$model->anonymize(); |
130
|
|
|
|
131
|
|
|
$this->assertNotEquals('Test', $model->name); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|