Skip to content

Commit 2a8f884

Browse files
authored
Merge pull request #176 from RonasIT/add-testing-for-version-8.4
fix: use invocable controllers with DI with bind by contracts
2 parents a3b8868 + 990ee07 commit 2a8f884

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/Traits/GetDependenciesTrait.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function resolveClassMethodDependencies(object $instance, string $method)
1818
}, (new ReflectionMethod($instance, $method))->getParameters());
1919
}
2020

21-
protected function transformDependency(ReflectionParameter $parameter)
21+
protected function transformDependency(ReflectionParameter $parameter): ?string
2222
{
2323
$type = $parameter->getType();
2424

@@ -29,18 +29,18 @@ protected function transformDependency(ReflectionParameter $parameter)
2929
return interface_exists($type->getName()) ? $this->getClassByInterface($type->getName()) : $type->getName();
3030
}
3131

32-
protected function getClassByInterface($interfaceName)
32+
protected function getClassByInterface($interfaceName): ?string
3333
{
34-
$bindings = Container::getInstance()->getBindings();
34+
$app = Container::getInstance();
35+
36+
$bindings = $app->getBindings();
3537

3638
$implementation = Arr::get($bindings, "{$interfaceName}.concrete");
3739

3840
if (empty($implementation)) {
3941
return null;
4042
}
4143

42-
$classFields = (new ReflectionFunction($implementation))->getStaticVariables();
43-
44-
return $classFields['concrete'];
44+
return get_class(call_user_func($implementation, $app));
4545
}
4646
}

tests/SwaggerServiceTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,23 @@ public function testAddDataWithNotExistsMethodOnController()
682682
$service->addData($request, $response);
683683
}
684684

685-
public function testAddDataWithBindingInterface()
685+
public static function getImplementations(): array
686686
{
687-
$this->app->bind(TestContract::class, TestRequest::class);
687+
return [
688+
[
689+
'implementation' => TestRequest::class,
690+
],
691+
[
692+
'implementation' => fn ($app) => new TestRequest(),
693+
],
694+
];
695+
}
696+
697+
#[DataProvider('getImplementations')]
698+
public function testAddDataWithBindingInterface($implementation)
699+
{
700+
$this->app->bind(TestContract::class, $implementation);
701+
688702
$this->mockDriverGetEmptyAndSaveProcessTmpData($this->getJsonFixture('tmp_data_get_user_request'));
689703

690704
$service = app(SwaggerService::class);

0 commit comments

Comments
 (0)