Skip to content

Commit

Permalink
BlogCommentsTable::validationDefault() のユニットテスト (#3379)
Browse files Browse the repository at this point in the history
Co-authored-by: Đỗ Văn Hùng <[email protected]>
  • Loading branch information
HungDV2022 and dovanhung authored Apr 29, 2024
1 parent e32b8fe commit 86a35df
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 57 deletions.
1 change: 1 addition & 0 deletions plugins/bc-blog/src/Model/Table/BlogCommentsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class BlogCommentsTable extends BlogAppTable
* @return Validator
* @checked
* @noTodo
* @unitTest
*/
public function validationDefault(Validator $validator): Validator
{
Expand Down
88 changes: 31 additions & 57 deletions plugins/bc-blog/tests/TestCase/Model/BlogCommentsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,79 +57,53 @@ public function test_initialize()
/*
* validate
*/
public function test空チェック()
public function test_validationDefaultEmptyCheck()
{
$this->markTestIncomplete('こちらのテストはまだ未確認です');
$this->BlogComment->create([
'BlogComment' => [
'name' => '',
'message' => '',
]
$validator = $this->BlogCommentsTable->getValidator('default');
$errors = $validator->validate([
'name' => '',
'message' => ''
]);

$this->assertFalse($this->BlogComment->validates());

$this->assertArrayHasKey('name', $this->BlogComment->validationErrors);
$this->assertEquals('お名前を入力してください。', current($this->BlogComment->validationErrors['name']));

$this->assertArrayHasKey('message', $this->BlogComment->validationErrors);
$this->assertEquals('コメントを入力してください。', current($this->BlogComment->validationErrors['message']));
$this->assertEquals('お名前を入力してください。', current($errors['name']));
$this->assertEquals('コメントを入力してください。', current($errors['message']));
}

public function test桁数チェック異常系()
public function test_validationDefaultMaxLength()
{
$this->markTestIncomplete('こちらのテストはまだ未確認です');
$this->BlogComment->create([
'BlogComment' => [
'name' => '123456789012345678901234567890123456789012345678901',
'email' => '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456@example.com',
'url' => 'http://example.com/1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456',
]
$validator = $this->BlogCommentsTable->getValidator('default');
$errors = $validator->validate([
'name' => str_repeat('a', 51),
'email' => str_repeat('a', 246) . '@gmail.com',
'url' => 'http://example.com/' . str_repeat('a', 237)
]);
$this->assertFalse($this->BlogComment->validates());

$this->assertArrayHasKey('name', $this->BlogComment->validationErrors);
$this->assertEquals('お名前は50文字以内で入力してください。', current($this->BlogComment->validationErrors['name']));

$this->assertArrayHasKey('email', $this->BlogComment->validationErrors);
$this->assertEquals('Eメールは255文字以内で入力してください。', current($this->BlogComment->validationErrors['email']));


$this->assertArrayHasKey('url', $this->BlogComment->validationErrors);
$this->assertEquals('URLは255文字以内で入力してください。', current($this->BlogComment->validationErrors['url']));
$this->assertEquals('お名前は50文字以内で入力してください。', current($errors['name']));
$this->assertEquals('Eメールは255文字以内で入力してください。', current($errors['email']));
$this->assertEquals('URLは255文字以内で入力してください。', current($errors['url']));
}

public function test桁数チェック正常系()
public function test_validationDefaultNotError()
{
$this->markTestIncomplete('こちらのテストはまだ未確認です');
$this->BlogComment->create([
'BlogComment' => [
'name' => '12345678901234567890123456789012345678901234567890',
'email' => '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678@example.com',
'url' => 'http://example.com/123456789012345678901234567890123456789012345678901234567890123456789012345678901234567567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456'
]
$validator = $this->BlogCommentsTable->getValidator('default');
$errors = $validator->validate([
'name' => str_repeat('a', 50),
'email' => str_repeat('a', 245) . '@gmail.com',
'url' => 'http://example.com/' . str_repeat('a', 236)
]);
$this->assertTrue($this->BlogComment->validates());
$this->assertCount(0, $errors);
}

public function testその他異常系()
public function test_validationDefaultOtherErrors()
{
$this->markTestIncomplete('こちらのテストはまだ未確認です');
// 形式チェック
$this->BlogComment->create([
'BlogComment' => [
'email' => 'hoge',
'url' => 'hoge'
]
$validator = $this->BlogCommentsTable->getValidator('default');
$errors = $validator->validate([
'name' => str_repeat('a', 5),
'email' => str_repeat('a', 10),
'url' => str_repeat('a', 10)
]);

$this->assertFalse($this->BlogComment->validates());

$this->assertArrayHasKey('email', $this->BlogComment->validationErrors);
$this->assertEquals('Eメールの形式が不正です。', current($this->BlogComment->validationErrors['email']));

$this->assertArrayHasKey('url', $this->BlogComment->validationErrors);
$this->assertEquals('URLの形式が不正です。', current($this->BlogComment->validationErrors['url']));
$this->assertEquals('Eメールの形式が不正です。', current($errors['email']));
$this->assertEquals('URLの形式が不正です。', current($errors['url']));
}

/**
Expand Down

0 comments on commit 86a35df

Please sign in to comment.