Akita Nakarmi
@nakarmi.akita
What is Test Driven Development?
TDD is a software development discipline where developers write test cases for features before writing any code.
BASIC FLOW OF TDD
Write a test, Watch it fail
Write enough code to pass the test
Improve on the code
PHP frameworks for Test
PREPARE LARAVEL TEST SUITE
Update your phpunit.xml file
//set enviroments for testing
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="API_DEBUG" value="false"/>
<env name="MAIL_DRIVER" value="log"/>
<ini name="memory_limit" value="512M" />
Prep base TestCase class
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, DatabaseMigrations, DatabaseTransactions;
protected $faker;
/**
* Set up the test
*/
public function setUp()
{
parent::setUp();
$this->faker = Faker::create();
}
/**
* Reset the migrations
*/
public function tearDown()
{
$this->artisan('migrate:reset');
parent::tearDown();
}
}
Write a Test case
class PostApiTest extends TestCase
{
public function test_it_can_create_a_post()
{
//Arrange
$data = [
'title' => $this->faker->sentence,
'content' => $this->faker->paragraph
];
//Act
$this->post(route('posts.store'), $data)
//Assert
->assertStatus(201)
->assertJson($data);
}
}
Run PHPUint
Error: Route not found
Test Result
Add route
//posts route
Route::post('posts', 'PostsApiController@store')->name('posts.store');
Run PHPUnit Again
Error: PostApiController does not Exist
Test Result
Create Controller
//create controller
php artisan make:controller PostsApiController
Add store method
class PostsApiController extends Controller
{
//post method to store post
public function store() {
dd('here!');
}
}
Run test
Create the post
//create post from the request data
public function store(Request $request) {
$data=$request->all();
$post=Post::create($data);
return $post;
}
Run Phpunit Again
Error: Opps! Post Class Not Found
Test Result
Create model Post
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable=
[
'title',
'content',
];
}
Run Test again and Check
Error: No Post Table Found
Test Result
Create a migration for table post
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
Run PHPUnit again to see what is going on
Test pass successfully!
Test Result
Refactor
Add Validation
<?php
namespace App\Articles\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PostAddRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required',
'content' => 'required'
];
}
}
Run PHPUnit to check
Validation Test
public function test_tile_is_required(){
$data = [
'content' => $this->faker->paragraph,
];
$this->post(route('posts.store'), $data)
->assertStatus(422);
}
public function test_content_is_required(){
$data = [
'title' => $this->faker->sentence,
];
$this->post(route('posts.store'), $data)
->assertStatus(422);
}
Conclusion
- We design and plan before we code
- It forces your code to be more modular
- We document our design by writing tests
- We write code that passes the tests as a proof that we implemented the code we designed
- It encourages the design of testable code and Testable code looks a lot like good code
- We know what's next to do
RESULT
You write better code in less time
CAVEATS
It takes a while to learn
AND
be Good at it.
Tips While using TDD
-
No logic in tests
-
Single responsibility
-
Start with an assert
Any Questions?
Thank You
Copy of PHP Meetup Ktm 2019 - TDD in Laravel
By nakarmi_akita
Copy of PHP Meetup Ktm 2019 - TDD in Laravel
- 223