#### Category : phpunit

I’ve recently built an app that imports around 4500 rows from Excel into a MongoDB database. I have the import working really well, but now I’m running into the GIGO issue. The excel spreadsheet has a column called ‘program_url’, this column is supposed to be an external link. About a thousand of these are malformed ..

I test code with PHPUnit 9.0. I use Laravel framework 8.* and PHP 7.4 I struggle to test a function that uses request() Here is a very short version of the code I have to test: trait SomeTrait { function someFunction() { //1. retrieve only the documents $documents = request()->only(‘documents’); …. //set an array called .. Read more 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 ..

I am writing tests that need to interact with the queue, the use case is shown below; public function testTryLogJobIsAsync() { // dispatch a job here Artisan::call("queue:work"); // assert if job performed required task e.g check if a file is written to } The job is processed but the trouble is Artisan::call("queue:work"); blocks the rest ..

I am having trouble externally processing jobs on the queue while in test mode using PHPUnit. I have a job that writes a message to the the log file which is dispatched when I visit a route, I thought it could work like the way it does in development where there is a terminal window ..

I’m a newbie here and i have a question: how to write unit test import excel function with model, i write in PHP laravel framework though. hope to receive all your response. Thank you so much. Sourc..

I have about 90 tests written with Pest for my Laravel application. Most of the time all the tests pass but even if I don’t make any change to the codebase i get the same error but on different tests. • TestsFeatureDomainProductResourcesDeleteProductTest > it can remove a product InvalidArgumentException You requested 1 items, but there ..

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

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

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 I’ve been searching around a bit for an answer to this but I can’t seem to find anything concrete. Let’s assume I have a class with multiple methods and in side that class I have a single method which is only called from a Javascript request. That method has a few other method calls inside .. 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 ..

I’m writing tests in laravel-Framework with PHPUnit Version 9.0 (PHP Version 7.4). I test a Job which calls a Trait Function, that I’v already mocked. This is working! But that Trait also calls an Function in another class that should be also mocked. Here is the simplified code to clearify my question: First: my current ..

I have a command to generate documentation but it depends on caching some data from our feature tests. I can run feature tests simply with this command: sail test –filter Feature When I try to invoke this from inside my command: sail apidocs:create I invoke the tests like this: $this->call(‘test’, [‘–filter’ => ‘Feature’]); But this .. Read more I have tow Models Endpoint and Request , my idea was that every Endpoint has many Request and Request Belongsto Endpoint and i am trying to test the Relationship with PhpUnit Test and i keep getting Failed asserting that null is an instance of class Unit test public function testProperties() {$Request = Request::factory()->make(); self::assertEmpty($Request->id); .. 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 am trying to test my Laravel API, but in some cases where I submit a post request I receive a 401 error with the message "Unauthenticated". All the GET requests that are protected are properly accessible and certain POST requests like submitting a status are also doing fine. Why do I get the 401 .. 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", ..

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

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