Skip to content

Commit 2fa2c8d

Browse files
myxiaoaoclaude
andcommitted
test: 第二阶段测试覆盖率提升,新增 70 个测试文件 (+776 tests, +1333 assertions)
覆盖 Grid Filters、Grid Displayers、Form Fields、Support、 Widgets、Http Middleware、Http Controllers、Repositories 等核心模块, 测试总数从 1351 提升至 2127,断言从 2261 提升至 3594。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5e7e755 commit 2fa2c8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+10485
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace Dcat\Admin\Tests\Unit\Form\Field;
4+
5+
use Dcat\Admin\Form\Field\ArrayField;
6+
use Dcat\Admin\Tests\TestCase;
7+
8+
class ArrayFieldTest extends TestCase
9+
{
10+
protected function getProtectedProperty(object $object, string $property)
11+
{
12+
$reflection = new \ReflectionProperty($object, $property);
13+
$reflection->setAccessible(true);
14+
15+
return $reflection->getValue($object);
16+
}
17+
18+
protected function setProtectedProperty(object $object, string $property, mixed $value): void
19+
{
20+
$reflection = new \ReflectionProperty($object, $property);
21+
$reflection->setAccessible(true);
22+
$reflection->setValue($object, $value);
23+
}
24+
25+
protected function createArrayField(string $column = 'items', ?string $label = null, ?\Closure $builder = null): ArrayField
26+
{
27+
$builder = $builder ?? function ($form) {};
28+
29+
$args = $label !== null ? [$label, $builder] : [$builder];
30+
31+
return new ArrayField($column, $args);
32+
}
33+
34+
// -------------------------------------------------------
35+
// constructor
36+
// -------------------------------------------------------
37+
38+
public function test_constructor_with_builder_only(): void
39+
{
40+
$builder = function ($form) {};
41+
$field = new ArrayField('items', [$builder]);
42+
43+
$column = $this->getProtectedProperty($field, 'column');
44+
45+
$this->assertSame('items', $column);
46+
}
47+
48+
public function test_constructor_with_label_and_builder(): void
49+
{
50+
$builder = function ($form) {};
51+
$field = new ArrayField('items', ['My Items', $builder]);
52+
53+
$label = $this->getProtectedProperty($field, 'label');
54+
$column = $this->getProtectedProperty($field, 'column');
55+
56+
$this->assertSame('My Items', $label);
57+
$this->assertSame('items', $column);
58+
}
59+
60+
public function test_constructor_stores_builder(): void
61+
{
62+
$builder = function ($form) {
63+
$form->text('name');
64+
};
65+
$field = new ArrayField('items', [$builder]);
66+
67+
$storedBuilder = $this->getProtectedProperty($field, 'builder');
68+
69+
$this->assertSame($builder, $storedBuilder);
70+
}
71+
72+
// -------------------------------------------------------
73+
// class hierarchy
74+
// -------------------------------------------------------
75+
76+
public function test_extends_has_many(): void
77+
{
78+
$field = $this->createArrayField();
79+
80+
$this->assertInstanceOf(\Dcat\Admin\Form\Field\HasMany::class, $field);
81+
}
82+
83+
public function test_extends_field(): void
84+
{
85+
$field = $this->createArrayField();
86+
87+
$this->assertInstanceOf(\Dcat\Admin\Form\Field::class, $field);
88+
}
89+
90+
// -------------------------------------------------------
91+
// buildRelatedForms() returns empty when no form set
92+
// -------------------------------------------------------
93+
94+
public function test_build_related_forms_returns_empty_when_no_form(): void
95+
{
96+
$field = $this->createArrayField();
97+
98+
$reflection = new \ReflectionMethod($field, 'buildRelatedForms');
99+
$reflection->setAccessible(true);
100+
101+
$result = $reflection->invoke($field);
102+
103+
$this->assertSame([], $result);
104+
}
105+
106+
// -------------------------------------------------------
107+
// columnClass is set
108+
// -------------------------------------------------------
109+
110+
public function test_column_class_is_set(): void
111+
{
112+
$field = $this->createArrayField('test_items');
113+
114+
$columnClass = $this->getProtectedProperty($field, 'columnClass');
115+
116+
$this->assertNotEmpty($columnClass);
117+
$this->assertIsString($columnClass);
118+
}
119+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
namespace Dcat\Admin\Tests\Unit\Form\Field;
4+
5+
use Dcat\Admin\Form\Field\Checkbox;
6+
use Dcat\Admin\Tests\TestCase;
7+
8+
class CheckboxTest extends TestCase
9+
{
10+
protected function createCheckbox(string $column = 'tags', string $label = 'Tags'): Checkbox
11+
{
12+
return new Checkbox($column, [$label]);
13+
}
14+
15+
protected function getProtectedProperty(object $object, string $property)
16+
{
17+
$reflection = new \ReflectionProperty($object, $property);
18+
$reflection->setAccessible(true);
19+
20+
return $reflection->getValue($object);
21+
}
22+
23+
// -------------------------------------------------------
24+
// options()
25+
// -------------------------------------------------------
26+
27+
public function test_options_with_array(): void
28+
{
29+
$field = $this->createCheckbox();
30+
$options = ['php' => 'PHP', 'js' => 'JavaScript'];
31+
32+
$result = $field->options($options);
33+
34+
$this->assertSame($field, $result);
35+
$this->assertSame($options, $this->getProtectedProperty($field, 'options'));
36+
}
37+
38+
public function test_options_with_empty_array(): void
39+
{
40+
$field = $this->createCheckbox();
41+
42+
$field->options([]);
43+
44+
$this->assertSame([], $this->getProtectedProperty($field, 'options'));
45+
}
46+
47+
public function test_options_with_closure(): void
48+
{
49+
$field = $this->createCheckbox();
50+
$closure = function () {
51+
return ['a' => 'A'];
52+
};
53+
54+
$result = $field->options($closure);
55+
56+
$this->assertSame($field, $result);
57+
$this->assertInstanceOf(\Closure::class, $this->getProtectedProperty($field, 'options'));
58+
}
59+
60+
// -------------------------------------------------------
61+
// style()
62+
// -------------------------------------------------------
63+
64+
public function test_default_style_is_primary(): void
65+
{
66+
$field = $this->createCheckbox();
67+
68+
$this->assertSame('primary', $this->getProtectedProperty($field, 'style'));
69+
}
70+
71+
public function test_style_sets_custom_style(): void
72+
{
73+
$field = $this->createCheckbox();
74+
75+
$result = $field->style('danger');
76+
77+
$this->assertSame($field, $result);
78+
$this->assertSame('danger', $this->getProtectedProperty($field, 'style'));
79+
}
80+
81+
public function test_style_can_be_changed_multiple_times(): void
82+
{
83+
$field = $this->createCheckbox();
84+
85+
$field->style('info');
86+
$field->style('success');
87+
88+
$this->assertSame('success', $this->getProtectedProperty($field, 'style'));
89+
}
90+
91+
// -------------------------------------------------------
92+
// canCheckAll()
93+
// -------------------------------------------------------
94+
95+
public function test_can_check_all_defaults_to_false(): void
96+
{
97+
$field = $this->createCheckbox();
98+
99+
$this->assertFalse($this->getProtectedProperty($field, 'canCheckAll'));
100+
}
101+
102+
public function test_can_check_all_enables_feature(): void
103+
{
104+
$field = $this->createCheckbox();
105+
106+
$result = $field->canCheckAll();
107+
108+
$this->assertSame($field, $result);
109+
$this->assertTrue($this->getProtectedProperty($field, 'canCheckAll'));
110+
}
111+
112+
// -------------------------------------------------------
113+
// inline()
114+
// -------------------------------------------------------
115+
116+
public function test_inline_defaults_to_true(): void
117+
{
118+
$field = $this->createCheckbox();
119+
120+
$this->assertTrue($this->getProtectedProperty($field, 'inline'));
121+
}
122+
123+
public function test_inline_can_be_disabled(): void
124+
{
125+
$field = $this->createCheckbox();
126+
127+
$result = $field->inline(false);
128+
129+
$this->assertSame($field, $result);
130+
$this->assertFalse($this->getProtectedProperty($field, 'inline'));
131+
}
132+
133+
public function test_inline_can_be_re_enabled(): void
134+
{
135+
$field = $this->createCheckbox();
136+
137+
$field->inline(false);
138+
$field->inline(true);
139+
140+
$this->assertTrue($this->getProtectedProperty($field, 'inline'));
141+
}
142+
143+
// -------------------------------------------------------
144+
// cascadeEvent default
145+
// -------------------------------------------------------
146+
147+
public function test_cascade_event_defaults_to_change(): void
148+
{
149+
$field = $this->createCheckbox();
150+
151+
$cascadeEvent = $this->getProtectedProperty($field, 'cascadeEvent');
152+
153+
$this->assertSame('change', $cascadeEvent);
154+
}
155+
}

0 commit comments

Comments
 (0)