-
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 tests for AbstractUniqueSluggableBehavior
- Loading branch information
1 parent
9bcf354
commit 0b55621
Showing
34 changed files
with
1,267 additions
and
3 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
36 changes: 36 additions & 0 deletions
36
tests/_support/base/domains/page/behaviors/PageSluggableBehavior.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,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]] : []], | ||
]; | ||
} | ||
} |
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,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 []; | ||
} | ||
} |
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,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, | ||
]; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/_support/base/domains/page/interfaces/PageActiveRepositoryInterface.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,10 @@ | ||
<?php | ||
|
||
namespace albertborsos\ddd\tests\support\base\domains\page\interfaces; | ||
|
||
use albertborsos\ddd\interfaces\ActiveRepositoryInterface; | ||
|
||
interface PageActiveRepositoryInterface extends ActiveRepositoryInterface | ||
{ | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
tests/_support/base/domains/page/interfaces/PageSlugActiveRepositoryInterface.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,10 @@ | ||
<?php | ||
|
||
namespace albertborsos\ddd\tests\support\base\domains\page\interfaces; | ||
|
||
use albertborsos\ddd\interfaces\ActiveRepositoryInterface; | ||
|
||
interface PageSlugActiveRepositoryInterface extends ActiveRepositoryInterface | ||
{ | ||
|
||
} |
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,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
80
tests/_support/base/domains/page/mysql/PageActiveRepository.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,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; | ||
} | ||
} |
Oops, something went wrong.