Exemplo n.º 1
0
 public function testDivide()
 {
     $calc = new Calculator();
     $quotient = $calc->divide(10, 2);
     $this->assertEquals(5, $quotient);
     //check if 10/2 is equal to 5
 }
 /**
  * @test
  */
 public function shouldDivideTwoNumbers()
 {
     // given
     $calculator = new \Calculator();
     // when
     $result = $calculator->divide(10, 5);
     // then
     $this->assertEquals(2, $result);
 }
 /**
  * @test
  * @expectedException InvalidArgumentException
  */
 public function shouldThrowExceptionForDivisionByZero()
 {
     // given
     $calculator = new \Calculator();
     // when
     $calculator->divide(10, 0);
     // then
     $this->fail('exception should be thrown');
 }
 /**
  * @test
  */
 public function shouldDivideTwoNumbers()
 {
     // given
     $a = rand(0, 100);
     $b = rand(1, 100);
     $calculator = new \Calculator();
     // when
     $result = $calculator->divide($a, $b);
     // then
     $this->assertEquals(round($a / $b), $result);
 }
Exemplo n.º 5
0
<?php

//set defaults
$number1 = 0;
$number2 = 0;
$result = 0;
//check the post
if (isset($_POST['submit'])) {
    //get the values
    $number1 = (int) strip_tags(trim($_POST['number1']));
    $number2 = (int) strip_tags(trim($_POST['number2']));
    $result = (int) strip_tags(trim($_POST['result']));
    //get result from model (send the values)
    $calc = new Calculator($number1, $number2, $result);
}
if ($_POST['plus']) {
    $result = $calc->add($number1, $number2);
} else {
    if ($_POST['minus']) {
        $result = $calc->minus($number1, $number2);
    } else {
        if ($_POST['times']) {
            $result = $calc->mutiply($number1, $number2);
        } else {
            if ($_POST['divide']) {
                $result = $calc->divide($number1, $number2);
            }
        }
    }
}
Exemplo n.º 6
0
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage Cannot divide by zero!
  */
 public function testThrowsInvalidArgumentExceptionIfTryingToDivideByZero()
 {
     $calculator = new Calculator();
     $calculator->divide(5, 0);
 }
Exemplo n.º 7
0
    public function __construct($a, $b)
    {
        $this->_a = $a;
        $this->_b = $b;
    }
    public function add()
    {
        return $this->_a + $this->_b;
    }
    public function subtract()
    {
        return $this->_a - $this->_b;
    }
    public function multiply()
    {
        return $this->_a * $this->_b;
    }
    public function divide()
    {
        return $this->_a / $this->_b;
    }
}
$mycalc = new Calculator(12, 6);
echo $mycalc->add() . "<br>";
// Displays 18
echo $mycalc->multiply() . "<br>";
// Displays 72
echo $mycalc->subtract() . "<br>";
// Displays 6
echo $mycalc->divide() . "<br>";
// Displays 2
    {
        $this->_val1 = $val1;
        $this->_val2 = $val2;
    }
    public function add()
    {
        return $this->_val1 + $this->_val2;
    }
    public function subtract()
    {
        return $this->_val1 - $this->_val2;
    }
    public function multiply()
    {
        return $this->_val1 * $this->_val2;
    }
    public function divide()
    {
        return $this->_val1 / $this->_val2;
    }
}
$calc = new Calculator(3, 4);
echo "<p>3 + 4 = " . $calc->add() . "</p>";
echo "<p>3 - 4 = " . $calc->subtract() . "</p>";
echo "<p>3 * 4 = " . $calc->multiply() . "</p>";
echo "<p>3 / 4 = " . $calc->divide() . "</p>";
?>

  </body>
</html>