-
Notifications
You must be signed in to change notification settings - Fork 5
Add C0 unit test for UsernameValidation #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,67 @@ | |
|
|
||
| namespace Tests\C0; | ||
|
|
||
| use App\UsernameValidation; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class UsernameValidationTest extends TestCase | ||
| { | ||
| // TODO: write 6 tests to coverage 100% code | ||
| function generateRandomString($length = 10) { | ||
| return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)))), 1, $length); | ||
| } | ||
|
|
||
| public function test_give_minimum_username_then_false_and_show_message() | ||
| { | ||
| $usernameValidation = new UsernameValidation(); | ||
|
|
||
| $this->assertFalse($usernameValidation->isValid('')); | ||
| $this->assertEquals('Minimum length is ' . UsernameValidation::MIN_LENGTH, $usernameValidation->getMessage()); | ||
| } | ||
|
|
||
| public function test_give_maximum_username_then_false_and_show_message() | ||
| { | ||
| $usernameValidation = new UsernameValidation(); | ||
| $username = $this->generateRandomString(UsernameValidation::MAX_LENGTH + 1); //get string always greater then MAX_LENGTH | ||
|
|
||
| $this->assertFalse($usernameValidation->isValid($username)); | ||
| $this->assertEquals('Maximum length is ' . UsernameValidation::MAX_LENGTH, $usernameValidation->getMessage()); | ||
| } | ||
|
|
||
| public function test_username_have_dash_at_begin_or_end_then_false_and_show_message() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So với cách đặt tên: Thì có dễ hiểu hơn không nhỉ? Ở đây IT là class under test => Khi dùng
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tuanpt-0634
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Should_ExpectedBehavior_When_StateUnderTest() {}
Should_ThrowException_When_AgeLessThan18() {}
Should_FailToWithdrawMoney_ForInvalidAccount() {}
Should_FailToAdmit_IfMandatoryFieldsAreMissing() {}=> public function test_it_expected_behavior_when_condition() {}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oki anh, thế thì mình nên tổng hợp lại rồi để đó là 1 convention ạ . Kiểu thế thì mọi người cũng dễ follow hơn
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. anw nếu mọi người quen rồi thì đặt như thế nào cũng được mà a :))), Freestyle mà ✌️ |
||
| { | ||
| $usernameValidation = new UsernameValidation(); | ||
| $username = $this->generateRandomString(floor(UsernameValidation::MAX_LENGTH / 2)); //get string allow in length | ||
| $username .= '-'; //add dash to test valid | ||
|
|
||
| $this->assertFalse($usernameValidation->isValid($username)); | ||
| $this->assertEquals('- cannot appear at begin or end of name', $usernameValidation->getMessage()); | ||
| } | ||
|
|
||
| public function test_username_have_multiple_dash_at_same_place_then_false_and_show_message() | ||
| { | ||
| $usernameValidation = new UsernameValidation(); | ||
| $username = $this->generateRandomString(floor(UsernameValidation::MAX_LENGTH / 2)); //get string allow in length | ||
| $username .= '--'; //add multiple dash to test valid | ||
|
|
||
| $this->assertFalse($usernameValidation->isValid($username)); | ||
| $this->assertEquals('Only single - is allowed', $usernameValidation->getMessage()); | ||
| } | ||
|
|
||
| public function test_username_have_character_not_allow_then_false_and_show_message() | ||
| { | ||
| $usernameValidation = new UsernameValidation(); | ||
| $username = $this->generateRandomString(floor(UsernameValidation::MAX_LENGTH / 2)); //get string allow in length | ||
| $username .= '@'; //add character not allow test valid | ||
|
|
||
| $this->assertFalse($usernameValidation->isValid($username)); | ||
| $this->assertEquals('Invalid character. Use only letters, digits and -', $usernameValidation->getMessage()); | ||
| } | ||
|
|
||
| public function test_username_id_valid() | ||
| { | ||
| $usernameValidation = new UsernameValidation(); | ||
| $username = $this->generateRandomString(floor(UsernameValidation::MAX_LENGTH / 2)); //get string allow in length | ||
|
|
||
| $this->assertTrue($usernameValidation->isValid($username)); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Có cần phải hàm phức tạp này không em? Viết sao cho nó vừa đúng case, vừa đơn giản, vừa đảm bảo chạy nhanh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Em thấy testcase nó cũng như class bình thường thôi ạ, viết sao cho nó thoải mái + reuse cao là được. Ngoài ra nữa thì hàm này sẽ suppỏt cho 1 case là nếu anh change MaxLength và MinLength thì nó vẫn đảm bảo là test case vẫn đúng. Ở đây em ko muốn người ta phải tập trung vào cách define hoặc config mà tập chung vào test thì nó sẽ oke hơn
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nếu anh viết test case như thế này thì có đơn giản hơn không :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oke a, cái này em giờ mới biết dùng được kiểu này :v
để e sửa ạ