Zoeken…


Eenvoudig testen in Symfony3

Hoofdstuk toets

Eenheidstests worden gebruikt om ervoor te zorgen dat uw code geen syntaxisfout bevat en om de logica van uw code te testen om te werken zoals u had verwacht. Snel voorbeeld:

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%
    }
}

proeven / 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);
    }
}

Ik heb mijn BillCalculator-klasse getest, zodat ik ervoor kan zorgen dat mijn BillCalculator de totale prijs van het product + 10% btw retourneert. In unit-test maken we onze eigen testcase. In deze test lever ik 2 producten op (de prijzen zijn 100 en 200), dus de belasting is 10% = 30. Ik verwacht dat de TaxCalculator 30 teruggeeft, zodat de totale prijs 300 + 30 = 330 is.

Functionele test

Functionele tests worden gebruikt om de invoer en uitvoer te testen. Met de gegeven input verwachtte ik wat output zonder het proces te testen om de output te creëren. (dit is anders bij de unit-test omdat we in de unit-test de codestroom testen). Snel voorbeeld:

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'),
            // ...
        );
    }
}

Ik heb mijn controller getest, zodat ik ervoor kan zorgen dat mijn controller 200 respons in plaats van 400 (niet gevonden) of 500 (interne serverfout) retourneert met de opgegeven URL.

Referenties:



Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow