We are currently working for a booking website and there are many partners that can integrate with our website via REST APIs. We need to give our partner a test environment during the integration process and we have three suggestions 1- Create the testing booking and production booking on the same database and the same ..
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 ..
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 ..
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 ..
I’m very lost with this, I want to make a test to test the Password Grant Tokens flow of Laravel Passport, but I don’t know how. I can’t use the credentials generated by the php artisan client –password because they are saved in the original database, not in the testing database. I tried to make ..
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 ..
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’ ..
I’m working under TDD, and I’m making a test to get a token using Password Grant Tokens with Laravel Passport, I’ve made the test manually (sending a curl with the client id, client secret, user and password) to the endpoint, and it works! It returns me a token, I’m using a client id and a ..
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); ..
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 ..

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 ..
i have a multi dimention array that i want to write test for . i read on web that there was a function called arrayHasSubKey but it does not exists any more it seems . i am using laravel 5.8 and i want to check if this array has a subkey this is what my ..
in my registration process if some setting is active i add a realtionship to user model called Vendor so if the setting is active when user is being created a model (relationship) will be created for him too . now what i want to do is to write a test for it that if the ..
I’m writing tests for my application and struggling to understand whi Storage class is not building the expected file url. I’m having thist test that fails on asserting that expected url equals the builded one, so i put an if in my code and dd() stuff out to understand, and that’s the result: IF in ..
I have just used Laravel Permission plugin with Laravel 8, and now, I need to seed 3 tables before each tests Roles, Permissions: Without this, my functional tests run in 57 secs. After including seeding of those 2 tables in setUp() method, test runs in 3 minutes and 8 seconds 🙁 public function setUp(): void ..
I was reading Laravel document HTTP tests and a question occurred. I can’t tell the difference between assertLocation($uri) and assertRedirect($uri), since both are for redirecting to specific uri. Anyone could help would be so much appreciated. Sourc..
TLDR; How can I run php unit tests in Laravel 7 without removing all output that is emitted during the execution of a test? Full question: I’m trying to run a few tests in Laravel 7 with PHPUnit 8.5. In these tests, I am dumping information as the test isn’t simply pass or fail, but ..
I’m working on a Laravel Project and try to integrate Laravel Scout. For testing with dusk I have created a .env.dusk.local environment file, that is used for testing. But when I try to run php artisan serve –env=dusk.local it just runs the default .env file. Also all other .env files don’t work. Im using Laravel ..
The only similar answer I found was this one, but it relates to ASP.NET. Was wondering if Laravel does something similar or the same, for I was wanting to test the controller instance used by the Route endpoint. Is it generated before the endpoint is used? And if so, any idea whether it is posible ..
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"/> ..
I have a simple LIvewire component consisting of a form to create a new User, and it work, i can tell since i’ve been asked to write tests after the project already reached staging. The Livewire component use a new User instance (non presisted, i.e. user->exists is false) to store inserted data before persisting it, ..
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 ..
This is my first experience with testing in general so sorry for the dummy questions. I was wondering which would be the best practice to write tests in Laravel. For example, if I want to test database, is it required to make separate db for testing or is it ok to use default db? I ..
I am getting this error whenever I run my tests.BaseFormRequest is base class for all the FormRequest class in my app which handles JSON response as per my need. All the tests were running fine few days ago. I had to do a clean OS install. After setting up my OS. I pulled all my ..
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’)), ..