CalculatorTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Noa
  5. * Date: 24/09/2017
  6. * Time: 15:14
  7. */
  8. namespace Cloud\RD\Calculator;
  9. use PHPUnit_Framework_TestCase;
  10. class CalculatorTest extends PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * @var Calculator $calculator
  14. */
  15. private $calculator;
  16. public function setUp()
  17. {
  18. $this->calculator = new Calculator();
  19. }
  20. /**
  21. * What : try to add non real numbers (10 + 'test')
  22. * Expected : An exception must be thrown
  23. */
  24. public function testAddNonRealNumber() {
  25. try {
  26. $this->calculator->add(10, 'test');
  27. $this->assertFalse(true, "An exception must be thrown");
  28. } catch (\Exception $e) {
  29. $this->assertInstanceOf(CalculatorException::class, $e);
  30. $this->assertEquals("Adding not real numbers", $e->getMessage());
  31. }
  32. }
  33. /**
  34. * What : add to real numbers (10.1 + 42)
  35. * Expected : Returns 52.1
  36. */
  37. public function testAddRealNumber() {
  38. $result = $this->calculator->add(10.1, 42);
  39. $this->assertEquals(52.1, $result);
  40. }
  41. /**
  42. * What : add to real numbers (10.1 + 42)
  43. * Expected : Returns 52.1
  44. */
  45. public function testAddProbeBothPositive() {
  46. $probe = '';
  47. $result = $this->calculator->add(10.1, 42, $probe);
  48. $this->assertEquals(52.1, $result);
  49. $this->assertEquals('1 1', $probe);
  50. }
  51. /**
  52. * What : add to real numbers (10.1 + 42)
  53. * Expected : Returns 42
  54. */
  55. public function testAddANullBPositive() {
  56. $probe = '';
  57. $result = $this->calculator->add(0, 42, $probe);
  58. $this->assertEquals(42, $result);
  59. $this->assertEquals('0 1', $probe);
  60. }
  61. }