Copy-paste gaps in rule tests: `DateTime`/`Time` tests check `Date`, and `Integer` has no contract tests
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 86/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- php
- Lĩnh vực
- testing-qa
Hướng nghiên cứu
Bắt đầu với tests/Rule/Date/DateTimeTest.php, tests/Rule/Date/TimeTest.php và NumberTest, xem xét testGetName(), testSkipOnError() và testWhen(). Sử dụng các test Date và Number hiện có làm mẫu, sau đó chạy các test rule bị ảnh hưởng để xác nhận rằng DateTime, Time và Integer đều có các kiểm tra contract này.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
What steps will reproduce the problem?
1. DateTimeTest and TimeTest test the Date rule instead of their own rule.
Three methods in tests/Rule/Date/DateTimeTest.php and tests/Rule/Date/TimeTest.php create new Date() instead of new DateTime() / new Time():
public function testGetName(): void
{
$rule = new Date(); // should be new DateTime() / new Time()
$this->assertSame('date', $rule->getName());
}
public function testSkipOnError(): void
{
$this->testSkipOnErrorInternal(new Date(), new Date(skipOnError: true));
}
public function testWhen(): void
{
$this->testWhenInternal(
new Date(),
new Date(when: static fn(mixed $value): bool => $value !== null),
);
}
So getName(), skipOnError and when are never tested for DateTime and Time. DateTest already covers Date, so these checks only run twice more on the same rule.
2. The Integer rule has no tests for getName(), skipOnError and when.
Integer has no test class of its own. NumberTest checks how Integer validates values (dataValidationPassed()/dataValidationFailed()), but testGetName(), testSkipOnError() and testWhen() there only use new Number(). AbstractNumber::getName() returns static::class, so Integer returns its own name, and no test checks that.
What is the expected result?
Every rule has these contract checks run on an instance of that rule.
What do you get instead?
DateTime, Time and Integer are not covered. A regression in their getName(), skipOnError() or when() would go unnoticed. For example: an override in the class, a missing trait after a refactoring, or a wrong default in the constructor.
Suggested fix
-
In
DateTimeTest/TimeTest, replaceDatewithDateTime/Timein the three methods above.getName()returns'date'for all of them (fromBaseDate), so the expected value stays the same. -
Add
testIntegerGetName(),testIntegerSkipOnError()andtestIntegerWhen()toNumberTest, or split them into a separateIntegerTest:public function testIntegerSkipOnError(): void { $this->testSkipOnErrorInternal(new Integer(), new Integer(skipOnError: true)); } -
Optionally, prevent this kind of copy-paste mistake: add one data-provider test that runs the
skipOnError/whenchecks over a list of all rules, instead of a separate method in each test class. Then adding a new rule means adding one line to the provider. The#[DataProvider]returns rule × option pairs (skipOnError,when); each pair gives a factory, the option name, its default, a new value and the getter name.
Additional info
| Q | A |
|---|---|
| Version | 2.x-dev (9be0c31) |
| PHP version | any |
| Operating system | any |
- Ngôn ngữ chính
- PHP
- Star
- 167
- Fork
- 47
- Merge trung bình
- 1 ngày 16 giờ
- Pull request đã merge (30 ngày)
- 4
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của yiisoft/validator
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 65/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
-
Multibyte trim supportĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 62/100
Tất cả issue của yiisoft/validator
Issue tương tự
-
Awaiting Triage
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
WordPress/two-factor#1008 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
sync-en
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
sync-en
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Перевод устарел
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 22 ngày