Facebook
From Chirag Goti, 2 Years ago, written in PHP.
This paste is a reply to Basic Calculator from RealBlocks - view diff
Embed
Download Paste or View Raw
Hits: 179
  1. <?php
  2.  
  3. namespace Chirag\Phptest;
  4.  
  5. use PHPUnit\Framework\TestCase;
  6.  
  7. class Calculator
  8. {
  9.         public static function add($a, $b)
  10.         {
  11.                 return $a + $b;
  12.         }
  13.  
  14.         public static function divide($a, $b)
  15.         {
  16.                 return $a / $b;
  17.         }
  18. }
  19.  
  20.  
  21. class CalculatorTest extends TestCase
  22. {
  23.         // Positive test case for add function
  24.         public function testAdd(): void
  25.         {
  26.                 $res = Calculator::add(18, 20);
  27.  
  28.                 $this->assertEquals(38,$res);
  29.         }
  30.  
  31.     // Positive test case for divide function
  32.     public function testDivideByTwo(): void
  33.     {
  34.         $this->assertEquals(1, Calculator::divide(2, 2));
  35.     }
  36.  
  37.         // Negative test case for add function
  38.     public function testAddNonNumeric(): void
  39.     {
  40.         $this->assertNotEquals(4, Calculator::add("abcd", "pqr"));
  41.     }
  42.  
  43.     // Negative test case for divide function
  44.     public function testDivideeByZero(): void
  45.     {
  46.         $this->assertNotEquals(1, Calculator::divide(1, 0));
  47.  
  48.         $this->assertFalse(is_finite(Calculator::divide(1, 0)));
  49.     }
  50. }