public function testAdd() { $calc = new Calculator(); $sum = $calc->add(array(2, 3, 4, 5)); $this->assertEquals(14, $sum); //check if 2+3+4+5 is equal to 14 }
public function testAdd() { $calc = new Calculator(); $result = $calc->add(30, 12); // ¡acierta que nuestra calculadora suma dos números correctamente! $this->assertEquals(42, $result); }
public function testAddWithMultipleArgsAndLineBreaks() { $calc = new Calculator(); $expected = 6; $value = $calc->add("1\n2,3"); $this->assertEqual($expected, $value); }
public function testAdd() { require 'Calculator.php'; $Controller = new Calculator(); $expected = 5; $this->assertEquals($expected, $Controller->add(3, 2)); }
public function testAdd() { $calc = new Calculator(); $result = $calc->add(30, 12); // assert that your calculator added the numbers correctly! $this->assertEquals(42, $result); }
/** * @test */ public function shouldAddTwoNumbers() { // given $calculator = new \Calculator(); // when $result = $calculator->add(2, 3); // then $this->assertEquals(5, $result); }
/** * @test */ public function shouldAddTwoNumbers() { // given $a = rand(0, 100); $b = rand(0, 100); $calculator = new \Calculator(); // when $result = $calculator->add($a, $b); // then $this->assertEquals($a + $b, $result); }
function testAddWithMultipleArgs() { assert($this->calc->add(1, 2) === 3); }
/** * Generated from @assert (1,3) == 4. * * @covers Calculator::add */ public function testAdd() { $this->assertEquals(4, $this->object->add(1, 3)); }
<?php require_once 'object.php'; $calculator = new Calculator; $calc2 = new Calculator; //new instance of the calculator with its own values /**good practice is to capitalize objects, you don't need to use parenthesis if * you are not passing arguments */ //if (assert($calculator->add(1,1) == 2)) echo "pass add"; function test ($assertion, $message = null) { echo (@assert($assertion) ? 'pass: '******'fail: ') . $message . "\n"; } test($calculator->add(1,1) == 2, '1 + 1 == 2'); test($calculator->register == 2, '$calculator remembers 2!'); test($calc2->register == 0, '$calc2 still set to 0');
/** * @dataProvider addProvider * @param $ex * @param $a * @param $b */ public function testAdd($ex, $a, $b) { $calc = new Calculator(); $this->assertEquals($ex, $calc->add($a, $b)); $this->assertTrue(true); }
public function div($value) { return $this->answer /= (double) $value; } public function clear() { $this->answer = 0; } public function eq() { return $this->answer; } } // Spl auto loader require 'SplClassLoader.php'; $loader = new SplClassLoader(null, implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', 'src'))); $loader->register(); // Calculator $calc = new Calculator(); $calc->add(15); // answer = 15 $calc->mul(3); // answer = 45 $calc->div(9); // answer = 5 echo $calc->eq(); // prints 5 echo "\n"; // Implement calculator chain $chain = new \Chainnn\Chain($calc, array('add', 'sub', 'mul', 'div', '^clear', '$eq')); echo $chain->clear()->add(100)->div(5)->sub(10)->mul(3)->eq();
/** * @test * @expectedException Exception * @expectedExceptionMessage negatives not allowed -2,-5 */ public function negativeNumbersThroesExceptionWithDump() { $this->calculator->add("//;\n1;-2;-5"); }
</head> <body> <form method="post"> <input type="number" name="number1"> <input type="text" name="operator"> <input type="number" name="number2"> <input type="submit" name="calculator" value="Calculate"> </form> <?php $calculator1 = new Calculator(); if (isset($_POST['calculator'])) { $number1 = $_POST['number1']; $number2 = $_POST['number2']; $operator = $_POST["operator"]; if ($operator == "+") { $calculator1->add($number1, $number2); echo $calculator1->getResult(); } elseif ($operator == "-") { $calculator1->sub($number1, $number2); echo $calculator1->getResult(); } elseif ($operator == "/") { $calculator1->div($number1, $number2); echo $calculator1->getResult(); } elseif ($operator == "*") { $calculator1->mul($number1, $number2); echo $calculator1->getResult(); } else { echo "Invalid operator"; } } ?>
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
/** * @param int $numOfYears * * @return string */ public function showAge($numOfYears) { $msg = sprintf("%s will be %d years old in %d years.\n", $this->name, $this->calculator->add($this->age, $numOfYears), $numOfYears); return $msg; }
<?php require_once 'calc.php'; describe('calculator', function () { $calc; beforeEach(function () use(&$calc) { $calc = new Calculator(); }); describe('adds', function () use(&$calc) { it('positive numbers', function () use(&$calc) { expect($calc->add(1, 2))->toBe(3); }); it('negative numbers', function () use(&$calc) { expect($calc->add(-4, -5))->toBe(-9); }); it('with zero', function () use(&$calc) { expect($calc->add(0, 10))->toBe(10); }); }); describe('divides', function () use(&$calc) { it('positive numbers', function () use(&$calc) { expect($calc->div(8, 2))->toBe(4); }); it('throws on division by zero', function () use(&$calc) { expect(function () use(&$calc) { $calc->div(1, 0); })->toThrow(); }); }); });
/** * Generated from @assert (1, 2) == 4. * * @covers Calculator::add */ public function testAdd5() { $this->assertEquals(3, $this->object->add(1, 2)); }
public function testAdd() { $calc = new Calculator(); $this->assertEquals(9, $calc->add(5, 4)); }
/** * Generated from @assert (1, 1) == 2. * * @covers Calculator::add */ public function testAdd4() { $this->assertEquals(2, $this->object->add(1, 1)); }
<?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); } } } }
/** * For testing calling the same mocked method twice. * * @param \Box\TestScribe\_fixture\_input\Calculator $calculator * * @return int */ public function AddTwice(Calculator $calculator) { $rc1 = $calculator->add(1, 2); $rc = $rc1 + $calculator->add(0, 1); return $rc; }
case E_USER_ERROR: echo "<b>My ERROR</b> [{$errno}] {$errstr}<br />\n"; echo " Fatal error on line {$errline} in file {$errfile}"; echo ', PHP ' . PHP_VERSION . ' (' . PHP_OS . ")<br />\n"; echo "Aborting...<br />\n"; exit(1); break; case E_USER_WARNING: echo "<b>My WARNING</b> [{$errno}] {$errstr}<br />\n"; break; case E_USER_NOTICE: echo "<b>My NOTICE</b> [{$errno}] {$errstr}<br />\n"; break; default: echo "Unknown error type: [{$errno}] {$errstr}<br />\n"; break; } /* Don't execute PHP internal error handler */ return true; } set_error_handler('myErrorHandler'); class Calculator { public function add(int $left, int $right) : int { return $left + $right; } } $calc = new Calculator(); $result = $calc->add(1, 3); $calc->add('not an int', 'whoops');
<?php require_once 'Calculator.php'; $Calc = new Calculator(); $Calc->add(5); $Calc->multiply(7); echo $Calc->getTotal();
$this->result = $this->fristnumb * $this->nextnumb; echo $this->result; } function div() { $this->result = $this->fristnumb / $this->nextnumb; echo $this->result; } } $num1 = $_POST['num1']; $num2 = $_POST['num2']; $char = $_POST['char']; switch ($char) { case '+': $add = new Calculator($num1, $num2); $add->add(); break; case '-': $sub = new Calculator($num1, $num2); $sub->sub(); break; case '*': $mul = new Calculator($num1, $num2); $mul->mul(); break; case '/': $div = new Calculator($num1, $num2); $div->div(); break; default: echo "输入有误";
} //test for the existance of a Calculator class test (class_exists('Calculator'), "A calculator class exists"); //create a new instance of the calculator $calculator = new Calculator; //test for an instance of a calculator test (($calculator != null), "There is an instance of the calculator"); //test if the new calcualtor is set to zero test ($calculator->register == 0, "The calculator is set to 0"); /** * test that there is a new function * * if (function_exists($calculator->add)) * * { * * echo "There is an add function"; * * } */ //test for an add function test ($calculator->add(1,1) == 2, "1 + 1 = 2"); //test for adding a single number
<?php require_once "calculator.php"; $calculator = new Calculator(); $calculator->add(1, 2); $calculator->showResult();
{ $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>