Skip to content

Commit 104e841

Browse files
committed
chore: configure linters and fixers
1 parent 16b6891 commit 104e841

2 files changed

Lines changed: 99 additions & 89 deletions

File tree

src/Factory/DefaultSpecsFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function make(): Specs
3434
return $registry;
3535
}
3636

37-
public function validate(mixed $name, mixed $properties): void
37+
private function validate(mixed $name, mixed $properties): void
3838
{
3939
if (! is_string($name)) {
4040
$given = gettype($name);

tests/Factory/DefaultSpecsFactoryTest.php

Lines changed: 98 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use InvalidArgumentException;
1111
use PHPUnit\Framework\TestCase;
1212

13-
class DefaultSpecsFactoryTest extends TestCase
13+
final class DefaultSpecsFactoryTest extends TestCase
1414
{
1515
use MakeExtension;
1616

@@ -41,16 +41,6 @@ public function testCanCreateSpecsFactoryWithSpecs(): void
4141
$this->assertFalse($registry->has('nonexistent'));
4242
}
4343

44-
public function testMakeReturnsRegistryWithDefaultBehavior(): void
45-
{
46-
$builder = $this->make(Builder::class);
47-
$factory = new DefaultSpecsFactory($builder);
48-
49-
$registry = $factory->make();
50-
51-
$this->assertFalse($registry->has('nonexistent'));
52-
}
53-
5444
public function testMakeRegistersSpecsCorrectly(): void
5545
{
5646
$builder = $this->make(Builder::class);
@@ -63,158 +53,178 @@ public function testMakeRegistersSpecsCorrectly(): void
6353
$factory = new DefaultSpecsFactory($builder, $specs);
6454
$registry = $factory->make();
6555

66-
// Test that all specs are registered correctly
6756
$this->assertTrue($registry->has('required'));
6857
$this->assertTrue($registry->has('string'));
6958
$this->assertTrue($registry->has('min'));
70-
71-
// Test that spec objects can be retrieved
72-
$this->assertNotNull($registry->get('required'));
73-
$this->assertNotNull($registry->get('min'));
7459
}
7560

76-
public function testValidateAcceptsValidStringNameAndArrayProperties(): void
61+
public function testMakeWithComplexSpecs(): void
7762
{
7863
$builder = $this->make(Builder::class);
79-
$factory = new DefaultSpecsFactory($builder);
80-
81-
$this->expectNotToPerformAssertions();
82-
$factory->validate('required', []);
83-
}
64+
$specs = [
65+
'required' => [],
66+
'string' => [],
67+
'integer' => [],
68+
'min' => ['params' => ['min']],
69+
'max' => ['params' => ['max']],
70+
'between' => [
71+
'params' => [
72+
'min',
73+
'max',
74+
],
75+
],
76+
'in' => ['params' => ['values']],
77+
'regex' => [
78+
'params' => [
79+
'pattern',
80+
'parameters:optional',
81+
],
82+
],
83+
];
8484

85-
public function testValidateAcceptsValidStringNameAndArrayPropertiesWithData(): void
86-
{
87-
$builder = $this->make(Builder::class);
88-
$factory = new DefaultSpecsFactory($builder);
85+
$factory = new DefaultSpecsFactory($builder, $specs);
86+
$registry = $factory->make();
8987

90-
$this->expectNotToPerformAssertions();
91-
$factory->validate('min', ['params' => ['min']]);
88+
$this->assertTrue($registry->has('required'));
89+
$this->assertTrue($registry->has('between'));
90+
$this->assertTrue($registry->has('regex'));
9291
}
9392

94-
public function testValidateThrowsExceptionForNonStringName(): void
93+
// Test scenarios that trigger validation errors in the validate method
94+
95+
public function testMakeThrowsExceptionWhenSpecNameIsNotString(): void
9596
{
9697
$builder = $this->make(Builder::class);
97-
$factory = new DefaultSpecsFactory($builder);
98+
$specs = [
99+
123 => [], // Integer key instead of string
100+
];
101+
102+
$factory = new DefaultSpecsFactory($builder, $specs);
98103

99104
$this->expectException(InvalidArgumentException::class);
100105
$this->expectExceptionMessage('Spec name must be a string, integer given.');
101106

102-
$factory->validate(123, []);
107+
$factory->make();
103108
}
104109

105-
public function testValidateThrowsExceptionForNullName(): void
110+
public function testMakeThrowsExceptionWhenSpecPropertiesIsNotArray(): void
106111
{
107112
$builder = $this->make(Builder::class);
108-
$factory = new DefaultSpecsFactory($builder);
113+
$specs = [
114+
'required' => 'invalid', // String value instead of array
115+
];
116+
117+
$factory = new DefaultSpecsFactory($builder, $specs);
109118

110119
$this->expectException(InvalidArgumentException::class);
111-
$this->expectExceptionMessage('Spec name must be a string, NULL given.');
120+
$this->expectExceptionMessage('Spec properties must be an array, string given.');
112121

113-
$factory->validate(null, []);
122+
$factory->make();
114123
}
115124

116-
public function testValidateThrowsExceptionForBooleanName(): void
125+
public function testMakeThrowsExceptionWhenSpecPropertiesIsInteger(): void
117126
{
118127
$builder = $this->make(Builder::class);
119-
$factory = new DefaultSpecsFactory($builder);
128+
$specs = [
129+
'required' => 123, // Integer value instead of array
130+
];
131+
132+
$factory = new DefaultSpecsFactory($builder, $specs);
120133

121134
$this->expectException(InvalidArgumentException::class);
122-
$this->expectExceptionMessage('Spec name must be a string, boolean given.');
135+
$this->expectExceptionMessage('Spec properties must be an array, integer given.');
123136

124-
$factory->validate(true, []);
137+
$factory->make();
125138
}
126139

127-
public function testValidateThrowsExceptionForArrayName(): void
140+
public function testMakeThrowsExceptionWhenSpecPropertiesIsNull(): void
128141
{
129142
$builder = $this->make(Builder::class);
130-
$factory = new DefaultSpecsFactory($builder);
143+
$specs = [
144+
'required' => null, // Null value instead of array
145+
];
146+
147+
$factory = new DefaultSpecsFactory($builder, $specs);
131148

132149
$this->expectException(InvalidArgumentException::class);
133-
$this->expectExceptionMessage('Spec name must be a string, array given.');
150+
$this->expectExceptionMessage('Spec properties must be an array, NULL given.');
134151

135-
$factory->validate([], []);
152+
$factory->make();
136153
}
137154

138-
public function testValidateThrowsExceptionForNonArrayProperties(): void
155+
public function testMakeThrowsExceptionWhenSpecPropertiesIsBoolean(): void
139156
{
140157
$builder = $this->make(Builder::class);
141-
$factory = new DefaultSpecsFactory($builder);
158+
$specs = [
159+
'required' => true, // Boolean value instead of array
160+
];
161+
162+
$factory = new DefaultSpecsFactory($builder, $specs);
142163

143164
$this->expectException(InvalidArgumentException::class);
144-
$this->expectExceptionMessage('Spec properties must be an array, string given.');
165+
$this->expectExceptionMessage('Spec properties must be an array, boolean given.');
145166

146-
$factory->validate('required', 'invalid');
167+
$factory->make();
147168
}
148169

149-
public function testValidateThrowsExceptionForNullProperties(): void
170+
public function testMakeThrowsExceptionWhenSpecPropertiesIsFloat(): void
150171
{
151172
$builder = $this->make(Builder::class);
152-
$factory = new DefaultSpecsFactory($builder);
173+
$specs = [
174+
'required' => 3.14, // Float value instead of array
175+
];
176+
177+
$factory = new DefaultSpecsFactory($builder, $specs);
153178

154179
$this->expectException(InvalidArgumentException::class);
155-
$this->expectExceptionMessage('Spec properties must be an array, NULL given.');
180+
$this->expectExceptionMessage('Spec properties must be an array, double given.');
156181

157-
$factory->validate('required', null);
182+
$factory->make();
158183
}
159184

160-
public function testValidateThrowsExceptionForIntegerProperties(): void
185+
public function testMakeThrowsExceptionWhenSpecPropertiesIsObject(): void
161186
{
162187
$builder = $this->make(Builder::class);
163-
$factory = new DefaultSpecsFactory($builder);
188+
$specs = [
189+
'required' => new \stdClass(), // Object value instead of array
190+
];
191+
192+
$factory = new DefaultSpecsFactory($builder, $specs);
164193

165194
$this->expectException(InvalidArgumentException::class);
166-
$this->expectExceptionMessage('Spec properties must be an array, integer given.');
195+
$this->expectExceptionMessage('Spec properties must be an array, object given.');
167196

168-
$factory->validate('required', 123);
197+
$factory->make();
169198
}
170199

171-
public function testValidateThrowsExceptionForBooleanProperties(): void
200+
public function testMakeWithMixedValidAndInvalidSpecs(): void
172201
{
173202
$builder = $this->make(Builder::class);
174-
$factory = new DefaultSpecsFactory($builder);
203+
$specs = [
204+
'valid_spec' => [],
205+
123 => [], // This will trigger the validation error
206+
];
207+
208+
$factory = new DefaultSpecsFactory($builder, $specs);
175209

176210
$this->expectException(InvalidArgumentException::class);
177-
$this->expectExceptionMessage('Spec properties must be an array, boolean given.');
211+
$this->expectExceptionMessage('Spec name must be a string, integer given.');
178212

179-
$factory->validate('required', false);
213+
$factory->make();
180214
}
181215

182-
public function testMakeWithComplexSpecs(): void
216+
public function testMakeHandlesEmptyArrayPropertiesCorrectly(): void
183217
{
184218
$builder = $this->make(Builder::class);
185219
$specs = [
186-
'required' => [],
187-
'string' => [],
188-
'integer' => [],
189-
'min' => ['params' => ['min']],
190-
'max' => ['params' => ['max']],
191-
'between' => [
192-
'params' => [
193-
'min',
194-
'max',
195-
],
196-
],
197-
'in' => ['params' => ['values']],
198-
'regex' => [
199-
'params' => [
200-
'pattern',
201-
'parameters:optional',
202-
],
203-
],
220+
'empty_spec' => [], // Empty array - should be valid
221+
'spec_with_data' => ['param' => 'value'],
204222
];
205223

206224
$factory = new DefaultSpecsFactory($builder, $specs);
207225
$registry = $factory->make();
208226

209-
$this->assertTrue($registry->has('required'));
210-
$this->assertTrue($registry->has('string'));
211-
$this->assertTrue($registry->has('integer'));
212-
$this->assertTrue($registry->has('min'));
213-
$this->assertTrue($registry->has('max'));
214-
$this->assertTrue($registry->has('between'));
215-
$this->assertTrue($registry->has('in'));
216-
$this->assertTrue($registry->has('regex'));
217-
$this->assertNotNull($registry->get('min'));
218-
$this->assertNotNull($registry->get('max'));
227+
$this->assertTrue($registry->has('empty_spec'));
228+
$this->assertTrue($registry->has('spec_with_data'));
219229
}
220230
}

0 commit comments

Comments
 (0)