FromStringTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\CarbonInterval;
  12. use Carbon\CarbonInterval;
  13. use Generator;
  14. use InvalidArgumentException;
  15. use Tests\AbstractTestCase;
  16. class FromStringTest extends AbstractTestCase
  17. {
  18. /**
  19. * @dataProvider \Tests\CarbonInterval\FromStringTest::dataForValidStrings
  20. *
  21. * @param string $string
  22. * @param CarbonInterval $expected
  23. */
  24. public function testReturnsInterval($string, $expected)
  25. {
  26. $result = CarbonInterval::fromString($string);
  27. $this->assertEquals($expected, $result, "'$string' does not return expected interval.");
  28. }
  29. public static function dataForValidStrings(): Generator
  30. {
  31. // zero interval
  32. yield ['', new CarbonInterval(0)];
  33. // single values
  34. yield ['1y', new CarbonInterval(1)];
  35. yield ['1mo', new CarbonInterval(0, 1)];
  36. yield ['1w', new CarbonInterval(0, 0, 1)];
  37. yield ['1d', new CarbonInterval(0, 0, 0, 1)];
  38. yield ['1h', new CarbonInterval(0, 0, 0, 0, 1)];
  39. yield ['1m', new CarbonInterval(0, 0, 0, 0, 0, 1)];
  40. yield ['1s', new CarbonInterval(0, 0, 0, 0, 0, 0, 1)];
  41. yield ['1ms', new CarbonInterval(0, 0, 0, 0, 0, 0, 0, 1000)];
  42. yield ['1µs', new CarbonInterval(0, 0, 0, 0, 0, 0, 0, 1)];
  43. // single values with space
  44. yield ['1 y', new CarbonInterval(1)];
  45. yield ['1 mo', new CarbonInterval(0, 1)];
  46. yield ['1 w', new CarbonInterval(0, 0, 1)];
  47. // fractions with integer result
  48. yield ['0.571428571429w', new CarbonInterval(0, 0, 0, 4)];
  49. yield ['0.5d', new CarbonInterval(0, 0, 0, 0, 12)];
  50. yield ['0.5h', new CarbonInterval(0, 0, 0, 0, 0, 30)];
  51. yield ['0.5m', new CarbonInterval(0, 0, 0, 0, 0, 0, 30)];
  52. // fractions with float result
  53. yield ['1.5w', new CarbonInterval(0, 0, 1, 3, 12)];
  54. yield ['2.34d', new CarbonInterval(0, 0, 0, 2, 8, 9, 36)];
  55. yield ['3.12h', new CarbonInterval(0, 0, 0, 0, 3, 7, 12)];
  56. yield ['3.129h', new CarbonInterval(0, 0, 0, 0, 3, 7, 44, 400000)];
  57. yield ['4.24m', new CarbonInterval(0, 0, 0, 0, 0, 4, 14, 400000)];
  58. yield ['3.56s', new CarbonInterval(0, 0, 0, 0, 0, 0, 3, 560000)];
  59. yield ['3.56ms', new CarbonInterval(0, 0, 0, 0, 0, 0, 0, 3560)];
  60. // combinations
  61. yield ['2w 3d', new CarbonInterval(0, 0, 0, 17)];
  62. yield ['1y 2mo 1.5w 3d', new CarbonInterval(1, 2, 1, 6, 12)];
  63. // multi same values
  64. yield ['1y 2y', new CarbonInterval(3)];
  65. yield ['1mo 20mo', new CarbonInterval(0, 21)];
  66. yield ['1w 2w 3w', new CarbonInterval(0, 0, 6)];
  67. yield ['10d 20d 30d', new CarbonInterval(0, 0, 0, 60)];
  68. yield ['5h 15h 25h', new CarbonInterval(0, 0, 0, 0, 45)];
  69. yield ['3m 3m 3m 1m', new CarbonInterval(0, 0, 0, 0, 0, 10)];
  70. yield ['55s 45s 1s 2s 3s 4s', new CarbonInterval(0, 0, 0, 0, 0, 0, 110)];
  71. yield ['1500ms 1623555µs', new CarbonInterval(0, 0, 0, 0, 0, 0, 0, 3123555)];
  72. yield ['430 milli', new CarbonInterval(0, 0, 0, 0, 0, 0, 0, 430000)];
  73. // multi same values with space
  74. yield ['1 y 2 y', new CarbonInterval(3)];
  75. yield ['1 mo 20 mo', new CarbonInterval(0, 21)];
  76. yield ['1 w 2 w 3 w', new CarbonInterval(0, 0, 6)];
  77. // no-space values
  78. yield ['2w3d', new CarbonInterval(0, 0, 0, 17)];
  79. yield ['1y2mo3w4d5h6m7s', new CarbonInterval(1, 2, 3, 4, 5, 6, 7)];
  80. // written-out units
  81. yield ['1year 2month 3week 4day 5hour 6minute 7second', new CarbonInterval(1, 2, 3, 4, 5, 6, 7)];
  82. yield ['1 year 2 month 3 week', new CarbonInterval(1, 2, 3)];
  83. yield ['2 Years 3 Months 4 Weeks', new CarbonInterval(2, 3, 4)];
  84. yield ['5 Days 6 Hours 7 Minutes 8 Seconds', new CarbonInterval(0, 0, 0, 5, 6, 7, 8)];
  85. // ignore invalid format; parse only [num][char-format] or [num] [char-format]
  86. yield ['Hello! Please add 1y2w to ...', new CarbonInterval(1, 0, 2)];
  87. yield ['nothing to parse :(', new CarbonInterval(0)];
  88. // case insensitive
  89. yield ['1Y 3MO 1W 3D 12H 23M 42S', new CarbonInterval(1, 3, 1, 3, 12, 23, 42)];
  90. // big numbers
  91. yield ['1999999999999.5 hours', new CarbonInterval(0, 0, 0, 0, 1999999999999, 30, 0)];
  92. yield [(0x7fffffffffffffff).' days', new CarbonInterval(0, 0, 0, 0x7fffffffffffffff, 0, 0, 0)];
  93. yield ['1999999999999.5 hours -85 minutes', new CarbonInterval(0, 0, 0, 0, 1999999999999, 115, 0)];
  94. yield ['2.333 seconds', new CarbonInterval(0, 0, 0, 0, 0, 0, 2, 333000)];
  95. }
  96. /**
  97. * @dataProvider \Tests\CarbonInterval\FromStringTest::dataForInvalidStrings
  98. *
  99. * @param string $string
  100. * @param string $part
  101. */
  102. public function testThrowsExceptionForUnknownValues($string, $part)
  103. {
  104. $message = null;
  105. try {
  106. CarbonInterval::fromString($string);
  107. } catch (InvalidArgumentException $exception) {
  108. $message = $exception->getMessage();
  109. }
  110. $this->assertStringContainsString($part, $message);
  111. }
  112. public static function dataForInvalidStrings(): Generator
  113. {
  114. yield ['1q', '1q'];
  115. yield ['about 12..14m', '12..'];
  116. yield ['4h 13', '13'];
  117. }
  118. }