Skip to content

Commit

Permalink
add tests for AbstractUniqueSluggableBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
albertborsos committed Aug 29, 2019
1 parent 9bcf354 commit 0b55621
Show file tree
Hide file tree
Showing 34 changed files with 1,267 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/behaviors/SluggableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use albertborsos\ddd\base\EntityEvent;
use albertborsos\ddd\interfaces\ActiveRepositoryInterface;
use albertborsos\ddd\interfaces\EntityInterface;
use albertborsos\ddd\interfaces\RepositoryInterface;
use albertborsos\ddd\traits\EvaluateAttributesTrait;
use Yii;
use yii\base\InvalidConfigException;
Expand Down Expand Up @@ -129,7 +128,7 @@ protected function validateSlug($slug)
$this->uniqueValidator
));

$model = Yii::createObject($validator->targetClass);
$model = Yii::createObject($this->getRepository()->getDataModelClass());
$model->clearErrors();
$model->{$this->slugAttribute} = $slug;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace albertborsos\ddd\tests\support\base\domains\page\behaviors;

use albertborsos\ddd\behaviors\AbstractUniqueSluggableBehavior;
use albertborsos\ddd\tests\support\base\domains\page\interfaces\PageActiveRepositoryInterface;
use albertborsos\ddd\tests\support\base\domains\page\interfaces\PageSlugActiveRepositoryInterface;

class PageSluggableBehavior extends AbstractUniqueSluggableBehavior
{
public $ensureUnique = true;

public $repository = PageSlugActiveRepositoryInterface::class;

/**
* List of \yii\validators\UniqueValidator configurations to validate the uniqueness of a slug in multiple repositories.
* The value of `targetClass` property is ridden from the actual repository.
*
* ```php
* [
* PageActiveRepositoryInterface::class => ['targetAttribute' => 'slug', 'filter' => $this->owner->id ? ['NOT', ['id' => $this->owner->id]] : []],
* PageSlugActiveRepositoryInterface::class => ['targetAttribute' => 'slug', 'filter' => $this->owner->id ? ['NOT', ['page_id' => $this->owner->id]] : []],
* ]
* ```
*
* @return array
* @var array
*/
protected function uniqueValidators()
{
return [
PageActiveRepositoryInterface::class => ['targetAttribute' => 'slug', 'filter' => $this->owner->id ? ['NOT', ['id' => $this->owner->id]] : []],
PageSlugActiveRepositoryInterface::class => ['targetAttribute' => 'slug', 'filter' => $this->owner->id ? ['NOT', ['page_id' => $this->owner->id]] : []],
];
}
}
94 changes: 94 additions & 0 deletions tests/_support/base/domains/page/entities/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

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

use albertborsos\ddd\behaviors\BlameableBehavior;
use albertborsos\ddd\behaviors\TimestampBehavior;
use albertborsos\ddd\behaviors\AbstractUniqueSluggableBehavior;
use albertborsos\ddd\models\AbstractEntity;
use albertborsos\ddd\tests\support\base\domains\page\behaviors\PageSluggableBehavior;
use albertborsos\ddd\tests\support\base\domains\page\interfaces\PageSlugActiveRepositoryInterface;
use mito\cms\core\traits\EntityCacheSyncTrait;
use albertborsos\ddd\tests\support\base\domains\page\interfaces\PageActiveRepositoryInterface;
use yii\base\Event;
use yii\helpers\Inflector;
use yii\helpers\Url;

/**
* Class Page
* @package albertborsos\ddd\tests\support\base\domains\page\entities
*/
class Page extends AbstractEntity
{
const SLUG_POSTFIX = '-latvanyterv';

const STATUSES = [
self::STATUS_HIDDEN => 'Hidden',
self::STATUS_VISIBLE => 'Visible',
];

const STATUS_HIDDEN = 0;
const STATUS_VISIBLE = 1;

public $id;
public $name;
public $category;
public $title;
public $description;
public $date;
public $slug;
public $sortOrder;
public $createdAt;
public $createdBy;
public $updatedAt;
public $updatedBy;
public $status;

public function behaviors()
{
return [
'timestamp' => TimestampBehavior::class,
'blameable' => BlameableBehavior::class,
'sluggable' => [
'class' => PageSluggableBehavior::class,
'value' => function ($event) {
return Inflector::slug($this->name . self::SLUG_POSTFIX);
},
],
];
}

public function fields()
{
return [
'id',
'name',
'category',
'title',
'description',
'date',
'slug',
'sortOrder',
'createdAt',
'createdBy',
'updatedAt',
'updatedBy',
'status',
];
}

public function getUrl()
{
return Url::to(['/' . $this->slug]);
}

/**
* Mapping of property keys to entity classnames.
*
* @return array
*/
public function relationMapping(): array
{
return [];
}
}
70 changes: 70 additions & 0 deletions tests/_support/base/domains/page/entities/PageSlug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

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

use albertborsos\ddd\behaviors\BlameableBehavior;
use albertborsos\ddd\behaviors\TimestampBehavior;
use albertborsos\ddd\models\AbstractEntity;
use mito\cms\core\traits\EntityCacheSyncTrait;
use albertborsos\ddd\tests\support\base\domains\page\interfaces\PageSlugActiveRepositoryInterface;
use yii\base\Event;

/**
* Class PageSlug
* @package albertborsos\ddd\tests\support\base\domains\page\entities
*/
class PageSlug extends AbstractEntity
{
public $id;
public $pageId;
public $slug;
public $createdAt;
public $createdBy;
public $updatedAt;
public $updatedBy;
public $status;

/** @var Page */
public $page;

public function behaviors()
{
return [
'timestamp' => TimestampBehavior::class,
'blameable' => BlameableBehavior::class,
];
}

public function fields()
{
return [
'id',
'pageId',
'slug',
'createdAt',
'createdBy',
'updatedAt',
'updatedBy',
'status',
];
}

public function extraFields()
{
return [
'page',
];
}

/**
* Mapping of property keys to entity classnames.
*
* @return array
*/
public function relationMapping(): array
{
return [
'page' => Page::class,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace albertborsos\ddd\tests\support\base\domains\page\interfaces;

use albertborsos\ddd\interfaces\ActiveRepositoryInterface;

interface PageActiveRepositoryInterface extends ActiveRepositoryInterface
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace albertborsos\ddd\tests\support\base\domains\page\interfaces;

use albertborsos\ddd\interfaces\ActiveRepositoryInterface;

interface PageSlugActiveRepositoryInterface extends ActiveRepositoryInterface
{

}
59 changes: 59 additions & 0 deletions tests/_support/base/domains/page/mysql/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace albertborsos\ddd\tests\support\base\domains\page\mysql;

use albertborsos\ddd\tests\support\base\domains\page\traits\PageAttributeLabelsTrait;

/**
* This is the model class for table "{{%page}}".
*
* @property int $id
* @property string $name
* @property string $category
* @property string $title
* @property string $description
* @property string $date
* @property string $slug
* @property int $sort_order
* @property int $created_at
* @property int $created_by
* @property int $updated_at
* @property int $updated_by
* @property int $status
*
* @property PageImage[] $pageImages
*/
class Page extends \yii\db\ActiveRecord
{
use PageAttributeLabelsTrait;

/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%page}}';
}

/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['description'], 'string'],
[['date'], 'safe'],
[['created_at', 'created_by', 'updated_at', 'updated_by', 'sort_order', 'status'], 'integer'],
[['name', 'category', 'title', 'slug'], 'string', 'max' => 255],
];
}

/**
* {@inheritdoc}
* @return PageQuery the active query used by this AR class.
*/
public static function find()
{
return new PageQuery(get_called_class());
}
}
80 changes: 80 additions & 0 deletions tests/_support/base/domains/page/mysql/PageActiveRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace albertborsos\ddd\tests\support\base\domains\page\mysql;

use albertborsos\ddd\repositories\AbstractActiveRepository;
use albertborsos\ddd\tests\support\base\domains\page\entities\Page;
use mito\cms\core\data\ActiveEntityDataProvider;
use albertborsos\ddd\tests\support\base\domains\page\interfaces\PageActiveRepositoryInterface;
use yii\data\BaseDataProvider;

class PageActiveRepository extends AbstractActiveRepository implements PageActiveRepositoryInterface
{
protected $dataModelClass = \albertborsos\ddd\tests\support\base\domains\page\mysql\Page::class;

protected $entityClass = \albertborsos\ddd\tests\support\base\domains\page\entities\Page::class;

/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @param string $formName
* @return ActiveEntityDataProvider
* @throws \yii\base\InvalidConfigException
*/
public function search($params, $formName = null): BaseDataProvider
{
$query = $this->find();

if ($params['expand'] ?? false) {
$query->with(explode(',', $params['expand']));
}

// add conditions that should always apply here

$dataProvider = new ActiveEntityDataProvider([
'entityClass' => $this->entityClass,
'hydrator' => $this->hydrator,
'query' => $query,
'pagination' => [
'params' => $params,
],
'sort' => [
'defaultOrder' => [
'sort_order' => SORT_ASC,
],
'params' => $params,
],
]);

$model = \Yii::createObject($this->dataModelClass);

$model->load($params, $formName);

if (!$model->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}

// grid filtering conditions
$query->andFilterWhere([
'id' => $model->id,
'date' => $model->date,
'created_at' => $model->created_at,
'created_by' => $model->created_by,
'updated_at' => $model->updated_at,
'updated_by' => $model->updated_by,
'status' => $model->status,
]);

$query->andFilterWhere(['like', 'name', $model->name])
->andFilterWhere(['like', 'category', $model->category])
->andFilterWhere(['like', 'title', $model->title])
->andFilterWhere(['like', 'description', $model->description])
->andFilterWhere(['like', 'slug', $model->slug]);

return $dataProvider;
}
}

0 comments on commit 0b55621

Please sign in to comment.