Skip to content

Commit

Permalink
Add affordance to restore queries while preparing response
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Jan 11, 2023
1 parent 67c4653 commit 1a1ed92
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1444,4 +1444,17 @@ public function preventQueriesWhilePreparingResponse($connection = null)

return $this;
}

/**
* Restores ability for database to query while preparing the request response.
*
* @param string|null $connection
* @return $this
*/
public function allowQueriesWhilePreparingResponse($connection = null)
{
$this['events']->listen(PreparingResponse::class, fn () => $this['db']->connection($connection)->allowQueries());

return $this;
}
}
25 changes: 25 additions & 0 deletions tests/Integration/Foundation/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,31 @@ public function toArray($request)

$this->assertCount(1, DB::getQueryLog());
}

public function testItcanRestoreValue()
{
Schema::create('foo', function ($table) {
$table->id();
});
Route::get('test-route', function () {
return new class('xxxx') extends JsonResource {
public function toArray($request)
{
return [
//
];
}
};
})->middleware(TestMiddleware::class);
DB::enableQueryLog();
$this->withoutExceptionHandling();

$this->app->preventQueriesWhilePreparingResponse();
$this->app->allowQueriesWhilePreparingResponse();
$this->get('test-route')->assertOk();

$this->assertCount(1, DB::getQueryLog());
}
}

class TestMiddleware
Expand Down

0 comments on commit 1a1ed92

Please sign in to comment.