Devo testare delle API con Unit Test di una classe Category. Come primo passaggio genero la classe di Test:
php artisan make:test CategoryTest
Questo comando mi genera un file sotto tests/Feature/CategoryTest.php che tosto personalizzo come segue, un metodo per ogni operazione del CRUD:
<?php
namespace Tests\Feature;
use App\Category;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class CategoryTest extends TestCase
{
public function testsCategoriesAreCreatedCorrectly()
{
$headers = ['Authorization' => "ajs8ebddau"];
$payload = [
'name' => 'Libri e Riviste INS',
'short' => 'libri INS',
];
$this->json('POST', '/api/categories', $payload, $headers)
->assertStatus(302)
->assertJson([
'name' => 'Libri e Riviste INS',
'short' => 'libri INS'
]);
}
public function testsCategoriesAreUpdatedCorrectly()
{
$headers = ['Authorization' => "ajs8ebddau"];
$category = new Category;
$category->name = 'Libri e Riviste TEST';
$category->short = 'libri TEST';
$category->save();
$id = $category->id;
$payload = [
'name' => 'Libri e Riviste UPDATE',
'short' => 'libri UPDATE',
];
$response = $this->json('PUT', '/api/categories/' . $id, $payload, $headers)
->assertStatus(200)
->assertJson([
'id' => $id,
'name' => 'Libri e Riviste UPDATE',
'short' => 'libri UPDATE'
]);
}
public function testsCategoriesAreDeletedCorrectly()
{
$headers = ['Authorization' => "ajs8ebddau"];
$id = 30;
$payload = [
'name' => 'Lorem',
'short' => 'Ipsum',
];
$category = new Category;
$category->name = 'Libri e Riviste DEL';
$category->short = 'libri DEL';
$category->save();
$id = $category->id;
$response = $this->json('DELETE', '/api/categories/' . $id, $payload, $headers)
->assertStatus(200);
}
public function testCategoriesAreListedCorrectly()
{
$category = new Category;
$category->name = 'Libri e Riviste first';
$category->short = 'libri first';
$category->save();
$category = new Category;
$category->name = 'Libri e Riviste second';
$category->short = 'libri second';
$category->save();
$headers = ['Authorization' => "ajs8ebddau"];
$response = $this->json('GET', '/api/categories', [], $headers)
->assertStatus(200)
->assertJson([
[ 'name' => 'Libri e Riviste first', 'short' => 'libri first' ],
[ 'name' => 'Libri e Riviste second', 'short' => 'libri second' ]
])
->assertJsonStructure([
'*' => ['id', 'name', 'short', 'created_at', 'updated_at'],
]);
}
}
In realtà la variabile $headers qui è superflua, ma mi viene comoda dopo, quando aggiungerò lo strato di autenticazione.
Eseguo i test
marcob@jsbach:shopping3$ composer test > vendor/bin/phpunit PHPUnit 7.4.0 by Sebastian Bergmann and contributors. ..... 5 / 5 (100%) Time: 248 ms, Memory: 16.00MB OK (5 tests, 7 assertions) marcob@jsbach:shopping3$
Il comando di test qui è dato utilizzando composer, in quanto ho definito uno script “test” all’interno del file di configurazione composer.json: alla sezione script ho aggiunto:
"scripts": {
...
"test" : [
"vendor/bin/phpunit"
]
},
L’alternativa è scrivere semplicemente vendor/bin/phpunit
Commenti recenti