/ Published in: PHP
                    
                                        
Uma classe e seu teste unitário
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
<?php
class financeira
{
private $value, $original, $info;
public function __construct($value = 100)
{
$this->value = $this->original = $value;
}
private function addLog($x)
{
$this->info['log'] .= "\n" . $x;
}
private function addOperacao($x)
{
$this->info['operacao'] .= $x;
$this->addLog($x);
}
public function getInfo($k = 'operacao')
{
return $this->info[$k];
}
public function setValue($x)
{
$this->original = $x;
$this->resetValue();
return $this;
}
public function getValue()
{
$this->addLog('=' . $this->value);
return (float) $this->value;
}
public function resetValue()
{
$this->value = $this->original;
$this->info['operacao'] = '';
$this->info['log'] = '';
return $this;
}
private function percent($x, $v = NULL)
{
{
$v = $this->value;
}
$r = ($v * $x) /100;
$this->addOperacao('-' . $x . '%');
return $r;
}
public function addDesconto($x)
{
$this->value -= $this->percent($x);
}
/*
* Calcula descontos progressivos como 10% + 20% + 5% a partir de um array
*/
{
foreach($descontos as $desconto)
{
$this->addDesconto($desconto);
}
return $this;
}
public function getDesconto()
{
return $this->original - $this->value;
}
public function getAlterador()
{
return ($this->getDesconto() * 100) / $this->original;
}
/*
* Calcula o numero multiplicador de desconto progressivo como 10% + 20% + 5% a partir de um array
*/
{
$this->resetValue();
foreach($descontos as $desconto)
{
$this->addDesconto($desconto);
}
return $this->getAlterador();
}
}
// Teste unitário, salvo no arquivo financeiraTest.php
/**
*
* @author Gilmar Pupo <[email protected]>
*
* Testa as funcoes da classe financeira
*/
$t = new lime_test(25);
$v = new financeira(100);
$v->addDesconto(10);
$t->is( $v->getValue(),90 ,'adicionando 10% de Porcentagem:' . $v->getValue());
$v->addDesconto(10);
$t->is( $v->getValue(),81 ,'adicionando 10% de Porcentagem:' . $v->getValue());
$v->resetValue();
$t->is( $v->getValue(),100 ,'reiniciando valores:' . $v->getValue());
$t->is( $v->getValue(),81 ,'adicionando 10% + 10% de Porcentagem:' . $v->getValue());
$v->resetValue();
$t->is( $v->getValue(),59.85 ,'adicionando 30% + 10% + 5% de desconto em 100:' . $v->getValue());
$t->diag('Testando métodos compactados');
$t->is( $v->getValue(),47.88 ,'adicionando 30% + 10% + 5% de desconto em 80:' . $v->getValue());
$t->is( $v->setValue(50)
->getValue()
,29.925 ,'adicionando 30% + 10% + 5% de desconto em 50:' . $v->getValue());
$t->diag('Obtendo dados informativos');
$t->is( $v->setValue(100)
->getDesconto()
,40.15 ,'acumulado de desconto foi de:' . $v->getDesconto());
$t->is( $v->getAlterador(), 40.15, 'alterador:' . $v->getAlterador());
$t->is( $v->setValue(80)
->getDesconto()
,32.12 ,'acumulado de desconto foi de:' . $v->getDesconto());
$t->is( $v->getAlterador(), 40.15, 'alterador:' . $v->getAlterador());
$t->is( $v->setValue(50)
->getDesconto()
,20.075 ,'acumulado de desconto foi de:' . $v->getDesconto());
$t->is( $v->getAlterador(), 40.15, 'alterador:' . $v->getAlterador());
$t->diag('Definindo multiplicador a partir de um array');
$y = new financeira();
$t->is( $i = $y->getMultiplicadorDaProgressao(array(30, 10, 10)) , 43.3, 'getMultiplicadorDaProgressao:' . $i);
$t->isa_ok( $y->getInfo(), 'string', 'operacao:' . $y->getInfo());
//echo $y->getInfo('log') . "\n";
$t->is( $i = $y->getMultiplicadorDaProgressao(array(20, 10, 10)) , 35.2, 'getMultiplicadorDaProgressao:' . $i);
$t->isa_ok( $y->getInfo(), 'string', 'operacao:' . $y->getInfo());
//echo $y->getInfo('log') . "\n";
$t->is( $i = $y->getMultiplicadorDaProgressao(array(30, 10, 5)) , 40.15, 'getMultiplicadorDaProgressao:' . $i);
$t->isa_ok( $y->getInfo(), 'string', 'operacao:' . $y->getInfo());
//echo $y->getInfo('log') . "\n";
$t->is($r = ((100 * $y->getMultiplicadorDaProgressao(array(30, 10, 5))) /100) , 40.15, $y->getInfo() . ':' . $r);
$t->is($r = ((80 * $y->getMultiplicadorDaProgressao(array(30, 10, 5))) /100) , 32.12, $y->getInfo() . ':' . $r);
$t->is($r = ((50 * $y->getMultiplicadorDaProgressao(array(30, 10, 5))) /100) , 20.075, $y->getInfo() . ':' . $r);
$t->is($r = ((100 * $y->getMultiplicadorDaProgressao(array(30, 7, 5))) /100) , 38.155, $y->getInfo() . ':' . $r);
$t->is($r = ((80 * $y->getMultiplicadorDaProgressao(array(30, 8, 5))) /100) , 31.056, $y->getInfo() . ':' . $r);
$t->is($r = ((50 * $y->getMultiplicadorDaProgressao(array(20, 9, 20))) /100) , 20.88, $y->getInfo() . ':' . $r);
Comments
 Subscribe to comments
                    Subscribe to comments
                
                