Laravel Common PHP Unit Testing CRUD made easy

Juan Miguel Torres
Feb 24 · 4 min read
<?php/* tests\utilities\functions.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);
}
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
},
"files": [
"tests/utilities/functions.php"
]

}
php artisan make:model Demo -a
Model created successfully.
Factory created successfully.
Created Migration: 2019_02_24_181538_create_demos_table
Schema::create('demos', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('address')->nullable();
$table->timestamps();
});
$factory->define(App\Demo::class, function (Faker $faker) {
return [
'name' => $faker->name,
'address' => $faker->streetAddress
];
});
public 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);
}
Route::resource('demo', 'DemoController');
Route::delete('demo', 'DemoController@destroyAll')
->name('demo.destroyAll');
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
php artisan make:test CreateDemoTest
// 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([], 'App\Demo', custom.route');//you can have the first parameter empty if base model is set
$this->create([], null, 'custom.route');
$ vendor/bin/phpunit --filter CreateDemoTest
PHPUnit 7.5.6 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)Time: 1.05 seconds, Memory: 26.00 MBOK (3 tests, 5 assertions)
php 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 status
$ vendor/bin/phpunit --filter UpdateDemoTest
PHPUnit 7.5.6 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)Time: 905 ms, Memory: 26.00 MBOK (2 tests, 3 assertions)
php 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('App\Demo', custom.route');//you can have the first parameter empty if base model is set
$this->destroy(null, 'custom.route');
$ vendor/bin/phpunit --filter DeleteDemoTest
PHPUnit 7.5.6 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)Time: 1.02 seconds, Memory: 26.00 MBOK (3 tests, 4 assertions)

Conclusion

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade