Created
November 22, 2019 16:05
-
-
Save dersonsena/2cbddbd84ac3f3518b23be874d7e8c07 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace Tests\Unit\Domain\User\Behaviors; | |
use Yii; | |
use App\Domain\User\Behaviors\CryptPassword; | |
use App\Domain\User\User; | |
use yii\db\ActiveRecord; | |
use Tests\TestCase; | |
class CryptPasswordTest extends TestCase | |
{ | |
/** | |
* @var CryptPassword | |
*/ | |
private $cryptPassword; | |
/** | |
* @var User | |
*/ | |
private $userToCreate; | |
/** | |
* @var User | |
*/ | |
private $userToUpdate; | |
protected function setUp(): void | |
{ | |
$this->mockApplication(); | |
// Creating a mock user to Create scenario tests | |
$this->userToCreate = $this->getMockBuilder(User::class) | |
->disableOriginalConstructor() | |
->setMethods(['attributes', 'getIsNewRecord']) | |
->getMock(); | |
$this->userToCreate->method('attributes')->willReturn(['password']); | |
$this->userToCreate->method('getIsNewRecord')->willReturn(true); | |
$this->userToCreate->password = '123456'; | |
// Creating a mock user to Update scenario tests | |
$this->userToUpdate = $this->getMockBuilder(User::class) | |
->disableOriginalConstructor() | |
->setMethods(['attributes', 'getIsNewRecord']) | |
->getMock(); | |
$this->userToUpdate->method('attributes')->willReturn(['password']); | |
$this->userToUpdate->method('getIsNewRecord')->willReturn(false); | |
$this->userToUpdate->currentPassword = ''; | |
$this->userToUpdate->password = '$2y$13$YG0ylWkkfnNntry6t4cIbugVzMKAjprKTrtjWa/vJ.XgtYjaJ.qia'; | |
$this->cryptPassword = new CryptPassword(); | |
} | |
public function testWhichEventTypesAreRegistered() | |
{ | |
$events = $this->cryptPassword->events(); | |
$this->assertEquals(3, count($events)); | |
$this->assertContains(ActiveRecord::EVENT_BEFORE_INSERT, array_keys($events)); | |
$this->assertContains(ActiveRecord::EVENT_BEFORE_UPDATE, array_keys($events)); | |
$this->assertContains(ActiveRecord::EVENT_AFTER_FIND, array_keys($events)); | |
} | |
public function testFindWhenIsNewRecord() | |
{ | |
$this->cryptPassword->owner = $this->userToCreate; | |
$this->cryptPassword->find(); | |
$this->assertEquals('', $this->cryptPassword->owner->currentPassword); | |
} | |
public function testFindWhenIsExistingRecord() | |
{ | |
$this->cryptPassword->owner = $this->userToUpdate; | |
$this->cryptPassword->find(); | |
$this->assertEquals($this->cryptPassword->owner->password, $this->cryptPassword->owner->currentPassword); | |
} | |
public function testIfPasswordChangesOnInsert() | |
{ | |
$this->cryptPassword->owner = $this->userToCreate; | |
$this->cryptPassword->insert(); | |
$this->assertEquals(60, strlen($this->cryptPassword->owner->password)); | |
$this->assertTrue(Yii::$app->getSecurity()->validatePassword('123456', $this->cryptPassword->owner->password)); | |
} | |
public function testIfPasswordChangesOnUpdatedWhenPasswordGreaterThan60() | |
{ | |
$this->cryptPassword->owner = $this->userToUpdate; | |
$this->cryptPassword->find(); | |
$this->cryptPassword->update(); | |
$this->assertTrue( | |
Yii::$app->getSecurity()->validatePassword('123456', $this->cryptPassword->owner->password) | |
); | |
} | |
public function testIfPasswordChangesOnUpdatedWhenPasswordLessThan60() | |
{ | |
$this->cryptPassword->owner = $this->userToUpdate; | |
$this->cryptPassword->find(); | |
$this->cryptPassword->owner->password = 'password-changed'; | |
$this->cryptPassword->update(); | |
$this->assertTrue( | |
Yii::$app->getSecurity()->validatePassword('password-changed', $this->cryptPassword->owner->password) | |
); | |
} | |
public function testIfPasswordChangesOnUpdatedWhenPasswordIsEmpty() | |
{ | |
$this->cryptPassword->owner = $this->userToUpdate; | |
$this->cryptPassword->find(); | |
$this->cryptPassword->owner->password = ''; | |
$this->cryptPassword->update(); | |
$this->assertEquals($this->cryptPassword->owner->currentPassword, $this->cryptPassword->owner->password); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment