Category : unit-testing

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 ..

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()); ..

Read more

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’ => ..

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’ ..

Read more

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

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 ..

Read more

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 ..

Read more

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 ..

Read more

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 ..

Read more

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 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

while testing a laravel package with Orchestral/testbench, I’m getting error 500. but the exception handling do not say much why the error happend. here is the TestCase.php <?php namespace DivineAlienGourlTests; use DivineAlienGourlGourlServiceProvider; use OrchestraTestbenchTestCase as Orchestra; abstract class TestCase extends Orchestra { public function setUp(): void { parent::setUp(); } protected function getPackageProviders($app) { return [ ..

Read more