Skip to content

Commit

Permalink
Custom tables table test (#2755)
Browse files Browse the repository at this point in the history
Co-authored-by: Nghiem <[email protected]>
  • Loading branch information
nghiem-mb and nghiemnv1205 authored Sep 25, 2023
1 parent 48fb9d9 commit 8d9ce6e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
10 changes: 10 additions & 0 deletions plugins/bc-custom-content/src/Model/Table/CustomTablesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CustomTablesTable extends AppTable
* @param array $config
* @checked
* @noTodo
* @unitTest
*/
public function initialize(array $config): void
{
Expand All @@ -51,6 +52,9 @@ public function initialize(array $config): void

/**
* ツリー構造形式の関連フィールドを hasMany で設定する
* @checked
* @noTodo
* @unitTest
*/
public function setHasManyLinksByThreaded()
{
Expand All @@ -65,6 +69,9 @@ public function setHasManyLinksByThreaded()

/**
* ツリー構造形式ではない通常一覧の関連フィールドを hasMany で設定する
* @checked
* @noTodo
* @unitTest
*/
public function setHasManyLinksByAll()
{
Expand All @@ -81,6 +88,9 @@ public function setHasManyLinksByAll()
*
* @param Validator $validator
* @return Validator
* @checked
* @noTodo
* @unitTest
*/
public function validationDefault(Validator $validator): Validator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
namespace BcCustomContent\Test\TestCase\Model\Table;

use BaserCore\TestSuite\BcTestCase;
use BcCustomContent\Model\Table\CustomTablesTable;

/**
* CustomTablesTableTest
* @property CustomTablesTable $CustomTablesTable
*/
class CustomTablesTableTest extends BcTestCase
{
Expand All @@ -25,6 +27,7 @@ class CustomTablesTableTest extends BcTestCase
public function setUp(): void
{
parent::setUp();
$this->CustomTablesTable = new CustomTablesTable();
}

/**
Expand All @@ -35,4 +38,74 @@ public function tearDown(): void
parent::tearDown();
}

/**
* test initialize
*/
public function test_initialize()
{
$this->assertTrue($this->CustomTablesTable->hasBehavior('Timestamp'));
$this->assertTrue($this->CustomTablesTable->hasAssociation('CustomEntries'));
$this->assertTrue($this->CustomTablesTable->hasAssociation('CustomContents'));
$this->assertTrue($this->CustomTablesTable->hasAssociation('CustomLinks'));
}

/**
* test setHasManyLinksByThreaded
*/
public function test_setHasManyLinksByThreaded()
{
$association = $this->CustomTablesTable->getAssociation('CustomLinks');
$this->assertEquals('threaded', $association->getFinder());
$this->assertEquals('custom_table_id', $association->getForeignKey());
$this->assertEquals(['CustomLinks.lft' => 'ASC'], $association->getSort());
}

/**
* test setHasManyLinksByAll
*/
public function test_setHasManyLinksByAll()
{
$this->CustomTablesTable->setHasManyLinksByAll();
$association = $this->CustomTablesTable->getAssociation('CustomLinks');
$this->assertEquals('all', $association->getFinder());
$this->assertEquals('custom_table_id', $association->getForeignKey());
$this->assertEquals(['CustomLinks.lft' => 'ASC'], $association->getSort());
}

/**
* test validationDefault
*/
public function test_validationDefault()
{
$validator = $this->CustomTablesTable->getValidator('default');
//入力フィールドのデータが超えた場合、
$errors = $validator->validate([
'name' => str_repeat('a', 256),
'title' => str_repeat('a', 256),
]);
//戻り値を確認
$this->assertEquals('255文字以内で入力してください。', current($errors['name']));
$this->assertEquals('255文字以内で入力してください。', current($errors['title']));
//入力フィールドのデータがない
$errors = $validator->validate([
'name' => '',
'title' => '',
]);
$this->assertEquals('識別名を入力してください。', current($errors['name']));
$this->assertEquals('タイトルを入力してください。', current($errors['title']));
//入力フィールドのデータが異常な記号含める
$errors = $validator->validate([
'name' => '',
]);
$this->assertEquals('識別名は半角英数字とアンダースコアのみで入力してください。', current($errors['name']));
//正常系実行
$errors = $validator->validate([
'name' => 'test',
'title' => 'test',
]);
$this->assertEmpty($errors);
}



}

0 comments on commit 8d9ce6e

Please sign in to comment.