Skip to content

Commit

Permalink
add entity compatible BlameableBehavior with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
albertborsos committed Aug 27, 2019
1 parent d973ee0 commit 98935de
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 13 deletions.
14 changes: 14 additions & 0 deletions src/behaviors/BlameableBehavior.php
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';
}
22 changes: 22 additions & 0 deletions tests/_support/base/UserMock.php
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace albertborsos\ddd\tests\support\base\domains\customer\entities;

use albertborsos\ddd\behaviors\BlameableBehavior;
use albertborsos\ddd\behaviors\TimestampBehavior;
use albertborsos\ddd\models\AbstractEntity;

Expand All @@ -25,6 +26,7 @@ public function behaviors()
{
return [
'timestamp' => TimestampBehavior::class,
'blameable' => BlameableBehavior::class,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace albertborsos\ddd\tests\support\base\domains\customer\entities;

use albertborsos\ddd\behaviors\BlameableBehavior;
use albertborsos\ddd\behaviors\TimestampBehavior;
use albertborsos\ddd\interfaces\EntityInterface;
use albertborsos\ddd\models\AbstractEntity;
Expand All @@ -21,6 +22,12 @@ public function behaviors()
EntityInterface::EVENT_BEFORE_INSERT => ['createdAt'],
],
],
'blameable' => [
'class' => BlameableBehavior::class,
'attributes' => [
EntityInterface::EVENT_BEFORE_INSERT => ['createdBy'],
],
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use albertborsos\ddd\tests\support\base\domains\customer\interfaces\CustomerActiveRepositoryInterface;
use yii\data\BaseDataProvider;

class CustomerWithBehaviorActiveRepository extends CustomerActiveRepository
class CustomerWithBehaviorsActiveRepository extends CustomerActiveRepository
{
protected $entityClass = \albertborsos\ddd\tests\support\base\domains\customer\entities\CustomerWithBehaviors::class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use albertborsos\ddd\interfaces\EntityInterface;
use yii\base\ModelEvent;

class CustomerWithModifiedBehaviorActiveRepository extends CustomerActiveRepository
class CustomerWithModifiedBehaviorsActiveRepository extends CustomerActiveRepository
{
public $fakeEventClass = false;

Expand Down
1 change: 1 addition & 0 deletions tests/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'cache' => [
'class' => \yii\caching\FileCache::class,
],
'user' => \albertborsos\ddd\tests\support\base\UserMock::class,
],
'container' => [
'definitions' => [
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/data/customer-with-behaviors.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
'id' => 1,
'name' => 'Albert',
'created_at' => 1,
'created_by' => 100,
'created_by' => 10,
'updated_at' => 1,
'updated_by' => 100,
'updated_by' => 10,
],
[
'id' => 2,
'name' => 'Noncsi',
'created_at' => 1,
'created_by' => 100,
'created_by' => 10,
'updated_at' => 2,
'updated_by' => 101,
'updated_by' => 11,
],
];
135 changes: 135 additions & 0 deletions tests/unit/behaviors/BlameableBehaviorTest.php
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);
}
}
14 changes: 7 additions & 7 deletions tests/unit/behaviors/TimestampBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
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\CustomerWithBehaviorActiveRepository;
use albertborsos\ddd\tests\support\base\domains\customer\mysql\CustomerWithModifiedBehaviorActiveRepository;
use albertborsos\ddd\tests\support\base\domains\customer\mysql\CustomerWithBehaviorsActiveRepository;
use albertborsos\ddd\tests\support\base\domains\customer\mysql\CustomerWithModifiedBehaviorsActiveRepository;
use Codeception\PHPUnit\TestCase;
use yii\test\FixtureTrait;

Expand Down Expand Up @@ -35,7 +35,7 @@ public function testInsert()
];

/** @var AbstractActiveRepository $repository */
$repository = \Yii::createObject(CustomerWithBehaviorActiveRepository::class);
$repository = \Yii::createObject(CustomerWithBehaviorsActiveRepository::class);

$entity = $repository->hydrate($data);
$this->assertTrue($repository->insert($entity));
Expand All @@ -57,7 +57,7 @@ public function testUpdate()
];

/** @var AbstractActiveRepository $repository */
$repository = \Yii::createObject(CustomerWithBehaviorActiveRepository::class);
$repository = \Yii::createObject(CustomerWithBehaviorsActiveRepository::class);
/** @var CustomerWithBehaviors $entity */
$entity = $repository->findOne($data['id']);

Expand All @@ -79,7 +79,7 @@ public function testUpdate()
public function testEmptyAttributes()
{
/** @var AbstractActiveRepository $repository */
$repository = \Yii::createObject(CustomerWithModifiedBehaviorActiveRepository::class);
$repository = \Yii::createObject(CustomerWithModifiedBehaviorsActiveRepository::class);

$data = [
'id' => 3,
Expand All @@ -105,8 +105,8 @@ public function testEmptyAttributes()
*/
public function testInvalidEventException()
{
/** @var CustomerWithModifiedBehaviorActiveRepository $repository */
$repository = \Yii::createObject(CustomerWithModifiedBehaviorActiveRepository::class);
/** @var CustomerWithModifiedBehaviorsActiveRepository $repository */
$repository = \Yii::createObject(CustomerWithModifiedBehaviorsActiveRepository::class);
$repository->fakeEventClass = true;

$data = [
Expand Down

0 comments on commit 98935de

Please sign in to comment.