Ricerca…
Semplice test in Symfony3
Test unitario
I test unitari vengono utilizzati per garantire che il codice non abbia errori di sintassi e per testare la logica del codice affinché funzioni come previsto. Esempio veloce:
src / AppBundle / Calculator / BillCalculator.php
<?php
namespace AppBundle\Calculator;
use AppBundle\Calculator\TaxCalculator;
class BillCalculator
{
private $taxCalculator;
public function __construct(TaxCalculator $taxCalculator)
{
$this->taxCalculator = $taxCalculator;
}
public function calculate($products)
{
$totalPrice = 0;
foreach ($products as $product) {
$totalPrice += $product['price'];
}
$tax = $this->taxCalculator->calculate($totalPrice);
return $totalPrice + $tax;
}
}
src / AppBundle / Calculator / TaxCalculator.php
<?php
namespace AppBundle\Calculator;
class TaxCalculator
{
public function calculate($price)
{
return $price * 0.1; // for example the tax is 10%
}
}
test / AppBundle / Calculator / BillCalculatorTest.php
<?php
namespace Tests\AppBundle\Calculator;
class BillCalculatorTest extends \PHPUnit_Framework_TestCase
{
public function testCalculate()
{
$products = [
[
'name' => 'A',
'price' => 100,
],
[
'name' => 'B',
'price' => 200,
],
];
$taxCalculator = $this->getMock(\AppBundle\Calculator\TaxCalculator::class);
// I expect my BillCalculator to call $taxCalculator->calculate once
// with 300 as the parameter
$taxCalculator->expects($this->once())->method('calculate')->with(300)->willReturn(30);
$billCalculator = new BillCalculator($taxCalculator);
$price = $billCalculator->calculate($products);
$this->assertEquals(330, $price);
}
}
Ho testato la mia classe BillCalculator per garantire che il mio BillCalculator restituisca il prezzo totale dei prodotti + 10% di tasse. Nel test unitario, creiamo il nostro caso di test. In questo test, fornisco 2 prodotti (i prezzi sono 100 e 200), quindi l'imposta sarà del 10% = 30. Mi aspetto che il TaxCalculator restituisca 30, in modo che il prezzo totale sia 300 + 30 = 330.
Test funzionale
I test funzionali vengono utilizzati per testare l'input e l'output. Con l'input fornito, mi aspettavo qualche output senza testare il processo per creare l'output. (questo è diverso con il test dell'unità perché nel test dell'unità testiamo il flusso del codice). Esempio veloce:
namespace Tests\AppBundle;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ApplicationAvailabilityFunctionalTest extends WebTestCase
{
/**
* @dataProvider urlProvider
*/
public function testPageIsSuccessful($url)
{
$client = self::createClient();
$client->request('GET', $url);
$this->assertTrue($client->getResponse()->isSuccessful());
}
public function urlProvider()
{
return array(
array('/'),
array('/posts'),
array('/post/fixture-post-1'),
array('/blog/category/fixture-category'),
array('/archives'),
// ...
);
}
}
Ho testato il mio controller in modo da garantire che il mio controller restituisca 200 risposte invece di 400 (non trovato) o 500 (errore interno del server) con l'url specificato.
Riferimenti:
- http://symfony.com/doc/current/best_practices/tests.html
- http://symfony.com/doc/current/book/testing.html