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

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 In Laravel below 5.6 there is option to remote code execution, remote code execution run by the phpunit test to avoid that, i want to know how to remove on production Sourc.. Read more I’m having troubles with feature tests and data seeming to both exist and not exist depending on where and when that data is being retrieved from. My test only seems to work if I run them one at a time but if run them all together using php artisan test then there seems to be .. 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 ..

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

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 have a class thats role is to loop through database records and create instances of the class names it finds. Each of these classes is created using App::make(). I would like to unit test this function but i cant find a way to assert app::make is never called. class Switcher { public function process($foo) ..

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

I’m writing unit tests for an API built in Laravel. I have a database instance specific for running tests against and have configured the phpunit.xml to tell Laravel to use that db during testing. However, my issue is when it comes to tests that perform a HTTP request to a route: $response =$this->json(‘POST’, route(‘api.login’), ..

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

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

I have a Basic Class Helper. namespace AppHelpers; class CommonUtility { public static function foo() { return ‘foo’; } } And it’s used in User.php <?php class User extends Authenticatable public function test() { return CommonUtility::foo(); } } And now I wanted to write a test with mocking CommonUtility class. Can anyone know about this ..

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

I’m playing a fresh install of Livewire 2 and Laravel 8, but in trying to run a phpunit test, I’m getting a RuntimeException : A facade root has not been set.. I’m tried the standard cache cleaning, but nothing seem to be working. Anyone else seeing this? Below if the full test I’m running. <?php ..

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

I just begin to work on this big project that has zero tests. The idea is to TDD every new feature and bug and with time we will increase the test coverage. I don’t make tests using SQLite in-memory DB. I do prefer to use MySql because it’s the same DB that I use in ..

I just begin to work on this big project that has zero tests. The idea is to TDD every new feature and/or bug, and with time we will increase the test coverage. I don’t make tests using SQLite in-memory DB. I do prefer to use MySql because it’s the same DB that I use in ..

I have a Model method like public static function getBusinessCardFieldsOptions() { return implode(‘,’, array_values(self::SEND_BUSINESS_CARD_FIELD)); } And I wanted to make an assertion that the implode and array_values method has been called at least once. Does anyone know how to mock PHP inbuilt methods? Sourc..

I’m using laravel eloquent to retrive data. When i run my test echo $company[‘name’]; like this it passes. Bun when i do this way echo$company->name; It gives me next error ErrorException: Trying to get property ‘name’ of non-object Sourc..

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

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