![Page 1: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/1.jpg)
Testing in Laravelan introduction
![Page 2: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/2.jpg)
About me
René Kulik
PHP Developer at the ABOUT YOU Shop Unit
@kulik_io
http://www.kulik.io/
![Page 3: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/3.jpg)
What is software testing?
● Process used to verify the correctness, completeness, security and quality of a
software
● Positive and negative conditions
● Intent of finding errors
● Different approaches
![Page 4: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/4.jpg)
Why don’t developers test?
● They do not know how to write tests
● They do not feel responsible
● They say writing tests takes too much time
● They are too convinced of their code
● They were instructed not to write tests
![Page 5: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/5.jpg)
Why developers should write tests?
● Reduce bugs in new and old features
● Allow refactoring
● Good documentation
● Maintain software quality
![Page 6: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/6.jpg)
Laravel’s test setup
● PHPUnit
● Pre-configured phpunit.xml
● Helper methods
● tests-directory
● Feature- & Unit-directory to classify tests
![Page 7: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/7.jpg)
Configure testing environment
phpunit.xml
<?xml version=”1.0” encoding=”UTF-8”?>
<phpunit ... >
<testsuites> ... </testsuites>
<filter> ... </filter>
<php>
<env name=”APP_ENV” value=”testing”/>
<env name=”CACHE_DRIVER” value=”array”/>
<env name=”SESSION_DRIVER” value=”array”/>
<env name=”QUEUE_DRIVER” value=”sync”/>
</php>
</phpunit>
![Page 8: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/8.jpg)
Create tests
// Create feature test
~ $ php artisan make:test ExampleTest
// Create unit test
~ $ php artisan make:test ExampleTest --unit
![Page 9: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/9.jpg)
Example test
class ExampleTest extends TestCase
{
public function testBasicTest()
{
$response = $this->get(‘/’);
$response->assertStatus(200);
}
}
![Page 10: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/10.jpg)
Run tests
// All tests
~ $ phpunit
// Filter tests
~ $ phpunit --filter ExampleTest
// Specific group
~ $ phpunit --group ExampleGroup
![Page 11: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/11.jpg)
Database Testing
Resetting database
use DatabaseMigrations;
use DatabaseTransactions;
// New in 5.5
use RefreshDatabase;
![Page 12: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/12.jpg)
Database Testing
Seeding database
$factory->define(
App\Task::class,
function (Faker\Generator $faker) {
return [‘title’ => $faker->sentence(5)];
}
);
![Page 13: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/13.jpg)
Database Testing
Available assertions
$this->assertDatabaseHas($table, array $data);
$this->assertDatabaseMissing($table, array $data);
$this->assertSoftDeleted($table, array $data);
![Page 14: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/14.jpg)
Database Testing
Example test
use DatabaseMigations;
public function testDatabase()
{
factory(App\Task::class)->create([‘title’ => ‘Prepare talk’]);
$this->assertDatabaseHas(‘tasks’, [‘title’, ‘Prepare talk’]);
}
![Page 15: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/15.jpg)
Testing APIs
Call given URI with a JSON request
public function json(
$method,
$uri,
array $data = [],
array $headers = []
) { … }
![Page 16: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/16.jpg)
Testing APIs
Available assertions
$response->assertJson(array $data);
$response->assertJsonFragment(array $data);
$response->assertJsonMissing(array $data);
$response->assertExactJson(array $data);
$response->assertJsonStructure(array $structure);
![Page 17: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/17.jpg)
Testing APIs
Example test
public function testCreateTask()
{
$this->json(‘POST‘, ‘/tasks‘, [’title’ => ‘Prepare talk‘])
->assertStatus(201);
}
![Page 18: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/18.jpg)
Browser tests
● Laravel Dusk
● Uses standalone ChromeDriver installation
● Neither JDK nor Selenium required
● New in 5.5: headless by default
![Page 19: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/19.jpg)
Browser tests
Example test
public function testLoginInBrowser()
{
$user = factory(User::class)->create([‘email’ => ‘[email protected]’]);
$this->browse(function ($browser) use ($user) {
$browser->visit(‘/login’)
->type(‘email’, $user->email)
->type(‘password’, ‘password123’)
->press(‘Login’)
->assertPathIs(‘/youAreLoggedIn’);
});
}
![Page 20: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/20.jpg)
Exception Handling
New in 5.5
public function setUp()
{
parent::setUp();
$this->withoutExceptionHandling();
}
public function testExceptionHandling()
{
$this->get(‘/throwsException’)->assertStatus(200);
}
![Page 21: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/21.jpg)
Some more available assertions$response->assertSuccessful();
$response->assertRedirect($uri);
$response->assertHeader($headerName, $value = null);
$response->assertCookie($cookieName, $value = null);
$response->assertPlainCookie($cookieName, $value = null);
$response->assertSessionHas($key, $value = null);
$response->assertSessionHasErrors(array $keys);
$response->assertSessionMissing($key);
$response->assertSee($value);
$response->assertSeeText($value);
$response->assertDontSee($value);
$response->assertDontSeeText($value);
$response->assertViewHas($key, $value = null);
...
![Page 22: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/22.jpg)
Let’s code!
![Page 23: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/23.jpg)
Further links
● https://laravel.com/docs/5.4/testing
● https://leanpub.com/laravel-testing-decoded
● https://laracasts.com/series/phpunit-testing-in-laravel
● https://laracasts.com/series/lets-build-a-forum-with-laravel
● https://github.com/rkulik/testing-in-laravel
![Page 24: Testing in Laravel - kulik.io fileWhy don’t developers test? They do not know how to write tests They do not feel responsible They say writing tests takes too much time](https://reader033.vdocuments.mx/reader033/viewer/2022041423/5e20594477d5af0dc52b766c/html5/thumbnails/24.jpg)
Thank you!