Create a gist now

Instantly share code, notes, and snippets.

PHPUnit en Nelio Software
<?php
function neliovat_get_vat( $quantity ) {
return $quantity * 0.21;
}//end neliovat_get_vat()
<?php
class VatFunctionTest extends PHPUnit_Framework_TestCase {
public function testVatOfIntegerIsCorrect() {
$result = neliovat_get_vat( 1000 );
$this->assertEquals( 210, $result );
}//end testVatOfIntegerIsCorrect()
public function testVatOfStringIsCorrect() {
$result = neliovat_get_vat( '1.000 €' );
$this->assertEquals( 210, $result );
}//end testVatOfStringIsCorrect()
}//end class
<?php
function neliovat_get_vat( $quantity ) {
if ( is_string( $quantity ) ) {
// Remove thousands separator.
$quantity = str_replace( '.', '', $quantity );
// Remove decimals separator.
$quantity = str_replace( ',', '.', $quantity );
// Remove currency.
$quantity = preg_replace( '/[^0-9,]/', '', $quantity );
}//end if
return $quantity * 0.21;
}//end neliovat_get_vat()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment