CalculatorTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.2")
  43. * Expected : Returns 52.3
  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 and probe value set as '1 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. /**
  60. * What : add to real numbers (10.1 + 42)
  61. * Expected : Returns 52.1 and probe value set as '1 1' and result is saved in db
  62. */
  63. public function testAddSave() {
  64. $mock = $this->getMockBuilder(Database::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $mock->expects($this->any())
  68. ->method("save")
  69. ->willReturn(true);
  70. /**
  71. * @var Database $database
  72. */
  73. $database = $mock;
  74. $probe = '';
  75. $result = $this->calculator->add(10.1, 42, $probe, $database);
  76. $this->assertEquals(52.1, $result);
  77. $this->assertEquals('1 1', $probe);
  78. $this->assertEquals(true, $this->calculator->getLastDatabaseStatus());
  79. }
  80. }