Facebook
From Sanjay Popli, 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: 201
  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.  
  21. class CalculatorTest extends TestCase
  22. {
  23.     // Positive test case of add function
  24.     public function testTwoPlusTwo(): void
  25.     {
  26.         $this->assertEquals(4, Calculator::add(2, 2));
  27.     }
  28.  
  29.     // Positive test case of divide function
  30.     public function testTwoDivideByTwo(): void
  31.     {
  32.         $this->assertEquals(1, Calculator::divide(2, 2));
  33.     }
  34.  
  35.     // Negative test case of add function
  36.     public function testTwoPlusOne(): void
  37.     {
  38.         $this->assertNotEquals(4, Calculator::add("a2a", "s1a"));
  39.     }
  40.  
  41.     // Negative test case of divide function
  42.     public function testOneDivideByZero(): void
  43.     {
  44.         $this->assertNotEquals(1, Calculator::divide(1, 0));
  45.  
  46.         // No need to expect exception (in case of 1/0) as Calculator class does not throw exception for this case and returns Infinity and issues warning.
  47.  
  48.         // Instead we can use below test case
  49.         $this->assertFalse(is_finite(Calculator::divide(1, 0)));
  50.     }
  51. }
  52.