Skip to content

Instantly share code, notes, and snippets.

@davilera
Last active December 21, 2016 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davilera/efbf3459e1c316f19bd285f9c8b3fc7d to your computer and use it in GitHub Desktop.
Save davilera/efbf3459e1c316f19bd285f9c8b3fc7d to your computer and use it in GitHub Desktop.
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