Category : testing

I have an Laravel app with about 30 permissions. App also uses plenty of endpoints. Now I want to write feature tests for each endpoint, checking every permission on it. There is my question: Should I write separate method for each permission like that: public function test_if_user_can_create_article_having_articles_index_permission() { $user = $this->makeUserWithSuchPermisssion(); $this->actingAs($user)->post(‘/articles’)->assertStatus(403) } public function ..

Read more

I’m using Laravel for my project and I’m new to unit/feature testing so I was wondering what is the best way to approach more complicated feature cases when writing tests? Let’s take this test example: public function testSucceedIfConnectAuthorised() { $connection = factory(Connection::class)->make([ ‘sender_id’ => 1, ‘receiver_id’ => 2, ‘accepted’ => false, ‘connection_id’ => 5, ]); ..

Read more

Ì got a function that returns a redirect with a success message as follows: public function update(Request $request) { … return redirect()->route(‘my.route’)->with(‘success’, ‘All objects have been updated.’); } I wrote a feature request to test the functionality of this function. public function test_user_can_update() { $response = $this->put(route(‘my.route’), [ ‘value’ => 15 ]); $response->assertStatus(302); $response->assertSessionHasNoErrors(); $this->assertEquals(‘All ..

Read more

I try to use Cypress for tests. With Laravel and Yarn as a package (v8.3.1) on MacBook with Apple M1 chip. Installation was ok, but all I can see after cypress open is: Cypress failed to start. This may be due to a missing library or dependency. https://on.cypress.io/required-dependencies Please refer to the error below for ..

Read more

Sanctum Version: ^2.11.2 Laravel Version: 8.54 PHP Version: 7.3|^8.0 Database Driver & Version: Description: deleting current user token works fine but in test it throw exception Steps To Reproduce: routes/api.php Route::prefix(‘auth’)->middleware(‘auth:sanctum’)->group(function () { Route::delete(‘/sign_out’, [AuthController::class, ‘signOut’]); /// other routes }); appHttpControllersApiAuthAuthController.php // * delete the current access token public function signOut(Request $request) { $request->user()->currentAccessToken()->delete(); return ..

Read more

I’m trying to test Laravel with different users roles. I have made 4 tests (actually, 2 tests with 2 different approach): use RefreshDatabase; /** @test */ public function if_administrators_can_view_user_list_post() { $this->seed(); $user = User::find(1); $credentials = [ ’email’ => $user->email, ‘password’ => ‘password’ ]; $response = $this->post(route(‘login’), $credentials); $this->get(route(‘admin.users.index’)) ->assertStatus(200); $this->post(route(‘logout’)); } /** @test */ ..

Read more

PS C:xampphtdocslaravel_testing> PHP artisan test Warning: TTY mode is not supported on the Windows platform. RUNS TestsUnitAccountantHelperTest • it can find profit Tests: 3 pending FAIL TestsUnitAccountantHelperTest ✕ it can find profit Tests: 1 failed, 2 pending Error Class ‘AppAccountantHelper’ not found at C:xampphtdocslaravel_testingtestsUnitAccountantHelperTest.php:16 12| * @test 13| */ 14| public function it_can_find_profit() 15| { ..

Read more

I’m implementing the password reset functionality described in the Laravel docs at https://laravel.com/docs/8.x/passwords. My method for resetting passwords is as follows: public function doPasswordReset(Request $request) { $request->validate([ ‘token’ => ‘required’, ’email’ => ‘required|email’, ‘password’ => ‘required|min:8|confirmed’, ]); $status = Password::reset( $request->only(’email’, ‘password’, ‘password_confirmation’, ‘token’), function ($user, $password) { $user->forceFill([ ‘password’ => Hash::make($password) ]); //remember token ..

Read more

I did some laravel testing for my project but I’ve got this problem: file_put_contents(): Exclusive locks are not supported for this stream I’ve already fixed this code in the filesystem "return file_put_contents($path, $contents, $lock ? LOCK_SH : 0);" but it’s not working for me. I am using Laravel 8, PHP 8.0.9, PHPUnit 9.5.8, and MAC ..

Read more

Imagine you have a controller or some code that looks something like this: <?php namespace AppHttpControllers; use IlluminateHttpRequest; use IlluminateHttpJsonResponse; class TeamController { public function create(Request $request): JsonResponse { $request->validated(); $team = new Team([‘name’ => $request->get(‘name’)]); if (!$team->save()) { // some really important logic you want to cover // cover by a test. } … ..

Read more

Argument 1 passed to IlluminateFoundationTestingTestCase::actingAs() must implement interface IlluminateContractsAuthAuthenticatable, instance of IlluminateDatabaseEloquentCollection given I have also tried some of the following methods, but none of them worked: Argument 1 passed to IlluminateFoundationTestingTestCase::actingAs() Problem with testing method with actingAs I hope there is another way I can solve this problem. This is my ClipartTest.php file: <?php ..

Read more

I’m using PHPUnit for testing my Laravel project. ?php namespace TestsFeature; use AppModelsUser; use TestsTestCase; class LoginTest extends TestCase { public function test_user_can_login(): { $user = User::factory()->create(); $response = $this->postJson(‘/api/auth/login’, [ ’email’ => $user->email, ‘password’ => $user->password, ]); $response ->assertStatus(200) ->assertHeader(‘Authorization’) ->assertJson([ ‘success’ => ‘Usuário logado com sucesso!’, ]); } } I want to create ..

Read more

Following is code of observer Nova::whenServing(function (NovaRequest $request) use ($employee) { $user = new User(); $user->name = $employee->name; $user->email = $employee->email; $user->password = Hash::make(‘password’); $user->timezone = ‘Europe/Zurich’; $user->save(); $user->refresh(); $role = Role::find($employee->role_id); $user->assignRole($role->slug); $employee->user()->save($user); $employee->refresh(); }); Following is code for test case /** @test */ public function creating_employee_creates_new_user_with_role() { $vehicle = AppModelsVehicle::all()->random(); $rolePerm = RolePermission::select(‘role_id’) ..

Read more