BaseTestAbstract.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Complex;
  3. use PHPUnit\Framework\TestCase;
  4. abstract class BaseTestAbstract extends TestCase
  5. {
  6. // Saved php.ini precision, so that we can adjust the setting
  7. private static $precision;
  8. /**
  9. * @beforeClass
  10. */
  11. public static function setPrecision()
  12. {
  13. self::$precision = ini_set('precision', 16);
  14. }
  15. /**
  16. * @afterClass
  17. */
  18. public static function resetPrecision()
  19. {
  20. ini_set('precision', self::$precision);
  21. }
  22. // Overload the older setExpectedException() method from PHPUnit, converting to the newer
  23. // expectException() and expectExceptionMessage() methods if available
  24. // (for backward compatibility with PHPUnit when testing against PHP versions prior to PHP7)
  25. public function setExpectedException($exception, $message = '', $code = null)
  26. {
  27. if (!method_exists($this, 'expectException')) {
  28. return parent::setExpectedException($exception, $message);
  29. }
  30. $this->expectException($exception);
  31. if (!empty($message)) {
  32. $this->expectExceptionMessage($message);
  33. }
  34. }
  35. protected function getAssertionPrecision($value)
  36. {
  37. if ($value === 0.0) {
  38. return \pow(10, -10);
  39. } elseif ($value > 10) {
  40. return \pow(10, -8);
  41. }
  42. return 1.0e-11;
  43. }
  44. protected function complexNumberAssertions($expected, $result)
  45. {
  46. if (is_numeric($expected)) {
  47. $this->assertEqualsWithDelta(
  48. $expected,
  49. $result->getReal(),
  50. $this->getAssertionPrecision($expected),
  51. 'Numeric Assertion'
  52. );
  53. } else {
  54. $expected = new Complex($expected);
  55. $this->assertEqualsWithDelta(
  56. $expected->getReal(),
  57. $result->getReal(),
  58. $this->getAssertionPrecision($expected->getReal()),
  59. 'Real Component'
  60. );
  61. $this->assertEqualsWithDelta(
  62. $expected->getImaginary(),
  63. $result->getImaginary(),
  64. $this->getAssertionPrecision($expected->getImaginary()),
  65. 'Imaginary Component'
  66. );
  67. }
  68. }
  69. private $oneComplexValueDataSets = [
  70. [12, null, null],
  71. [12.345, null, null],
  72. [0.12345, null, null],
  73. [12.345, 6.789, null],
  74. [12.345, -6.789, null],
  75. [0.12345, 6.789, null],
  76. [0.12345, -6.789, null],
  77. [0.12345, 0.6789, null],
  78. [0.12345, -0.6789, null],
  79. [-9.8765, null, null],
  80. [-0.98765, null, null],
  81. [-9.8765, +4.321, null],
  82. [-9.8765, -4.321, null],
  83. [-0.98765, 0.4321, null],
  84. [-0.98765, -0.4321, null],
  85. [0, 1, null],
  86. [0, -1, null],
  87. [0, 0.123, null],
  88. [0, -0.123, null],
  89. [-1, null, null],
  90. ];
  91. protected function formatOneArgumentTestResultArray($expectedResults)
  92. {
  93. $testValues = array();
  94. foreach ($this->oneComplexValueDataSets as $test => $dataSet) {
  95. $name = 'value ' . (new Complex($dataSet))->format();
  96. $testValues[$name][] = $dataSet;
  97. $testValues[$name][] = $expectedResults[$test];
  98. }
  99. return $testValues;
  100. }
  101. abstract public function dataProvider();
  102. public function dataProviderInvoker()
  103. {
  104. return $this->dataProvider();
  105. }
  106. }