CalculatorTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yguern
  5. * Date: 29/09/2017
  6. * Time: 09:01
  7. */
  8. namespace Cloud\RDexp\Calculator;
  9. class CalculatorTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @var Calculator $calculator
  13. */
  14. private $calculator;
  15. public function setUp()
  16. {
  17. $this->calculator = new Calculator();
  18. }
  19. /**
  20. * What : try to add non real number 10 + 'test'
  21. * Expected : An exception must be thrown
  22. */
  23. public function testAddNonRealNumber() {
  24. $this->calculator = new Calculator();
  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 but one of them is a string (10.1 + "42")
  43. * Expected : Returns 52.1
  44. */
  45. public function testAddRealNumberAsString() {
  46. $result = $this->calculator->add(10.1, "42.2");
  47. $this->assertEquals(52.3, $result);
  48. }
  49. /**
  50. * What : add to real numbers (10.1 + 42)
  51. * Expected : Returns 52.1
  52. */
  53. public function testAddProbeBothPositive() {
  54. $probe = '';
  55. $result = $this->calculator->add(10.1, 42, $probe);
  56. $this->assertEquals(52.1, $result);
  57. $this->assertEquals('1 1', $probe);
  58. }
  59. }