Skip to content

Instantly share code, notes, and snippets.

@owenconti
Last active January 8, 2021 21:41
Show Gist options
  • Save owenconti/8d5030cef94985bb9a2c36355a1eef24 to your computer and use it in GitHub Desktop.
Save owenconti/8d5030cef94985bb9a2c36355a1eef24 to your computer and use it in GitHub Desktop.
Storing and testing encrypted values in Laravel
<?php
...
use Sagalbot\Encryptable\Encryptable;
class User extends Authenticatable
{
use Encryptable;
protected $encryptable = ['secret_token'];
...
}
<?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' ]
<?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'
]);
}
<?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