Example #1
0
 public function testifobjectequals()
 {
     $calculator_obj = new Calculator();
     $calculator_obj->setOperand(array(4, 2));
     $calculator_obj->setOperation(new Addition());
     $this->assertEquals(6, (int) $calculator_obj->process());
 }
Example #2
0
 public function testCalculatorCanChainCalculations()
 {
     $this->assertEquals(15, Calculator::add(Calculator::add(5, 5), Calculator::subtract(10, 5)));
 }
Example #3
0
#!/usr/bin/php
<?php 
chdir(dirname(__DIR__));
require './vendor/autoload.php';
$cli = php_sapi_name() === 'cli' or defined('STDIN');
$cr = $cli ? "\n" : '<br>';
use Calculator\Calculator;
$calculator = new Calculator();
// 1 + 5 * 3.5 / 2 = 9.75
$result = $calculator->number(1)->operator('+')->number(5)->operator('*')->number(3.5)->operator('/')->number(2)->execute();
echo 'Result: ' . $result . $cr;
echo 'Full operation: ' . $calculator . $cr;
$result = $calculator->number(2)->operator('*')->number(2)->operator('^')->number(4)->execute();
echo 'Result: ' . $result . $cr;
echo 'Full operation: ' . $calculator . $cr;
$result = $calculator->number(2)->operator('*')->number(2)->negative()->operator('^')->number(4)->execute();
echo 'Result: ' . $result . $cr;
echo 'Full operation: ' . $calculator . $cr;
$result = $calculator->number(9)->operator('√')->execute();
echo 'Result: ' . $result . $cr;
echo 'Full operation: ' . $calculator . $cr;
$result = $calculator->number(1)->operator('cos')->execute();
echo 'Result: ' . $result . $cr;
echo 'Full operation: ' . $calculator . $cr;
Example #4
0
 public function testCalculatorRejectsStringInArgumentsWhenSubtractingTwoNumbers()
 {
     $this->expectException(\InvalidArgumentException::class);
     Calculator::subtract(1, 2, 'o');
 }
Example #5
0
<?php

include 'bootstrap.php';
use Calculator\Calculator;
use Calculator\Operation\Addition;
$calculator_obj = new Calculator();
$calculator_obj->setOperand(array(4, 2));
$calculator_obj->setOperation(new Addition());
echo $calculator_obj->process();
Example #6
0
<?php

require_once 'vendor/autoload.php';
class BiggerOperator extends \Calculator\Operators\BaseOperator
{
    protected $symbol = '>';
    protected $priority = 2;
    public function process($index, \SplDoublyLinkedList $list)
    {
        $first = $list->offsetGet($index - 1);
        $second = $list->offsetGet($index + 1);
        $result = (bool) ($first > $second);
        $list->offsetSet($index - 1, $result);
        unset($list[$index]);
        unset($list[$index]);
    }
}
$formula = '( ( 10 * x + y ) / z AND ( 20 OR 0 ) XOR ( x XNOR y )  ) OR 1 + 2';
$calculator = new \Calculator\Calculator($formula);
//$calculator->setFormula($formula);
\Calculator\Calculator::extend(BiggerOperator::class);
var_dump($calculator->result(['x' => 10, 'y' => 20, 'z' => 2]));
Example #7
0
 public function testDivideByZeroNotPossible()
 {
     $this->expectException(DivideByZeroException::class);
     Calculator::divide(10, 0);
 }
Example #8
0
 /**
  * @expectedException InvalidArgumentException
  */
 public function test_calculate_divide_by_zero()
 {
     $calc = new Calculator(10);
     $calc->calculate(0, new Division());
 }