Category : phpunit

I have a problem with writing test for pdf generation. I have controller, service and repository, so service is injected in my controller and it’s calling method which is part of repository class. My test always returns error 500, probably because missing dependencies. Does anybody knows how to make test passing? This is the code: ..

Read more

I’m writing automated tests for a legacy laravel project, 5.8.38. I have this test method. public function testUserReceivesAnEmailWithAPasswordResetLink() { Notification::fake(); $user = factory(User::class)->create([ ’email’ => ‘[email protected]’, ]); $this->post($this->passwordEmailPostRoute(), [ ’email’ => ‘[email protected]’, ]); $this->assertNull($token = DB::table(‘password_resets’)->first()); Notification::assertSentTo($user, ResetPassword::class, function ($notification, $channels) use ($token) { return Hash::check($notification->token, $token->token) === true; }); } This always fails because ..

Read more

I have this class: <?php namespace AppHttpControllersAlerts; use AppHttpControllersController; use AppModelsAlert; use AppModelsFiredAlert; use AppModelsUnitReport; use AppNotificationsAlertSetup; use CarbonCarbon; use IlluminateHttpRequest; use IlluminateSupportFacadesNotification; class AlertController extends Controller { /** * @param Alert $alert * @param null $schema */ public static function checkForInactivity(Alert $alert, $schema = null): void { …some code… } /** * Get int ..

Read more

I try to use phpunit with Laravel and Spatie but i have a issue. I have this test : public function testBasicTest() { $user = User::where(‘id’, 2)->first(); $response = $this->actingAs($user, ‘api’)->json(‘POST’, ‘/providersList’, [ ‘database’ => ‘test’ ]); $response->assertStatus(200); } But i have a 401 error Expected status code 200 but received 401. Failed asserting that ..

Read more

I am struggling to bind a model to the request in a unit test so that the relationship of the model can be retrieved in the form request. This is the form request: class TimeSlotUpdateRequest extends FormRequest { public function rules(): array { return [ ‘time’ => [ ‘required’, ‘string’, ‘max:50’, Rule::unique(‘time_slots’) ->where(‘schedule_id’, $this->timeSlot->schedule->id) ->ignore($this->timeSlot), ..

Read more

Basic Factory: <?php namespace DatabaseFactories; use AppFlareModelsNotification; use IlluminateDatabaseEloquentFactoriesFactory; class NotificationsFactory extends Factory { /** * The name of the factory’s corresponding model. * * @var string */ protected $model = Notification::class; /** * Define the model’s default state. * * @return array */ public function definition() { return [ ]; } } Attached to ..

Read more

i’m new with the tests in Laravel (7.x), i create two feature tests, but the seconds don’t run with php artisan test my UserTest.php <?php namespace TestsFeature; use IlluminateFoundationTestingRefreshDatabase; use IlluminateFoundationTestingWithFaker; use TestsTestCase; class UserTest extends TestCase { /** * A basic feature test example. * @test * @return void */ public function an_authtenticated_user_can_get_own_details() { ..

Read more

Fatal error: Uncaught IlluminateContractsContainerBindingResolutionException: Target [IlluminateContractsDebugExceptionHandler] is not instantiable. in /builds/xxx/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 1017 IlluminateContractsContainerBindingResolutionException: Target [IlluminateContractsDebugExceptionHandler] is not instantiable. in /builds/xxx/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 1017 This error occurs after Gitlab passes the test which causes the pipeline to fail which isn’t nice. All the tests pass locally and the error does not occur there – ..

Read more

I am facing problem in unit testing my APIs, I am using stancl/tenancy package for multi tenancy. https://tenancyforlaravel.com/docs/v3/testing when I am trying to run this test case /** @test */ public function some_test() { $data = [ ’email’ => ‘[email protected]’, ‘password’ => ‘test123’, ]; $response = $this->post(route(‘api.user.login’), $data)->assertStatus(200); } this is my setUp() method public ..

Read more

When using Laravel/Lumen: I can apparently reference helper methods like storage_path(), base_path() etc in various PHPUnit test methods, but when I try to reference them in the setUp() method for my test, I receive the following error: Error : Call to undefined method IlluminateContainerContainer::basePath() How does one get these helper methods to work? Is it ..

Read more

I have the following written the following TEST in tests/Feature/emailPollTest.php … …. Mail::fake(); $ja=["id"=>1, ‘status’=>’error’,’log’=>’log test’]; $response = $this->post(‘/api/store’,$data); $response ->assertStatus(201); $response->assertJson($ja); $order = new NotifyPoll($ja); Mail::assertSent(function (NotifyPoll $mail) use ($order) { $count = 0; if($mail->itemPoll[‘id’] === $order->itemPoll[‘id’]) $count++; if($mail->itemPoll[‘status’] === $order->itemPoll[‘status’]) $count++; if($mail->itemPoll[‘log’] === $order->itemPoll[‘log’]) $count++; return 3 === $count; }); Mail::assertSent(NotifyPoll::class, 1); can ..

Read more

I’m using Pest PHP (with Laravel) and i’m trying to run test with a given role but i already have 403 status (Forbidden). I’m using spatie/laravel-permission to manage roles. This is my route : Route::get(‘liste’, IndexAccessController::class)->name(‘access.index’)->middleware(‘role:team’); And this is my Pest PHP Test: beforeEach(function () { $me = factory(User::class)->create(); $role = SpatiePermissionModelsRole::where(‘name’, ‘team’)->first(); $me->assignRole($role); $this->actingAs($me); ..

Read more

I have running project on localhost port 8000, and at first seek it opens home page (‘/’) perfecly but for some reasons <?php namespace TestsFeature; use IlluminateFoundationTestingRefreshDatabase; use TestsTestCase; class ExampleTest extends TestCase { /** * A basic test example. * * @return void */ public function testBasicTest() { $response = $this->get(‘/’); $response->assertStatus(200); } } ..

Read more