CalculatorTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. use PHPUnit\Framework\TestCase;
  10. class CalculatorTest extends 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 number 10 + 'test'
  22. * Expected : An exception must be thrown
  23. */
  24. public function testAddNonRealNumber() {
  25. $this->calculator = new Calculator();
  26. $this->expectException(CalculatorException::class);
  27. $this->expectExceptionMessage("Adding not real numbers");
  28. $this->calculator->add(10, 'test');
  29. }
  30. /**
  31. * What : add to real numbers (10.1 + 42)
  32. * Expected : Returns 52.1
  33. */
  34. public function testAddRealNumber() {
  35. $result = $this->calculator->add(10.1, 42);
  36. $this->assertEquals(52.1, $result);
  37. }
  38. /**
  39. * What : add to real numbers but one of them is a string (10.1 + "42.2")
  40. * Expected : Returns 52.3
  41. */
  42. public function testAddRealNumberAsString() {
  43. $result = $this->calculator->add(10.1, "42.2");
  44. $this->assertEquals(52.3, $result);
  45. }
  46. /**
  47. * What : add to real numbers (10.1 + 42)
  48. * Expected : Returns 52.1 and probe value set as '1 1'
  49. */
  50. public function testAddProbeBothPositive() {
  51. $probe = '';
  52. $result = $this->calculator->add(10.1, 42, $probe);
  53. $this->assertEquals(52.1, $result);
  54. $this->assertEquals('1 1', $probe);
  55. }
  56. /**
  57. * What : add to real numbers (10.1 + 42)
  58. * Expected : Returns 52.1 and probe value set as '1 1' and result is saved in db
  59. */
  60. public function testAddSave() {
  61. $mock = $this->getMockBuilder(Database::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $mock->expects($this->any())
  65. ->method("save")
  66. ->willReturn(true);
  67. /**
  68. * @var Database $database
  69. */
  70. $database = $mock;
  71. $probe = '';
  72. $result = $this->calculator->add(10.1, 42, $probe, $database);
  73. $this->assertEquals(52.1, $result);
  74. $this->assertEquals('1 1', $probe);
  75. $this->assertEquals(true, $this->calculator->getLastDatabaseStatus());
  76. }
  77. }