Category : phpunit

Making a test for database with Laravel (8.44.0) assetion assertModelExists(), fails on an error. Error : Call to undefined method TestsFeatureCommuneTest::assertModelExists() The test class looks like this <?php namespace TestsFeature; use AppModelsCommune; use IlluminateFoundationTestingConcernsInteractsWithDatabase; use IlluminateFoundationTestingRefreshDatabase; use IlluminateFoundationTestingWithFaker; use TestsTestCase; class CommuneTest extends TestCase { use RefreshDatabase, InteractsWithDatabase; /** * Test can save a commune ..

Read more

I am trying to create a test for a feature I’ve written. The logic is quite simple: From the api.php I am calling the store method: Route::group([‘prefix’ => ‘/study/{study}/bookmark_list’], function () { … Route::post(‘/{bookmarkList}/bookmark’, ‘[email protected]’); … }); thus I am injecting the study and the bookmark list. My controller passes down the parameters public function ..

Read more

What’s the secret of testing a mailable was sent from a notification? Testing the notification: it(‘does send notification when model is deleted’, function() { Notification::fake(); $this->model->delete(); Notification::assertSentTo($this->model->user, MyModelDeletedNotification::class); }); Passes. Testing the mailable: it(‘does send email when model is deleted’, function() { Mail::fake(); $this->model->delete(); Mail::assertQueued(MyModelDeletedMail::class, 1); }); Fails. No mails queued. When a model is ..

Read more

I am using Pest library to write tests in laravel. I created the my-laravel-application/tests/Integration directory in laravel and defined a new test suite in phpunit.xml <testsuite name="Integration"> <directory suffix=".test.php">./tests/Integration</directory> </testsuite> So that laravel acknowledges the test files in Integration directory and I could write my integration tests in a separate directory with a proper name(Integration ..

Read more

Attempting to write unit testing for some ui validation messages that come back formatted like the following and completely lost Unprocessable Content Cache-Control: no-cache, private Content-Type: application/json Date: Fri, 31 Dec 2021 15:55:48 GMT {"errors":{"assets":["filename must have a value"],"questions":[],"targeting_sets":[],"lists":[]}} there error response gets generated using the following function private function errorResponse() { return response()->json([ ‘errors’ ..

Read more

I created my feature test; ProfilesControllerTest.php <?php namespace TestsFeature; use AppUser; use IlluminateFoundationTestingRefreshDatabase; use IlluminateFoundationTestingWithFaker; use TestsTestCase; class ProfileControllerTest extends TestCase { use RefreshDatabase; /** * A basic feature test example. * * @return void */ public function test_the_profile_page_is_rendered() { // First The user is created $user = User::factory()->create(); //act as user $this->actingAs($user); // Then ..

Read more

i always used reuqest validation inside the controller method directly but i have seen lots of laravel developers suggesting the use of a FormRequest object and injecting it in the controller method as a replacement for request , yes it looks cleaner and straight forward when it comes to customizing some validation rules or messages ..

Read more

My testing class: class QuarterlyReportFormController extends Controller { public function uu(Request $request, QuarterlyReportForm $quarterlyReportForm) { dd($quarterlyReportForm->id); } } in routes/web.php Route::post(‘/aa/{quarterlyReportForm}/uu’, ‘[email protected]’); My test function: public function testUu() { $obj = (new QuarterlyReportForm)->create([ ‘user_id’ => $this->createUser(), ‘complete’=> 0, ‘active’ => 1 ]); $this->assertNotEmpty($obj->id); $this->assertEquals(1, $obj->id); $responseUU = $this->post( "aa/{$obj->id}/uu", [‘complete’ => 1]); dd($responseUU); //this is ..

Read more

I am trying to send a request to my Laravel end point using PHPUnit. $stock = new stdClass(); $stock->product_id = $product->id; $stock->quantity = $quantity; $attributes[‘stock’] = [ $stock ]; $this->json(‘POST’, ‘/api/posts/’.$postRequest->id, $attributes, [‘Accept’ => ‘application/json’]) ->assertStatus(200) ->assertJsonFragment([ ‘id’ => $stock->product_id, ‘content’ => $stock->quantity ]); The end point seems to be expecting a form input as ..

Read more

I have the following test in my app. However the response isn’t passing the test, and I can’t see why… public function test_get() { $user = $this->signInAsUser(); $product = factory(‘AppModelsProduct’)->create(); factory(‘AppModelsStock’)->create([ ‘product_id’ => $product->id, ‘qty’ => $qty = rand(1, 100), ]); $this->json(‘GET’, ‘/api/stock’, , [‘Accept’ => ‘application/json’]) ->assertStatus(200) ->assertJson([ ‘data’ => [ ‘product_id’ => "$product->id", ..

Read more

I have one issue with testing and I hope you can help me. I have a scheduled job that collects information about others websites. it is a web crawler. This is the job: <?php namespace AppJobs; use AppModelsPost; use IlluminateBusQueueable; use IlluminateContractsQueueShouldBeUnique; use IlluminateContractsQueueShouldQueue; use IlluminateFoundationBusDispatchable; use IlluminateQueueInteractsWithQueue; use IlluminateQueueSerializesModels; use IlluminateSupportFacadesHttp; use IlluminateSupportStr; class ..

Read more

I’m using Dependency injection to call custom services in Laravel and it works fine. But when i inject those dependencies into my Phpunit test case classes using interfaces, i receive the following error: Target [AppServicesInterfacesCarServiceInterface] is not instantiable. although the interface has been bound to the target concrete class in the provider correctly. i’ve used ..

Read more

I’m trying to set a sensorId property that will be used by every method in my feature test. The problem is that it is not persisting between tests. Here is my test class: class LogApiPostTest extends TestCase { public $sensorId; public function setUp(): void { parent::setUp(); $this->sensorId = ‘SEN’ . rand(10000000, 999999999); } public function ..

Read more