#### Category : unit-testing

I am looking forward to learn Livewire and its test as well. Now I have created component for registering users and it is working fine, but when I try to do a test I get this error: Failed asserting that an array has the key ‘redirect’. Here are the parts of the code: RegisterTest class ..

I am using phpunit and Laravel. I need to test the routes of a controller that uses TwilioRestClient and I am stuck at the create function! This is the general code of the create function that gives me problems: public function create(Request $request) { //some code$twilioAccount = $this->twilioClient->api->v2010->accounts->create($account_options); $this->twilioClient = new Client($twilioAccount->sid, $twilioAccount->auth_token);$apiKey ..

Trying to write a test for my LoginListener. As the name states, it listens for the login event and then logs a record of this in the activity_log table. When I try run the test it throws the below error: LoginListener::handle(): Argument #1 ($event) must be of type IlluminateAuthEventsLogin, string given What I need to .. Read more there is a case where I gotta create a user record in another user table, using the main User object, then when the User2 record is done, update its username field appending its id. (don’t ask about why, cuz I don’t know) Anyway, I have extremely hard time testing it. I am trying to mock .. Read more I’m testing the deletion of an element, however when I do an assertCount() it still finds me the element, this is because of the softDelete. How can I count only the elements that are not deleted? Here is the code: /** @test */ public function test_auth_users_can_delete_customer() {$user = User::factory()->make(); $this->actingAs($user) ->post(‘/customers/create’, $this->data());$this->assertCount(1, Customer::all()); ..

PHP 7.3.21 & Laravel Framework 8.70.2 I have a validation with the following rules: public function rules() { return [ ‘lat’ => ‘required’, ‘lon’ => ‘required’, ‘time’ => ‘required’, ]; } and the following messages public function messages() { return [ ‘lat.required’ => ‘The latitude is missing’, ‘lon.required’ => ‘The longitude is missing’, ‘time.required’ => ..

I’m writing an e-commerce where a non-authenticated user can add items to it’s cart (using the session_id as a reference). When the user logs in, all the items stored with its’ session_id are transferred to its’ customer_id. e.g. Table cart before login customer_id | session_id | product_id | quantity null sess_123a 1 22 Table cart ..

I have an Laravel User model based on the IlluminateDatabaseEloquentModel. It’s used within a service that I want to unit test. That’s why I want to mock the User with certain properties, in this case it’s about the id which should be set to 23. I create my mock via: /** * @param int $id .. Read more I am using Laravel policies to control authorisation on my api routes. I want to only allow the current user to update their own post. This works fine when i manually run it through the application, but the unit tests fail. The unit tests, redirect to the login screen. route: Route::post(‘/posts/{post:reference}/editDetails’, [PostDetailsApiController::class, ‘update’]) ->middleware(‘can:update,post’); policy: .. Read more I’m working on a new app. Most of my unit-tests work as I expect them to, except one where I’m updating an existing item: /** @test */ function can_edit_thought() {$user = User::factory()->create(); $response =$this->actingAs($user)->post(‘/submit’, [ ‘thought’ => ‘My first thought’ ]);$thought_id = $user->thoughts->first()->id;$response = $this->actingAs($user)->post(‘/submit’, [ ‘thought’ => ‘something different’, ‘thought_id’ ..

Does anyone know why or suggest a solution to why my unit tests are not picking up my code changes when running tests inside my docker container? eg. I make changes to the test and I keep getting the same error. even when I remove the test function completely or add @skip to comments. Source: ..

I’m testing a unit test for passport laravel. So here is my code for the test: Here is the controller for testing: $data = [ "grant_type" => "password", ‘client_id’ =>$client->id, ‘client_secret’ => $parameters[‘client_secret’], ‘username’ =>$userName->name, ‘password’ => $parameters[‘password’], "scope" => "*" ]; // Check Authentication$request = new ServerRequest($data);$request = $request->withParsedBody($data); $response .. Read more$response->assertDownload($result);$response->assertStatus(302); While using this code response always error . Sourc..

I have a Laravel 8 app that I’m working on where I need to use GET parameters (e.g. test?num=1) but unfortunately, I don’t appear to be able to figure out how to run the unit tests against this. I’ve followed the guidance I can find online (including Laravel phpunit testing get with parameters, which looked ..

I want to pass the token to the header to check if the endpoint works and reach null $clientRepository = new ClientRepository();$client = $clientRepository->createPersonalAccessClient( null, ‘Test Personal Access Client’, ‘http://localhost’ ); DB::table(‘oauth_personal_access_clients’)->insert([ ‘client_id’ =>$client->id, ‘created_at’ => new DateTime, ‘updated_at’ => new DateTime, ]); $token =$user->createToken(‘Personal Access Token’)->accessToken; $headers = [‘HTTP_AUTHORIZATION’ => "Bearer .. Read more I have a test file which contains the tests I am running. The test is as simple as the following: public function test_create_time(){$knownDate = Carbon::createFromFormat(‘Y-m-d H:i:s’, ‘2025-05-21 12:00:00’); Carbon::setTestNow($knownDate);$now = Carbon::now(); $data = [ ‘time’ =>$now, ]; $response =$this->post(‘/test’, $data);$flash = session(‘flash_notification’)->first(); } Where I send the changed date with ..

In my Laravel 8 project, I have this action class: <?php namespace AppActionsContent; use IlluminateSupportFacadesConfig; class FixUriAction { public function __invoke(string $uri) { if (preg_match(‘/^https?:///i’,$uri)) { return $uri; } return ‘/’ . Config::get(‘current_lang’)->code . ‘/’ .$uri; } } I want to write unit tests for this class, now I have this code in ..

I write here after many attempts but my problem isn’t solved yet. I want to create a test using PHPUnit on Laravel my class has function described like below: public function test_not_connected_user_can_not_create_new_task(){ $this->withoutExceptionHandling(); //Given we have a task object$task = Task::factory()->make(); //When unauthenticated user submits post request to create task endpoint // He should ..

Using Laravel 8, I’m running some unit tests, but getting this error on one of my tests: Call to undefined method TestsUnitApplicationTest::assertSessionHasErrors() use TestsTestCase; class ApplicationTest extends TestCase … $applicationRef = Application::inRandomOrder()->pluck(‘reference’)->first();$this->post( ‘http://website.test/applications/’ . $applicationRef . ‘/update’, [ ‘title’ => null, ]);$this->assertSessionHasErrors(‘title’); I’ve tried all sorts to test form validation, but cant get ..
I’ve a code for testing create_new_blog, the Blog model has strings data as well as images field which is a list of images. the code for testing is: public function test_create_blog() { $user = User::factory()->create();$blog = Blog::factory( [‘user_id’ => $user->id] )->make();$response = $this->post(‘/api/blogs’,$blog->toArray()); $response->assertStatus(201)->assertJson([ ‘status’ => true, ‘message’ => ‘Blog Created’, ‘id’ .. Read more Tell me please. New to Unit tests, writing a test. The application has helpers (App Helpers Helper, ‘Helper’ => App Helpers Helper :: class added to the aliases in the config) For tests, it is necessary to use the functionality of the helpers, since authorization goes through another service, and I send all requests there .. Read more I am trying to test a specific part of my code, but some dependencies of a Model class make this test very difficult to perform. If those dependencies were on a class that is injected, I could easily mock the class. Is it possible to do something similar when it comes from a database relationship .. Read more I want to write a unit test for a form post request. public function addForm(SaveFormRequest$request) { $this->servicelayer->addFormDetails($request->validated()); return redirect->route(‘register’); } How can I mock $request->validated(); for a FormRequest class. I trying below like$saveRequest = $this->createMock(SaveFormRequest::class); But I got an error: Error: call to a member function call() on null. Thanks in advance. Source: .. Read more$schedule->command(‘abc:some-function {argument}’)->monthlyOn(10, ’09:00′); How can I write a unit test for this command ? Sourc..