Skip to content

Commit f028ec8

Browse files
committed
fix: use invocable controllers with DI with bind by contracts
1 parent 3506f8f commit f028ec8

File tree

5 files changed

+18
-170
lines changed

5 files changed

+18
-170
lines changed

tests/SwaggerServiceTest.php

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

687-
public function testAddDataWithBindingInterface()
687+
public static function addDataWithBindingInterface(): array
688688
{
689-
$this->app->bind(TestContract::class, TestRequest::class);
689+
return [
690+
[
691+
'concrete' => TestRequest::class,
692+
],
693+
[
694+
'concrete' => fn ($app) => new TestRequest(),
695+
],
696+
];
697+
}
698+
699+
#[DataProvider('addDataWithBindingInterface')]
700+
public function testAddDataWithBindingInterface($concrete)
701+
{
702+
$this->app->bind(TestContract::class, $concrete);
703+
690704
$this->mockDriverGetEmptyAndSaveProcessTmpData($this->getJsonFixture('tmp_data_get_user_request'));
691705

692706
$service = app(SwaggerService::class);
@@ -875,44 +889,4 @@ public function testAddDataWhenInvokableClass()
875889

876890
app(SwaggerService::class)->addData($request, $response);
877891
}
878-
879-
public function testAddDataWhenInvokableClassWithBindingContractToObject()
880-
{
881-
$this->mockDriverGetEmptyAndSaveProcessTmpData($this->getJsonFixture('tmp_data_get_user_request_invoke_bind_closure'));
882-
883-
$this->app->bind(TestRequestContract::class, fn () => new TestRequest());
884-
885-
$request = $this->generateRequest(
886-
type: 'get',
887-
uri: 'users',
888-
controllerMethod: '__invoke',
889-
controllerClass: InvokableTestController::class,
890-
);
891-
892-
$response = $this->generateResponse('example_success_user_response.json', 200, [
893-
'Content-type' => 'application/json',
894-
]);
895-
896-
app(SwaggerService::class)->addData($request, $response);
897-
}
898-
899-
public function testAddDataWhenInvokableClassWithBindingContractToClassName()
900-
{
901-
$this->mockDriverGetEmptyAndSaveProcessTmpData($this->getJsonFixture('tmp_data_get_user_request_invoke_bind_closure'));
902-
903-
$this->app->bind(TestRequestContract::class, TestRequest::class);
904-
905-
$request = $this->generateRequest(
906-
type: 'get',
907-
uri: 'users',
908-
controllerMethod: '__invoke',
909-
controllerClass: InvokableTestController::class,
910-
);
911-
912-
$response = $this->generateResponse('example_success_user_response.json', 200, [
913-
'Content-type' => 'application/json',
914-
]);
915-
916-
app(SwaggerService::class)->addData($request, $response);
917-
}
918892
}

tests/TestCase.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,12 @@ protected function generateRequest(
126126
array $headers = [],
127127
array $routeConditions = [],
128128
string $controllerMethod = 'test',
129-
string $controllerClass = TestController::class,
130129
): Request {
131130
$request = $this->getBaseRequest($type, $uri, $data, $pathParams, $headers);
132131

133-
return $request->setRouteResolver(function () use ($uri, $request, $controllerMethod, $routeConditions, $controllerClass) {
132+
return $request->setRouteResolver(function () use ($uri, $request, $controllerMethod, $routeConditions) {
134133
$route = Route::get($uri)
135-
->setAction(['controller' => $controllerClass . '@' . $controllerMethod])
134+
->setAction(['controller' => TestController::class . '@' . $controllerMethod])
136135
->bind($request);
137136

138137
foreach ($routeConditions as $condition) {

tests/fixtures/SwaggerServiceTest/tmp_data_get_user_request_invoke_bind_closure.json

Lines changed: 0 additions & 108 deletions
This file was deleted.

tests/support/Mock/InvokableTestController.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/support/Mock/TestRequestContract.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)