Código en el blog.
PHPUnit en Nelio Software
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function neliovat_get_vat( $quantity ) { | |
return $quantity * 0.21; | |
}//end neliovat_get_vat() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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