Skip to content
This repository was archived by the owner on Mar 2, 2023. It is now read-only.

Commit a0c8b76

Browse files
committed
master - fix env and added more unit tests
1 parent d8a1bea commit a0c8b76

File tree

6 files changed

+138
-18
lines changed

6 files changed

+138
-18
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ before_script:
1010
- composer install --no-interaction
1111

1212
script:
13-
- vendor/bin/phpunit --bootstrap vendor/autoload.php
13+
- vendor/bin/phpunit

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@
1515
<directory>tests</directory>
1616
</testsuite>
1717
</testsuites>
18+
<filter>
19+
<whitelist processUncoveredFilesFromWhitelist="true">
20+
<directory suffix=".php">./src</directory>
21+
</whitelist>
22+
</filter>
23+
<logging>
24+
<log type="coverage-html" target="coverage" lowUpperBound="35" highLowerBound="70"/>
25+
</logging>
1826
<php>
19-
<server name="ENCRYPTION_KEY" value="secret16charlong"/>
20-
<server name="FAKER_LOCALE" value="en_US"/>
27+
<env name="ENCRYPTION_KEY" value="secret16charlong"/>
28+
<env name="FAKER_LOCALE" value="en_US"/>
2129
</php>
2230
</phpunit>

src/Models/BaseModel.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function getAttribute($key)
6868
*/
6969
protected function aesDecrypt($val, $cypher = 'aes-128-ecb', $mySqlKey = true)
7070
{
71-
$secret = env('ENCRYPTION_KEY');
71+
$secret = getenv('ENCRYPTION_KEY');
7272

7373
$key = $mySqlKey ? $this->generateMysqlAesKey($secret) : $secret;
7474

@@ -120,7 +120,7 @@ public function setAttribute($key, $value)
120120
*/
121121
protected function aesEncrypt($val, $cypher = 'aes-128-ecb', $mySqlKey = true)
122122
{
123-
$secret = env('ENCRYPTION_KEY');
123+
$secret = getenv('ENCRYPTION_KEY');
124124

125125
$key = $mySqlKey ? $this->generateMysqlAesKey($secret) : $secret;
126126

@@ -193,7 +193,7 @@ public function getAnonymizable()
193193
*/
194194
public function scopeWhereEncrypted(Builder $query, $field, $value)
195195
{
196-
return $query->whereRaw('AES_DECRYPT(' . $field . ', "' . env("ENCRYPTION_KEY") . '") LIKE "' . $value . '" COLLATE utf8mb4_general_ci');
196+
return $query->whereRaw('AES_DECRYPT(' . $field . ', "' . getenv("ENCRYPTION_KEY") . '") LIKE "' . $value . '" COLLATE utf8mb4_general_ci');
197197
}
198198

199199
/**
@@ -207,7 +207,7 @@ public function scopeWhereEncrypted(Builder $query, $field, $value)
207207
*/
208208
public function scopeOrWhereEncrypted(Builder $query, $field, $value)
209209
{
210-
return $query->orWhereRaw('AES_DECRYPT(' . $field . ', "' . env("ENCRYPTION_KEY") . '") LIKE "' . $value . '" COLLATE utf8mb4_general_ci');
210+
return $query->orWhereRaw('AES_DECRYPT(' . $field . ', "' . getenv("ENCRYPTION_KEY") . '") LIKE "' . $value . '" COLLATE utf8mb4_general_ci');
211211
}
212212

213213
/**
@@ -217,11 +217,17 @@ public function scopeOrWhereEncrypted(Builder $query, $field, $value)
217217
*/
218218
public function anonymize($locale = null)
219219
{
220-
$faker = Factory::create($locale ?? env('FAKER_LOCALE', Factory::DEFAULT_LOCALE));
220+
$faker = Factory::create($locale ?? (getenv('FAKER_LOCALE') ?? Factory::DEFAULT_LOCALE));
221221

222222
foreach ($this->anonymizable as $field => $type) {
223223
if (in_array($field, $this->attributes)) {
224-
$this->$field = call_user_func([$faker, $type[0]], array_slice($type, 1));
224+
$method = $type[0];
225+
226+
if (count($type) > 1) {
227+
$this->$field = call_user_func([$faker, $method], array_slice($type, 1));
228+
} else {
229+
$this->$field = $faker->$method;
230+
}
225231
}
226232
}
227233
}

src/MysqlEncryptionServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private function addValidators()
3636

3737
$field = isset($parameters[1]) ? $parameters[1] : $attribute;
3838

39-
$items = DB::select("SELECT count(*) as aggregate FROM `" . $parameters[0] . "` WHERE AES_DECRYPT(`" . $field . "`, '" . env("ENCRYPTION_KEY") . "') LIKE '" . $value . "' COLLATE utf8mb4_general_ci");
39+
$items = DB::select("SELECT count(*) as aggregate FROM `" . $parameters[0] . "` WHERE AES_DECRYPT(`" . $field . "`, '" . getenv("ENCRYPTION_KEY") . "') LIKE '" . $value . "' COLLATE utf8mb4_general_ci");
4040

4141
return $items[0]->aggregate === 0;
4242
});
@@ -51,7 +51,7 @@ private function addValidators()
5151

5252
$field = isset($parameters[1]) ? $parameters[1] : $attribute;
5353

54-
$items = DB::select("SELECT count(*) as aggregate FROM `" . $parameters[0] . "` WHERE AES_DECRYPT(`" . $field . "`, '" . env("ENCRYPTION_KEY") . "') LIKE '" . $value . "' COLLATE utf8mb4_general_ci");
54+
$items = DB::select("SELECT count(*) as aggregate FROM `" . $parameters[0] . "` WHERE AES_DECRYPT(`" . $field . "`, '" . getenv("ENCRYPTION_KEY") . "') LIKE '" . $value . "' COLLATE utf8mb4_general_ci");
5555

5656
return $items[0]->aggregate > 0;
5757
});

tests/Models/BaseModelTest.php

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use IonGhitun\MysqlEncryption\Models\BaseModel;
77
use PHPUnit\Framework\TestCase;
8+
use ReflectionClass;
89

910
/**
1011
* Class BaseModelTest
@@ -20,7 +21,7 @@ public function testGetAnonymizable()
2021
{
2122
$model = new BaseModel();
2223

23-
$this->assertEquals($model->getAnonymizable(), []);
24+
$this->assertEquals([], $model->getAnonymizable());
2425
}
2526

2627
/**
@@ -30,6 +31,111 @@ public function testGetEncrypted()
3031
{
3132
$model = new BaseModel();
3233

33-
$this->assertEquals($model->getEncrypted(), []);
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);
34140
}
35141
}

0 commit comments

Comments
 (0)