forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[10.x] Add 'hashed' cast (laravel#46947)
* Add 'hashed' cast * Fix linting issues
- Loading branch information
1 parent
51251d4
commit 1c783fe
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
91 changes: 91 additions & 0 deletions
91
tests/Integration/Database/EloquentModelHashedCastingTest.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database; | ||
|
||
use Illuminate\Contracts\Hashing\Hasher; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class EloquentModelHashedCastingTest extends DatabaseTestCase | ||
{ | ||
protected $hasher; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->hasher = $this->mock(Hasher::class); | ||
Hash::swap($this->hasher); | ||
} | ||
|
||
protected function defineDatabaseMigrationsAfterDatabaseRefreshed() | ||
{ | ||
Schema::create('hashed_casts', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('password')->nullable(); | ||
}); | ||
} | ||
|
||
public function testHashed() | ||
{ | ||
$this->hasher->expects('needsRehash') | ||
->with('this is a password') | ||
->andReturnTrue(); | ||
|
||
$this->hasher->expects('make') | ||
->with('this is a password') | ||
->andReturn('hashed-password'); | ||
|
||
$subject = HashedCast::create([ | ||
'password' => 'this is a password', | ||
]); | ||
|
||
$this->assertSame('hashed-password', $subject->password); | ||
$this->assertDatabaseHas('hashed_casts', [ | ||
'id' => $subject->id, | ||
'password' => 'hashed-password', | ||
]); | ||
} | ||
|
||
public function testNotHashedIfAlreadyHashed() | ||
{ | ||
$this->hasher->expects('needsRehash') | ||
->with('already-hashed-password') | ||
->andReturnFalse(); | ||
|
||
$subject = HashedCast::create([ | ||
'password' => 'already-hashed-password', | ||
]); | ||
|
||
$this->assertSame('already-hashed-password', $subject->password); | ||
$this->assertDatabaseHas('hashed_casts', [ | ||
'id' => $subject->id, | ||
'password' => 'already-hashed-password', | ||
]); | ||
} | ||
|
||
public function testNotHashedIfNull() | ||
{ | ||
$subject = HashedCast::create([ | ||
'password' => null, | ||
]); | ||
|
||
$this->assertNull($subject->password); | ||
$this->assertDatabaseHas('hashed_casts', [ | ||
'id' => $subject->id, | ||
'password' => null, | ||
]); | ||
} | ||
} | ||
|
||
class HashedCast extends Model | ||
{ | ||
public $timestamps = false; | ||
protected $guarded = []; | ||
|
||
public $casts = [ | ||
'password' => 'hashed', | ||
]; | ||
} |