Last active
January 8, 2021 21:41
-
-
Save owenconti/8d5030cef94985bb9a2c36355a1eef24 to your computer and use it in GitHub Desktop.
Storing and testing encrypted values in Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
... | |
use Sagalbot\Encryptable\Encryptable; | |
class User extends Authenticatable | |
{ | |
use Encryptable; | |
protected $encryptable = ['secret_token']; | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Store value | |
$user = User::create([ | |
'secret_token' => 'abc123' | |
]); | |
// Access directly | |
$user->secret_token // outputs 'abc123' | |
// Access via toArray()/toJson() | |
$user->toArray() // outputs [ ..., 'secret_token' => 'abc123' ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** @test */ | |
public function it_stores_users_secrets() | |
{ | |
// Given | |
$user = factory(User::class)->create([ | |
'secret_token' => encrypt('api-key') | |
]); | |
// Then | |
$this->assertEncrypted('users', ['id' => $user->id], [ | |
'secret_token' => 'api-key' | |
]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests; | |
use OhSeeSoftware\LaravelAssertEncrypted\Traits\AssertEncrypted; | |
class SomeTest extends TestCase | |
{ | |
use AssertEncrypted; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment