-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add entity compatible BlameableBehavior with tests
- Loading branch information
1 parent
d973ee0
commit 98935de
Showing
10 changed files
with
194 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace albertborsos\ddd\behaviors; | ||
|
||
use albertborsos\ddd\traits\EvaluateAttributesTrait; | ||
|
||
class BlameableBehavior extends \yii\behaviors\BlameableBehavior | ||
{ | ||
use EvaluateAttributesTrait; | ||
|
||
public $createdByAttribute = 'createdBy'; | ||
|
||
public $updatedByAttribute = 'updatedBy'; | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace albertborsos\ddd\tests\support\base; | ||
|
||
use yii\base\BaseObject; | ||
|
||
class UserMock extends BaseObject | ||
{ | ||
public $id; | ||
public $isGuest = true; | ||
|
||
public function login($id) | ||
{ | ||
$this->isGuest = false; | ||
$this->id = $id; | ||
} | ||
public function logout() | ||
{ | ||
$this->isGuest = true; | ||
$this->id = null; | ||
} | ||
} |
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
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
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
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
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
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
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,135 @@ | ||
<?php | ||
|
||
namespace albertborsos\ddd\tests\behaviors; | ||
|
||
use albertborsos\ddd\repositories\AbstractActiveRepository; | ||
use albertborsos\ddd\tests\fixtures\CustomerWithBehaviorsFixtures; | ||
use albertborsos\ddd\tests\support\base\domains\customer\entities\CustomerWithBehaviors; | ||
use albertborsos\ddd\tests\support\base\domains\customer\mysql\CustomerWithBehaviorsActiveRepository; | ||
use albertborsos\ddd\tests\support\base\domains\customer\mysql\CustomerWithModifiedBehaviorsActiveRepository; | ||
use albertborsos\ddd\tests\support\base\UserMock; | ||
use Codeception\PHPUnit\TestCase; | ||
use Codeception\Util\Debug; | ||
use yii\test\FixtureTrait; | ||
use yii\web\User; | ||
|
||
class BlameableBehaviorTest extends TestCase | ||
{ | ||
use FixtureTrait; | ||
|
||
const DEFAULT_USER_ID = 10; | ||
const UPDATER_USER_ID = 11; | ||
|
||
public function fixtures() | ||
{ | ||
return [ | ||
'customers' => CustomerWithBehaviorsFixtures::class, | ||
]; | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->initFixtures(); | ||
$this->authenticateUser(self::DEFAULT_USER_ID); | ||
} | ||
|
||
public function testInsert() | ||
{ | ||
$data = [ | ||
'id' => 3, | ||
'name' => 'Test blameable attributes are filled on insert', | ||
]; | ||
|
||
/** @var AbstractActiveRepository $repository */ | ||
$repository = \Yii::createObject(CustomerWithBehaviorsActiveRepository::class); | ||
|
||
$entity = $repository->hydrate($data); | ||
$this->assertTrue($repository->insert($entity)); | ||
|
||
/** @var CustomerWithBehaviors $entity */ | ||
$entity = $repository->findOne($data['id']); | ||
$this->assertInstanceOf($repository->getEntityClass(), $entity); | ||
|
||
$this->assertEquals(self::DEFAULT_USER_ID, $entity->createdBy); | ||
$this->assertEquals(self::DEFAULT_USER_ID, $entity->updatedBy); | ||
$this->assertEquals($entity->createdBy, $entity->updatedBy); | ||
} | ||
|
||
public function testUpdate() | ||
{ | ||
$this->authenticateUser(self::UPDATER_USER_ID); | ||
|
||
$data = [ | ||
'id' => 1, | ||
'name' => 'Test updatedBy timestamp attribute is modified on update', | ||
]; | ||
|
||
/** @var AbstractActiveRepository $repository */ | ||
$repository = \Yii::createObject(CustomerWithBehaviorsActiveRepository::class); | ||
/** @var CustomerWithBehaviors $entity */ | ||
$entity = $repository->findOne($data['id']); | ||
|
||
$oldCreatedBy = $entity->createdBy; | ||
$oldUpdatedBy = $entity->updatedBy; | ||
|
||
$entity->setAttributes($data, false); | ||
$this->assertTrue($repository->update($entity)); | ||
|
||
$entity = $repository->findOne($data['id']); | ||
$this->assertInstanceOf($repository->getEntityClass(), $entity); | ||
|
||
$this->assertEquals($oldCreatedBy, $entity->createdBy); | ||
$this->assertEquals(self::UPDATER_USER_ID, $entity->updatedBy); | ||
$this->assertNotEquals($oldUpdatedBy, $entity->updatedBy); | ||
} | ||
|
||
public function testEmptyAttributes() | ||
{ | ||
/** @var AbstractActiveRepository $repository */ | ||
$repository = \Yii::createObject(CustomerWithModifiedBehaviorsActiveRepository::class); | ||
|
||
$data = [ | ||
'id' => 3, | ||
'name' => 'test only createdBy', | ||
]; | ||
|
||
/** @var CustomerWithBehaviors $entity */ | ||
$entity = $repository->hydrate($data); | ||
|
||
$this->assertTrue($repository->insert($entity)); | ||
|
||
/** @var CustomerWithBehaviors $entity */ | ||
$entity = $repository->findOne($data['id']); | ||
$this->assertInstanceOf($repository->getEntityClass(), $entity); | ||
|
||
$this->assertNotEmpty($entity->createdBy); | ||
$this->assertEmpty($entity->updatedBy); | ||
} | ||
|
||
/** | ||
* @expectedException \yii\base\InvalidConfigException | ||
* @expectedExceptionMessageRegExp /must be used with `albertborsos\\ddd\\base\\EntityEvent`$/ | ||
*/ | ||
public function testInvalidEventException() | ||
{ | ||
/** @var CustomerWithModifiedBehaviorsActiveRepository $repository */ | ||
$repository = \Yii::createObject(CustomerWithModifiedBehaviorsActiveRepository::class); | ||
$repository->fakeEventClass = true; | ||
|
||
$data = [ | ||
'id' => 3, | ||
'name' => 'test invalid event exception', | ||
]; | ||
|
||
/** @var CustomerWithBehaviors $entity */ | ||
$entity = $repository->hydrate($data); | ||
|
||
$this->assertTrue($repository->insert($entity)); | ||
} | ||
|
||
private function authenticateUser(int $id) | ||
{ | ||
\Yii::$app->get('user')->login($id); | ||
} | ||
} |
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