Category : testing

I have a factory with the below definition public function definition() { $startDate = Carbon::createFromFormat(‘Y’, $this->faker->year); $endDate = $startDate->addYear(); return [ ‘name’ => $startDate->year, ‘start_date’ => $startDate->format(‘Y-m-d’), ‘end_date’ => $endDate->format(‘Y-m-d’), ]; } So this generates an array like below [ ‘name’ => 2020, ‘start_date’ => ‘2020-01-30’, ‘end_date’ => ‘2021-01-30’ ] The above worked as expected ..

Read more

I’m writing a basic repository with basic methods, to have it extended from other repositories, e.g.: BaseRepository ├─ UserRepository ├─ PostRepository └─ CommentRepository I want to test my BaseRepository with a custom testing model, not with an actual application model (e.g. User, Post, etc), so i need to create this custom model with its migration ..

Read more

I’m trying to write a feature test in RESTful API that I have been created in laravel 8. I have ChatController that gets user chats by getUserChats method: // get chats for user public function getUserChats() { $chats = $this->chats->getUserChats(); return ChatShowResource::collection($chats); } that called by this route: Route::get(‘chats’, [ AppHttpControllersChatsChatController::class, ‘getUserChats’ ]); this is ..

Read more

I am using a transaction accompanied by updateOrCreate(). As you can infer, things could go awry if multiple connections access and update the same id, a race condition in writing into the DB (horrible!). I am also locking that specific row that is being dealt with in my model. My abbreviated code looks as follows ..

Read more

I have written a basic api which I am trying to write test for but the test is returning 404 instead of 422. How can i fix this so the validation is returning 422? https://github.com/shorif2000/api in my auth controller public function register(Request $request) { die(); $validatedData = $request->validate([ ‘givenName’ => ‘required|max:55’, ‘familyName’ => ‘required|max:55′, ’email’ ..

Read more

I am working with Laravel 8.25. I have a Service class that has the following method: public function getUsersBusinessManagers() { $user = Auth::user()->id; return $user->businessManagers; } My test for this method is: public function testCorrectBusinessManagersRetrievedForUser() { $user = User::factory() ->hasAttached( BusinessManager::factory()->count(1), [‘user_fb_bm_id’ => ‘test’] ) ->create(); $businessManagerService = new BusinessManagerService(); $usersBusinessManagers = $businessManagerService->getUsersBusinessManagers(); $this->actingAs($user)->assertEquals($user->businessManagers()->first()->id, $usersBusinessManagers->first()->id); ..

Read more

I have a service class VideosHandler in which constructor I get database records for further usage in this service class. Then, I have a VideosImporter command where I pass the service as a dependency in the constructor: public function __construct(protected VideosHandler $videosHandler) { parent::__construct(); } Because of this dependency, whenever I run the tests I’m ..

Read more

I am trying to use Paratest for my application. My tests fail because of race-conditions and DEADLOCKS because they all use the same database. Thus, I would like to create one database per process. In the docs, they mention this: https://github.com/paratestphp/paratest/tree/2.2.0#test-token So I figured that getenv(‘TEST_TOKEN’) is an integer value of a process. I have ..

Read more

I’m trying to writing testing in Laravel with phpunit. However, when I run testing by php artisan test, laravel auto write and refresh on my main database. I’ve tried to create another database and set phpunit run with it but it doesn’t work. This is my phpunit.xml config: <php> <server name="APP_ENV" value="local"/> <server name="BCRYPT_ROUNDS" value="4"/> ..

Read more

In laravel or none laravel application we have tests like Feature and Unit tests (or whatever, browser, integration they are the same things in different languages as i understood). I read from different places and some of them says that "do not use database operations in your unit tests" and other article gives an example ..

Read more

I want to use Sqlite driver for both test database and as actual database. My .env file is: DB_CONNECTION=sqlite It’s okay, i can use sqlite for actual database and for testing i defined something like this under my connections array in config/database.php: ‘testing’ => [ ‘driver’ => ‘sqlite’, ‘url’ => env(‘DATABASE_URL’), ‘database’ => env(‘DB_DATABASE’, database_path(‘test.sqlite’)), ..

Read more