Laravel Common PHP Unit Testing CRUD made easy

Laravel Common PHP Unit Testing CRUD made easyJuan Miguel TorresBlockedUnblockFollowFollowingFeb 24If you’re doing PHP unit test in Laravel, this code snippets might be useful for you.

Basically, the most common test we made on every features is CRUD.

So I made this code to use it on every feature.

Let’s get started.

First, I created a utility function for factory just like Jeffrey Way did on his tutorial.

 :)<?php/* testsutilitiesfunctions.

php */function create($class, $attributes = [], $times = null){ return factory($class, $times)->create($attributes);}function make($class, $attributes = [], $times = null){ return factory($class, $times)->make($attributes);}function raw($class, $attributes = [], $times = null){ return factory($class, $times)->raw($attributes);}Then add the file path in composer.

json autoload-dev.

"autoload-dev": { "psr-4": { "Tests": "tests/" }, "files": [ "tests/utilities/functions.

php" ]}So on TestCase.

php, I added the function that we will be using on every feature test.

You can get it here: https://gist.

github.

com/wanmigs/8ed74633ddff91062f51e8b9aa2778a9So now we have added the code on TestCase.

php, let’s now create a test.

To make it easier let’s create a model with migrations, controller and factory.

php artisan make:model Demo -aThis will generateModel created successfully.

Factory created successfully.

Created Migration: 2019_02_24_181538_create_demos_tableSo on migration, let’s have this as example.

Schema::create('demos', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('address')->nullable(); $table->timestamps();});Then, on you factory DemoFactory.

php$factory->define(AppDemo::class, function (Faker $faker) { return [ 'name' => $faker->name, 'address' => $faker->streetAddress ];});DemoController.

phppublic function store(Request $request){ request()->validate(['name' => 'required']); $demo = Demo::create(request()->all()); return response($demo, 201);}public function update(Request $request, Demo $demo){ request()->validate([ 'name' => 'required', ]); $demo = tap($demo)->update(request()->all()); return response()->json($demo);}public function destroy(Demo $demo){ $demo->delete(); return response([], 204);}public function destroyAll(){ Demo::whereIn('id', request('ids'))->delete(); return response([], 204);}Now we have set the controller.

We’re going add it in our routeRoute::resource('demo', 'DemoController');Route::delete('demo', 'DemoController@destroyAll') ->name('demo.

destroyAll');You can check resource controller on laravel documentation https://laravel.

com/docs/5.

7/controllers#resource-controllersLet’s get real on unit testing now.

Make sure to add this on phpunit.

xml to have our test data stored in memory only not in your actual database.

<env name="DB_CONNECTION" value="sqlite"/><env name="DB_DATABASE" value=":memory:"/>Create a feature test now.

First, we’re going to make a Create testphp artisan make:test CreateDemoTestCreateDemoTest.

phpAs you can see on public function setUp, we set the base route and base model that we defined in TestCase.

php.

Also the create method in TestCase.

php returns json reponse so we can able to assert json and status.

// This are the parameters the function have$this->create($attribute = [], $model = '', $route ='');//Also returns json response so you can assert json or status// return json response so you can assert json or status$this->create()->assertJsonFragments();// if you have custom route$this->create([], 'AppDemo', custom.

route');//you can have the first parameter empty if base model is set$this->create([], null, 'custom.

route');Run the test.

$ vendor/bin/phpunit –filter CreateDemoTestPHPUnit 7.

5.

6 by Sebastian Bergmann and contributors.

3 / 3 (100%)Time: 1.

05 seconds, Memory: 26.

00 MBOK (3 tests, 5 assertions)Let’s have UpdateDemoTest nowphp artisan make:test UpdateDemoTest// This are the parameters the function have$this->update($attribute = [], $model = '', $route ='');//Also returns json response so you can assert json or statusRun the test$ vendor/bin/phpunit –filter UpdateDemoTestPHPUnit 7.

5.

6 by Sebastian Bergmann and contributors.

2 / 2 (100%)Time: 905 ms, Memory: 26.

00 MBOK (2 tests, 3 assertions)Lastly, DeleteDemoTestphp artisan make:test DeleteDemoTest$this->destroy($model = '', $route ='');// return json response so you can assert json or status$this->destroy()->assertJsonFragments();// if you have custom route$this->destroy('AppDemo', custom.

route');//you can have the first parameter empty if base model is set$this->destroy(null, 'custom.

route');Run the test$ vendor/bin/phpunit –filter DeleteDemoTestPHPUnit 7.

5.

6 by Sebastian Bergmann and contributors.

3 / 3 (100%)Time: 1.

02 seconds, Memory: 26.

00 MBOK (3 tests, 4 assertions)ConclusionThat’s it!.With very little effort, we’ve implemented a re-usable code in php unit testing for CRUD.

We were able to run the test successfully.

Thanks for reading!.

. More details

Leave a Reply