Making tests on laravel 9 site I try to catch custom Exception and looking at this Undefind withoutExceptionHandling() example I do in my tests
<?php
namespace Tests\Feature;
//use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Artisan;
use Tests\TestCase;
use Carbon\Carbon;
use App\Models\Article;
use App\Models\User;
use Illuminate\Support\Str;
class ArticlesCrudTest extends TestCase
{
use InteractsWithExceptionHandling;
public function testNegativeArticleGotFailureWithInactiveUserToBeAdded()
{
$loggedUser = clone(self::$loggedUser);
$this->withoutExceptionHandling();
$loggedUser->status = 'I' ; // Inactive
$newArticleObject = \App\Models\Article::factory(Article::class)->make();
$response = $this->actingAs($loggedUser, 'api')->post(route('articles.store'),
$newArticleObject->toArray());
$this->expectException(\App\Exceptions\UserAccountManagerAccessException::class);
$response->assertStatus(400);
}
But I got error message :
here was 1 error:
1) Tests\Feature\ArticlesCrudTest::testNegativeArticleGotFailureWithInactiveUserToBeAdded
App\Exceptions\UserAccountManagerAccessException: Your account must be active in "store" !
/ProjectPath/app/Library/Services/LocalUserAccountManager.php:59
/ProjectPath/app/Repositories/ArticleCrudRepository.php:191
/ProjectPath/app/Http/Controllers/ArticleController.php:86
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:43
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/Route.php:260
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/Route.php:205
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/Router.php:725
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:141
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:50
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:180
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php:126
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php:102
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php:54
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:180
/ProjectPath/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php:44
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:180
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:116
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/Router.php:726
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/Router.php:703
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/Router.php:667
/ProjectPath/vendor/laravel/framework/src/Illuminate/Routing/Router.php:656
/ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:190
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:141
/ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:21
/ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php:31
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:180
/ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:21
/ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php:40
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:180
/ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:180
/ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php:86
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:180
/ProjectPath/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php:62
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:180
/ProjectPath/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php:39
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:180
/ProjectPath/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:116
/ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:165
/ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:134
/ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:545
/ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:340
/ProjectPath/tests/Feature/ArticlesCrudTest.php:108
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Yes, valid exception UserAccountManagerAccessException was raised, but why methods expectException and $response->assertStatus did not work and my test did not passed with sucess?
TOPIC DETAILS: In routes/api.php :
Route::middleware('auth:api') ->group(function () {
Route::apiResource('articles', ArticleController::class);
in app/Http/Controllers/ArticleController.php Repository method is called:
public function store(Request $request)
{
return $this->articleCrudRepository->store(data: $request->only('title', 'text', 'text_shortly',
'creator_id', 'published'), makeValidation: true);
}
and in app/Repositories/ArticleCrudRepository.php Repository :
<?php
namespace App\Repositories;
...
class ArticleCrudRepository
{
public function store(array $data, bool $makeValidation = false): JsonResponse|MessageBag
{
...
$this->userAccountManager->checkPermissions( [ ], "store");
// UserAccountManagerAccessException is raised in method above, BEFORE try block with transaction...
DB::beginTransaction();
try {
$article = Article::create([
'title' => $data['title'],
'text' => $data['text'],
'text_shortly' => $data['text_shortly'],
'creator_id' => $data['creator_id'],
'published' => $data['published'],
]);
DB::Commit();
$article->load('creator');
return response()->json(['article' => (new ArticleResource($article))], 201);
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
\Log::info($errorMessage);
DB::rollback();
return sendErrorResponse($e->getMessage(), 500);
}
}
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.19",
"laravel/passport": "^11.3",
Thanks in advance!
I need to set such order:
$this->expectException(\App\Exceptions\UserAccountManagerAccessException::class);
And after that :
$response = $this->actingAs($loggedUser, 'api')->post(route('articles.store')
That works!