Facebook
From Praveen Kumar, 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: 218
  1. <?php
  2.  
  3. class Calculator
  4. {
  5.  public static function add($a, $b)
  6.  {
  7.   return $a + $b;
  8.  }
  9.  
  10.  public static function divide($a, $b)
  11.  {
  12.   return $a / $b;
  13.  }
  14. }
  15.  
  16. use PHPUnit\Framework\TestCase;
  17.  
  18. class CalculatorTest extends TestCase
  19. {
  20.     public function testAddition(): void
  21.     {
  22.         // Positive Test Case 1: add() method
  23.         $a = 5;
  24.         $b = 10;
  25.         $expectedSum = 15;
  26.         $sum = Calculator::add($a, $b);
  27.         $this->assertEquals($expectedSum, $sum);
  28.     }
  29.  
  30.     public function testDivision(): void
  31.     {
  32.         // Positive Test Case 2: divide() method
  33.         $a = 20;
  34.         $b = 4;
  35.         $expectedQuotient = 5;
  36.         $quotient = Calculator::divide($a, $b);
  37.         $this->assertEquals($expectedQuotient, $quotient);
  38.     }
  39.  
  40.     public function testAdditionWithInvalidInput(): void
  41.     {
  42.         // Negative Test Case 1: add() method with invalid input
  43.         $a = "abc";
  44.         $b = 10;
  45.         $this->expectException(TypeError::class);
  46.         Calculator::add($a, $b);
  47.     }
  48.  
  49.     public function testDivisionByZero(): void
  50.     {
  51.         // Negative Test Case 2: divide() method with division by zero
  52.         $a = 10;
  53.         $b = 0;
  54.         $this->expectException(DivisionByZeroError::class);
  55.         Calculator::divide($a, $b);
  56.     }
  57. }
  58.