Skip to content
This repository was archived by the owner on Apr 7, 2021. It is now read-only.

Commit 44c878c

Browse files
committedNov 26, 2019
new unit tests that bypass eloquent and validate raw db values re: #30 & #35
1 parent b31bce8 commit 44c878c

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed
 

‎tests/Models/RealModel.php

+39-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,43 @@
1515
*/
1616
abstract class RealModel extends \Illuminate\Database\Eloquent\Model
1717
{
18-
//
18+
public function getRawValues(array $columns = null): ?array
19+
{
20+
if (!property_exists($this, 'exists') || !is_bool($this->exists) || $this->exists !== true) {
21+
return null;
22+
} else if (!property_exists($this, 'primaryKey') || !is_string($this->primaryKey)) {
23+
return null;
24+
} else if (!array_key_exists($this->primaryKey, $this->attributes) || !is_int($this->{$this->primaryKey})) {
25+
return null;
26+
} else if (is_null($columns)) {
27+
$columns = ['*'];
28+
}
29+
30+
$query = sprintf(
31+
'SELECT %s FROM %s WHERE %s = %u LIMIT 1',
32+
implode(', ', $columns),
33+
$this->table,
34+
$this->primaryKey,
35+
$this->{$this->primaryKey}
36+
);
37+
38+
try {
39+
$dsn = sprintf('mysql:dbname=%s;host=%s;charset=utf8', \DB::getDatabaseName(), env('TESTING_DB_HOST', '127.0.0.1'));
40+
$connection = new \PDO($dsn, env('TESTING_DB_USER', 'root'), env('TESTING_DB_PASS', ''));
41+
$statement = $connection->prepare($query);
42+
43+
$statement->execute();
44+
45+
$result = $statement->setFetchMode(\PDO::FETCH_ASSOC);
46+
$rows = is_bool($result) && $result === true ? $statement->fetchAll() : null;
47+
} catch (\PDOException $exception) {
48+
return null;
49+
} finally {
50+
$connnection = null;
51+
52+
unset($connection);
53+
}
54+
55+
return count($rows) === 1 && count($rows[0]) === count($columns) ? $rows[0] : null;
56+
}
1957
}

‎tests/Traits/DatabaseTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,38 @@ public function testCreate()
2525
$this->assertTrue($model->exists);
2626
}
2727

28+
public function testCreateShouldBeEncryptedRaw()
29+
{
30+
$model = DatabaseModel::create($this->randomValues());
31+
32+
$this->assertTrue($model->exists);
33+
34+
$values = $model->getRawValues(['id', 'should_be_encrypted']);
35+
36+
$this->assertNotNull($values);
37+
$this->assertTrue(is_array($values));
38+
$this->assertArrayHasKey('id', $values);
39+
$this->assertArrayHasKey('should_be_encrypted', $values);
40+
$this->assertTrue(is_string($values['should_be_encrypted']));
41+
$this->assertStringStartsWith(chr(1) . chr(2) . '__LARAVEL-DATABASE-ENCRYPTED-VERSION-', $values['should_be_encrypted']);
42+
}
43+
44+
public function testCreateShouldntBeEncryptedRaw()
45+
{
46+
$model = DatabaseModel::create($this->randomValues());
47+
48+
$this->assertTrue($model->exists);
49+
50+
$values = $model->getRawValues(['id', 'shouldnt_be_encrypted']);
51+
52+
$this->assertNotNull($values);
53+
$this->assertTrue(is_array($values));
54+
$this->assertArrayHasKey('id', $values);
55+
$this->assertArrayHasKey('shouldnt_be_encrypted', $values);
56+
$this->assertTrue(is_string($values['shouldnt_be_encrypted']));
57+
$this->assertStringStartsNotWith(chr(1) . chr(2) . '__LARAVEL-DATABASE-ENCRYPTED-VERSION-', $values['shouldnt_be_encrypted']);
58+
}
59+
2860
public function testUpdate()
2961
{
3062
$model = DatabaseModel::create($this->randomValues());

0 commit comments

Comments
 (0)
This repository has been archived.