Facebook
From Abhishek Shrivastava, 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: 206
  1. <?php
  2.  
  3. namespace Tests\Unit;
  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. class CalculatorTest extends TestCase
  21. {
  22.     // Positive test case for add function
  23.     public function testTwoPlusTwo(): void
  24.     {
  25.         $this->assertEquals(4, Calculator::add(2, 2));
  26.     }
  27.  
  28.     // Positive test case for divide function
  29.     public function testTwoDivideByTwo(): void
  30.     {
  31.         $this->assertEquals(1, Calculator::divide(2, 2));
  32.     }
  33.  
  34.     // Negative test case for add function
  35.     public function testTwoPlusOne(): void
  36.     {
  37.         $this->assertNotEquals(4, Calculator::add("a2a", "s1a"));
  38.     }
  39.  
  40.     // Negative test case for divide function
  41.     public function testOneDivideByZero(): void
  42.     {
  43.         $this->assertNotEquals(1, Calculator::divide(1, 0));
  44.  
  45.         // We cannot expect an exception for the case of 1/0 as the Calculator class does not throw an exception for this case, but instead returns infinity and issues a warning.
  46.         // Therefore, we can use the below test case to check if the result of dividing by zero is finite or not.
  47.         $this->assertFalse(is_finite(Calculator::divide(1, 0)));
  48.     }
  49. }
  50.