Skip to content

Commit 718c274

Browse files
koriymclaude
andcommitted
Add test coverage for SetterMethod::accept() exception handling
This commit adds two new test cases to improve code coverage for the SetterMethod::accept() method's exception handling logic: 1. testAcceptWithUnboundException: Tests that Unbound exceptions are properly re-thrown when isOptional is false 2. testAcceptWithUnboundExceptionOptional: Tests that Unbound exceptions are silently handled (not re-thrown) when isOptional is true These tests cover the previously untested catch block in SetterMethod::accept(), improving the method's line coverage from ~75% to 93.75%. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0aa8dce commit 718c274

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

tests/di/SetterMethodTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Ray\Di\Exception\Unbound;
99
use ReflectionMethod;
10+
use ReflectionParameter;
1011

1112
use function spl_object_hash;
1213

@@ -41,4 +42,115 @@ public function testUnbound(): void
4142
$car = new FakeCar(new FakeEngine());
4243
$this->setterMethods->__invoke($car, $container);
4344
}
45+
46+
public function testAcceptWithUnboundException(): void
47+
{
48+
$this->expectException(Unbound::class);
49+
$method = new ReflectionMethod(FakeCar::class, 'setTires');
50+
$setterMethod = new SetterMethod($method, new Name(Name::ANY));
51+
52+
$visitor = new class implements VisitorInterface
53+
{
54+
public function visitSetterMethod(string $method, Arguments $arguments): void
55+
{
56+
throw new Unbound(FakeTyreInterface::class);
57+
}
58+
59+
public function visitDependency(NewInstance $newInstance, ?string $postConstruct, bool $isSingleton): void
60+
{
61+
}
62+
63+
public function visitProvider(Dependency $dependency, string $context, bool $isSingleton): string
64+
{
65+
return '';
66+
}
67+
68+
/** @param mixed $value */
69+
public function visitInstance($value): string
70+
{
71+
return '';
72+
}
73+
74+
public function visitAspectBind(\Ray\Aop\Bind $aopBind): void
75+
{
76+
}
77+
78+
public function visitNewInstance(string $class, SetterMethods $setterMethods, ?Arguments $arguments, ?AspectBind $bind): void
79+
{
80+
}
81+
82+
/** @param array<SetterMethod> $setterMethods */
83+
public function visitSetterMethods(array $setterMethods): void
84+
{
85+
}
86+
87+
/** @param array<Argument> $arguments */
88+
public function visitArguments(array $arguments): void
89+
{
90+
}
91+
92+
/** @param mixed $defaultValue */
93+
public function visitArgument(string $index, bool $isDefaultAvailable, $defaultValue, ReflectionParameter $parameter): void
94+
{
95+
}
96+
};
97+
98+
$setterMethod->accept($visitor);
99+
}
100+
101+
public function testAcceptWithUnboundExceptionOptional(): void
102+
{
103+
$method = new ReflectionMethod(FakeCar::class, 'setTires');
104+
$setterMethod = new SetterMethod($method, new Name(Name::ANY));
105+
$setterMethod->setOptional();
106+
107+
$visitor = new class implements VisitorInterface
108+
{
109+
public function visitSetterMethod(string $method, Arguments $arguments): void
110+
{
111+
throw new Unbound(FakeTyreInterface::class);
112+
}
113+
114+
public function visitDependency(NewInstance $newInstance, ?string $postConstruct, bool $isSingleton): void
115+
{
116+
}
117+
118+
public function visitProvider(Dependency $dependency, string $context, bool $isSingleton): string
119+
{
120+
return '';
121+
}
122+
123+
/** @param mixed $value */
124+
public function visitInstance($value): string
125+
{
126+
return '';
127+
}
128+
129+
public function visitAspectBind(\Ray\Aop\Bind $aopBind): void
130+
{
131+
}
132+
133+
public function visitNewInstance(string $class, SetterMethods $setterMethods, ?Arguments $arguments, ?AspectBind $bind): void
134+
{
135+
}
136+
137+
/** @param array<SetterMethod> $setterMethods */
138+
public function visitSetterMethods(array $setterMethods): void
139+
{
140+
}
141+
142+
/** @param array<Argument> $arguments */
143+
public function visitArguments(array $arguments): void
144+
{
145+
}
146+
147+
/** @param mixed $defaultValue */
148+
public function visitArgument(string $index, bool $isDefaultAvailable, $defaultValue, ReflectionParameter $parameter): void
149+
{
150+
}
151+
};
152+
153+
$result = $setterMethod->accept($visitor);
154+
$this->assertNull($result);
155+
}
44156
}

0 commit comments

Comments
 (0)