Skip to content

Commit 55fe5a6

Browse files
committed
refactor(PropertyInspector): update return type and error handling
- Change PropertyInspector::inspect() to return PropertyAttributeHandler - Update PropertyInspectorTest to reflect new return type and behavior - Add additional tests for exception and error handling - Remove obsolete test cases
1 parent 7103d09 commit 55fe5a6

File tree

3 files changed

+36
-31
lines changed

3 files changed

+36
-31
lines changed

src/Contract/PropertyInspector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface PropertyInspector
1616
*
1717
* @throws PropertyInspectionException If there's an error inspecting the object
1818
*
19-
* @return array<string, array<int, mixed>> The inspection results
19+
* @return PropertyAttributeHandler The inspection results
2020
*/
21-
public function inspect(object $object, PropertyAttributeHandler $handler): array;
21+
public function inspect(object $object, PropertyAttributeHandler $handler): PropertyAttributeHandler;
2222
}

src/Utility/PropertyInspector.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,21 @@ public function __construct(private readonly AttributeAnalyzer $attributeAnalyze
1515
{
1616
}
1717

18-
public function inspect(object $object, PropertyAttributeHandler $handler): array
18+
public function inspect(object $object, PropertyAttributeHandler $handler): PropertyAttributeHandler
1919
{
2020
try {
2121
$analysisResults = $this->attributeAnalyzer->analyzeObject($object);
22-
$handledResults = [];
23-
2422
foreach ($analysisResults as $propertyName => $propertyData) {
2523
foreach ($propertyData['attributes'] as $attribute) {
26-
$result = $handler->handleAttribute($propertyName, $attribute, $propertyData['value']);
27-
if (null !== $result) {
28-
$handledResults[$propertyName][] = $result;
29-
}
24+
$handler->handleAttribute($propertyName, $attribute, $propertyData['value']);
3025
}
3126
}
3227

33-
return $handledResults;
28+
return $handler;
3429
} catch (\ReflectionException $e) {
3530
throw new PropertyInspectionException('Failed to analyze object: ' . $e->getMessage(), 0, $e);
3631
} catch (\Exception $e) {
37-
throw new PropertyInspectionException('An error occurred during object analysis: ' . $e->getMessage(), 0, $e);
32+
throw new PropertyInspectionException('An exception occurred during object analysis: ' . $e->getMessage(), 0, $e);
3833
} catch (\Error $e) {
3934
throw new PropertyInspectionException('An error occurred during object analysis: ' . $e->getMessage(), 0, $e);
4035
}

tests/Utility/PropertyInspectorTest.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public function testInspect(): void
4040

4141
$mockHandler->expects($this->once())
4242
->method('handleAttribute')
43-
->willReturn('handled result');
43+
->with('property1', $this->isInstanceOf(\stdClass::class), 'value1');
4444

4545
$result = $this->inspector->inspect($object, $mockHandler);
4646

47-
$this->assertEquals(['property1' => ['handled result']], $result);
47+
$this->assertSame($mockHandler, $result);
4848
}
4949

5050
public function testInspectWithNoResults(): void
@@ -56,9 +56,12 @@ public function testInspectWithNoResults(): void
5656
->method('analyzeObject')
5757
->willReturn([]);
5858

59+
$mockHandler->expects($this->never())
60+
->method('handleAttribute');
61+
5962
$result = $this->inspector->inspect($object, $mockHandler);
6063

61-
$this->assertEmpty($result);
64+
$this->assertSame($mockHandler, $result);
6265
}
6366

6467
public function testInspectWithAnalyzerException(): void
@@ -76,7 +79,7 @@ public function testInspectWithAnalyzerException(): void
7679
$this->inspector->inspect($object, $mockHandler);
7780
}
7881

79-
public function testInspectWithHandlerReturningNull(): void
82+
public function testInspectWithMultipleAttributes(): void
8083
{
8184
$object = new \stdClass();
8285
$mockHandler = $this->createMock(PropertyAttributeHandler::class);
@@ -86,39 +89,46 @@ public function testInspectWithHandlerReturningNull(): void
8689
->willReturn([
8790
'property1' => [
8891
'value' => 'value1',
89-
'attributes' => [new \stdClass()],
92+
'attributes' => [new \stdClass(), new \stdClass()],
9093
],
9194
]);
9295

93-
$mockHandler->expects($this->once())
96+
$mockHandler->expects($this->exactly(2))
9497
->method('handleAttribute')
95-
->willReturn(null);
98+
->with('property1', $this->isInstanceOf(\stdClass::class), 'value1');
9699

97100
$result = $this->inspector->inspect($object, $mockHandler);
98101

99-
$this->assertEmpty($result);
102+
$this->assertSame($mockHandler, $result);
100103
}
101104

102-
public function testInspectWithMultipleAttributes(): void
105+
public function testInspectWithGeneralException(): void
103106
{
104107
$object = new \stdClass();
105108
$mockHandler = $this->createMock(PropertyAttributeHandler::class);
106109

107110
$this->analyzer->expects($this->once())
108111
->method('analyzeObject')
109-
->willReturn([
110-
'property1' => [
111-
'value' => 'value1',
112-
'attributes' => [new \stdClass(), new \stdClass()],
113-
],
114-
]);
112+
->willThrowException(new \Exception('General exception'));
115113

116-
$mockHandler->expects($this->exactly(2))
117-
->method('handleAttribute')
118-
->willReturn('handled result');
114+
$this->expectException(PropertyInspectionException::class);
115+
$this->expectExceptionMessage('An exception occurred during object analysis: General exception');
119116

120-
$result = $this->inspector->inspect($object, $mockHandler);
117+
$this->inspector->inspect($object, $mockHandler);
118+
}
119+
120+
public function testInspectWithError(): void
121+
{
122+
$object = new \stdClass();
123+
$mockHandler = $this->createMock(PropertyAttributeHandler::class);
124+
125+
$this->analyzer->expects($this->once())
126+
->method('analyzeObject')
127+
->willThrowException(new \Error('Fatal error'));
121128

122-
$this->assertEquals(['property1' => ['handled result', 'handled result']], $result);
129+
$this->expectException(PropertyInspectionException::class);
130+
$this->expectExceptionMessage('An error occurred during object analysis: Fatal error');
131+
132+
$this->inspector->inspect($object, $mockHandler);
123133
}
124134
}

0 commit comments

Comments
 (0)