|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace KaririCode\PropertyInspector\Tests; |
| 6 | + |
| 7 | +use Attribute; |
| 8 | +use KaririCode\PropertyInspector\AttributeAnalyzer; |
| 9 | +use KaririCode\PropertyInspector\Contract\AttributeAnalyzer as AttributeAnalyzerContract; |
| 10 | +use KaririCode\PropertyInspector\Exception\PropertyInspectionException; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +#[\Attribute()] |
| 14 | +class TestAttribute |
| 15 | +{ |
| 16 | +} |
| 17 | + |
| 18 | +class TestObject |
| 19 | +{ |
| 20 | + #[TestAttribute] |
| 21 | + public string $testProperty = 'test value'; |
| 22 | + |
| 23 | + private string $privateProperty = 'private value'; |
| 24 | +} |
| 25 | + |
| 26 | +final class AttributeAnalyzerTest extends TestCase |
| 27 | +{ |
| 28 | + private AttributeAnalyzerContract $analyzer; |
| 29 | + |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + $this->analyzer = new AttributeAnalyzer(TestAttribute::class); |
| 33 | + } |
| 34 | + |
| 35 | + public function testAnalyzeObject(): void |
| 36 | + { |
| 37 | + $object = new TestObject(); |
| 38 | + $result = $this->analyzer->analyzeObject($object); |
| 39 | + |
| 40 | + $this->assertArrayHasKey('testProperty', $result); |
| 41 | + $this->assertEquals('test value', $result['testProperty']['value']); |
| 42 | + $this->assertInstanceOf(TestAttribute::class, $result['testProperty']['attributes'][0]); |
| 43 | + } |
| 44 | + |
| 45 | + public function testAnalyzeObjectWithNoAttributes(): void |
| 46 | + { |
| 47 | + $object = new class { |
| 48 | + public string $propertyWithoutAttribute = 'no attribute'; |
| 49 | + }; |
| 50 | + |
| 51 | + $result = $this->analyzer->analyzeObject($object); |
| 52 | + |
| 53 | + $this->assertEmpty($result); |
| 54 | + } |
| 55 | + |
| 56 | + public function testAnalyzeObjectWithPrivateProperty(): void |
| 57 | + { |
| 58 | + $object = new TestObject(); |
| 59 | + $result = $this->analyzer->analyzeObject($object); |
| 60 | + |
| 61 | + $this->assertArrayNotHasKey('privateProperty', $result); |
| 62 | + } |
| 63 | + |
| 64 | + public function testReflectionExceptionThrownDuringAnalyzeObject(): void |
| 65 | + { |
| 66 | + // Define a fake attribute class for testing |
| 67 | + $attributeClass = 'FakeAttributeClass'; |
| 68 | + |
| 69 | + // Create the AttributeAnalyzer with the fake attribute class |
| 70 | + $analyzer = new AttributeAnalyzer($attributeClass); |
| 71 | + |
| 72 | + // Simulate an object that will trigger a ReflectionException |
| 73 | + $object = new class { |
| 74 | + private $inaccessibleProperty; |
| 75 | + |
| 76 | + public function __construct() |
| 77 | + { |
| 78 | + // Simulating an inaccessible property that will cause ReflectionException |
| 79 | + $this->inaccessibleProperty = null; |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + // We expect a PropertyInspectionException due to ReflectionException |
| 84 | + $this->expectException(PropertyInspectionException::class); |
| 85 | + $this->expectExceptionMessage('An error occurred during object analysis: Class "FakeAttributeClass" not found'); |
| 86 | + |
| 87 | + // Execute the analyzeObject method, which should trigger the exception |
| 88 | + $analyzer->analyzeObject($object); |
| 89 | + } |
| 90 | + |
| 91 | + public function testErrorThrownDuringAnalyzeProperty(): void |
| 92 | + { |
| 93 | + // Define a fake attribute class for testing |
| 94 | + $attributeClass = 'FakeAttributeClass'; |
| 95 | + |
| 96 | + // Create the AttributeAnalyzer with the fake attribute class |
| 97 | + $analyzer = new AttributeAnalyzer($attributeClass); |
| 98 | + |
| 99 | + // Simulate an object that will trigger an Error during property analysis |
| 100 | + $object = new class { |
| 101 | + private $errorProperty; |
| 102 | + |
| 103 | + public function __construct() |
| 104 | + { |
| 105 | + // Simulating an error in the property that will cause an Error during reflection |
| 106 | + $this->errorProperty = null; |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + // Mock Reflection to throw an error during attribute analysis |
| 111 | + $reflectionPropertyMock = $this->createMock(\ReflectionProperty::class); |
| 112 | + $reflectionPropertyMock->method('getAttributes') |
| 113 | + ->willThrowException(new \Error('Simulated Error')); |
| 114 | + |
| 115 | + // We expect a PropertyInspectionException due to the Error |
| 116 | + $this->expectException(PropertyInspectionException::class); |
| 117 | + $this->expectExceptionMessage('An error occurred during object analysis: Class "FakeAttributeClass" not found'); |
| 118 | + |
| 119 | + // Execute the analyzeObject method, which should trigger the exception |
| 120 | + $analyzer->analyzeObject($object); |
| 121 | + } |
| 122 | +} |
0 commit comments