diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..18c9e75 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,18 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.idea export-ignore +/.vscode export-ignore +/tests export-ignore +/phpstan-baseline.neon export-ignore +/phpstan.neon export-ignore +/phpunit.xml export-ignore +/pint.json export-ignore +/UPGRADING.md export-ignore +/ray.php export-ignore +/.run export-ignore +/rector.php export-ignore diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..0bc378d --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,12 @@ +# Please see the documentation for all configuration options: +# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" diff --git a/.github/workflows/analyse.yml b/.github/workflows/analyse.yml new file mode 100644 index 0000000..7372b87 --- /dev/null +++ b/.github/workflows/analyse.yml @@ -0,0 +1,31 @@ +name: Static Analysis (PHPStan) + +on: + push: + branches: + - master + paths: + - '**.php' + - 'phpstan.neon' + pull_request: +# schedule: +# - cron: '0 0 * * 5' + +jobs: + phpstan: + name: phpstan + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.4' + coverage: none + + - name: Install composer dependencies + uses: ramsey/composer-install@v2 + + - name: Run PHPStan + run: ./vendor/bin/phpstan --error-format=github diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 0000000..13e1043 --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,22 @@ +name: Format Dry Run (Pint) + +on: + push: + paths: + - '**.php' + branches: + - master + # - develop + # - '*.x' + pull_request: + +jobs: + format: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: "Run formatter" + uses: aglipanci/laravel-pint-action@2.3.0 + with: + testMode: true + useComposer: true diff --git a/.github/workflows/pull-requests.yml b/.github/workflows/pull-requests.yml new file mode 100644 index 0000000..18b32b3 --- /dev/null +++ b/.github/workflows/pull-requests.yml @@ -0,0 +1,12 @@ +name: pull requests + +on: + pull_request_target: + types: [opened] + +permissions: + pull-requests: write + +jobs: + uneditable: + uses: laravel/.github/.github/workflows/pull-requests.yml@main diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..6e9309e --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,50 @@ +name: Tests + +#on: [push] + +on: + push: + branches: + - master +# - develop +# - '*.x' + pull_request: +# schedule: +# - cron: '0 0 * * 5' + +#permissions: +# contents: read + +jobs: + tests: + runs-on: ubuntu-latest + + strategy: + fail-fast: true + matrix: + php: [8.2, 8.3, 8.4] + + name: PHP ${{ matrix.php }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite + coverage: pcov + + - name: Install Composer dependencies + run: composer i --prefer-dist --no-interaction --no-progress + + - name: Copy environment file + run: cp .env.example .env + + - name: Generate app key + run: php artisan key:generate + + - name: Execute tests + run: vendor/bin/pest --ci --parallel --coverage \ No newline at end of file diff --git a/.gitignore b/.gitignore index dbcd112..c772037 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/ vendor/ build/ +composer.lock \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100755 index 3c42620..0000000 --- a/.travis.yml +++ /dev/null @@ -1,40 +0,0 @@ -language: php - -dist: trusty - -sudo: false - -matrix: - fast_finish: true - include: - - php: 7.0 - - php: 7.1 - - php: 7.2 - - php: 7.3 - - php: 7.4 - - php: 8.0 - -branches: - only: - - "master" - -cache: - directories: - - $HOME/.composer/cache - - vendor - -before_install: - - composer selfupdate - -install: - - composer install --no-progress --no-interaction --prefer-dist - -before_script: - - mkdir -p build/logs - -script: - - composer run test - - composer run phpstan - -after_script: - - php ./vendor/bin/coveralls diff --git a/composer.json b/composer.json index e4e8c30..6e6e98f 100755 --- a/composer.json +++ b/composer.json @@ -20,18 +20,14 @@ } ], "require": { - "php": ">=5.4" + "php": "^8.2" }, "require-dev": { - "phpstan/phpstan": "^0.12.81", - "phpunit/phpunit": "~4.8|~5.0", - "satooshi/php-coveralls": "~1.0", - "symfony/yaml": "~2.0", - "symfony/config": "~2.0", - "symfony/console": "~2.0", - "symfony/stopwatch": "~2.0", - "symfony/filesystem": "~2.0", - "doctrine/instantiator": "1.0.4" + "composer-runtime-api": "^2.2", + "phpstan/phpstan": "^2.1.12", + "pestphp/pest": "^3.8.2", + "laravel/pint": "^1.22", + "rector/rector": "^2.0" }, "autoload": { "psr-4": { @@ -39,8 +35,15 @@ } }, "scripts": { - "test": "phpunit -c phpunit.xml", - "phpstan": "vendor/bin/phpstan" + "test": "vendor/bin/pest --parallel", + "format": "vendor/bin/pint", + "analyse": "vendor/bin/phpstan", + "refactor": "vendor/bin/rector process" }, - "minimum-stability": "stable" + "minimum-stability": "stable", + "config": { + "allow-plugins": { + "pestphp/pest-plugin": true + } + } } diff --git a/phpstan.neon.dist b/phpstan.neon similarity index 71% rename from phpstan.neon.dist rename to phpstan.neon index 6e451fa..776ccd8 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon @@ -1,4 +1,4 @@ parameters: - level: 6 + level: max paths: - src diff --git a/phpunit.xml b/phpunit.xml index bd8e429..9f368f7 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,7 +10,7 @@ > - test/ + tests/ diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..d303053 --- /dev/null +++ b/pint.json @@ -0,0 +1,19 @@ +{ + "preset": "laravel", + "exclude": [ + "config", + "build" + ], + "notPath": [ + "ray.php", + "composer-setup.php" + ], + "cache-file": "build/pint", + "rules": { + "declare_strict_types": true, + "single_trait_insert_per_statement": true, + "strict_comparison": true, + "strict_param": true, + "yoda_style": true + } +} diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..b959170 --- /dev/null +++ b/rector.php @@ -0,0 +1,14 @@ +withPhpSets() + ->withPaths([ + __DIR__.'/src', + __DIR__.'/tests', + ]) + ->withCache( + cacheDirectory: 'build/rector', + cacheClass: Rector\Caching\ValueObject\Storage\FileCacheStorage::class, + ); diff --git a/src/Duration.php b/src/Duration.php index 82f5d73..54ec695 100755 --- a/src/Duration.php +++ b/src/Duration.php @@ -1,66 +1,30 @@ reset(); @@ -69,8 +33,6 @@ public function __construct($duration = null, $hoursPerDay = 24) $this->minutesRegex = '/([0-9]{1,2})\s?(?:m|M)/'; $this->secondsRegex = '/([0-9]{1,2}(\.\d+)?)\s?(?:s|S)/'; - $this->hoursPerDay = $hoursPerDay; - if (null !== $duration) { $this->parse($duration); } @@ -79,10 +41,10 @@ public function __construct($duration = null, $hoursPerDay = 24) /** * Attempt to parse one of the forms of duration. * - * @param int|float|string|null $duration A string or number, representing a duration + * @param float|int|string|null $duration A string or number, representing a duration * @return self|bool returns the Duration object if successful, otherwise false */ - public function parse($duration) + public function parse(float|int|string|null $duration): bool|Duration|static { $this->reset(); @@ -91,28 +53,28 @@ public function parse($duration) } if (is_numeric($duration)) { - $this->seconds = (float)$duration; + $this->seconds = (float) $duration; if ($this->seconds >= 60) { - $this->minutes = (int)floor($this->seconds / 60); + $this->minutes = (int) floor($this->seconds / 60); // count current precision $precision = 0; - if (($delimiterPos = strpos((string)$this->seconds, '.')) !== false) { - $precision = strlen(substr((string)$this->seconds, $delimiterPos + 1)); + if (($delimiterPos = strpos((string) $this->seconds, '.')) !== false) { + $precision = strlen(substr((string) $this->seconds, $delimiterPos + 1)); } - $this->seconds = (float)round(($this->seconds - ($this->minutes * 60)), $precision); + $this->seconds = round(($this->seconds - ($this->minutes * 60)), $precision); } if ($this->minutes >= 60) { - $this->hours = (int)floor($this->minutes / 60); - $this->minutes = (int)($this->minutes - ($this->hours * 60)); + $this->hours = (int) floor($this->minutes / 60); + $this->minutes = (int) ($this->minutes - ($this->hours * 60)); } if ($this->hours >= $this->hoursPerDay) { - $this->days = (int)floor($this->hours / $this->hoursPerDay); - $this->hours = (int)($this->hours - ($this->days * $this->hoursPerDay)); + $this->days = (int) floor($this->hours / $this->hoursPerDay); + $this->hours = (int) ($this->hours - ($this->days * $this->hoursPerDay)); } return $this; @@ -121,14 +83,14 @@ public function parse($duration) if (preg_match('/\:/', $duration)) { $parts = explode(':', $duration); - if (count($parts) == 2) { - $this->minutes = (int)$parts[0]; - $this->seconds = (float)$parts[1]; + if (2 === count($parts)) { + $this->minutes = (int) $parts[0]; + $this->seconds = (float) $parts[1]; } else { - if (count($parts) == 3) { - $this->hours = (int)$parts[0]; - $this->minutes = (int)$parts[1]; - $this->seconds = (float)$parts[2]; + if (3 === count($parts)) { + $this->hours = (int) $parts[0]; + $this->minutes = (int) $parts[1]; + $this->seconds = (float) $parts[2]; } } @@ -141,22 +103,22 @@ public function parse($duration) preg_match($this->secondsRegex, $duration)) { if (preg_match($this->daysRegex, $duration, $matches)) { $num = $this->numberBreakdown((float) $matches[1]); - $this->days += (int)$num[0]; + $this->days += (int) $num[0]; $this->hours += $num[1] * $this->hoursPerDay; } if (preg_match($this->hoursRegex, $duration, $matches)) { $num = $this->numberBreakdown((float) $matches[1]); - $this->hours += (int)$num[0]; + $this->hours += (int) $num[0]; $this->minutes += $num[1] * 60; } if (preg_match($this->minutesRegex, $duration, $matches)) { - $this->minutes += (int)$matches[1]; + $this->minutes += (int) $matches[1]; } if (preg_match($this->secondsRegex, $duration, $matches)) { - $this->seconds += (float)$matches[1]; + $this->seconds += (float) $matches[1]; } return $this; @@ -170,18 +132,17 @@ public function parse($duration) * * For example, one hour and 42 minutes would be "6120" * - * @param int|float|string $duration A string or number, representing a duration - * @param int|bool $precision Number of decimal digits to round to. If set to false, the number is not rounded. - * @return int|float + * @param float|int|string|null $duration A string or number, representing a duration + * @param int|false $precision Number of decimal digits to round to. If set to false, the number is not rounded. */ - public function toSeconds($duration = null, $precision = false) + public function toSeconds(float|int|string|null $duration = null, int|false $precision = false): int|float { if (null !== $duration) { $this->parse($duration); } $this->output = ($this->days * $this->hoursPerDay * 60 * 60) + ($this->hours * 60 * 60) + ($this->minutes * 60) + $this->seconds; - return $precision !== false ? round($this->output, $precision) : $this->output; + return false !== $precision ? round($this->output, $precision) : $this->output; } /** @@ -189,25 +150,24 @@ public function toSeconds($duration = null, $precision = false) * * For example, one hour and 42 minutes would be "102" minutes * - * @param int|float|string $duration A string or number, representing a duration - * @param int|bool $precision Number of decimal digits to round to. If set to false, the number is not rounded. - * @return int|float + * @param float|int|string|null $duration A string or number, representing a duration + * @param bool|int $precision Number of decimal digits to round to. If set to false, the number is not rounded. */ - public function toMinutes($duration = null, $precision = false) + public function toMinutes(float|int|string|null $duration = null, bool|int $precision = false): float|int { if (null !== $duration) { $this->parse($duration); } // backward compatibility, true = round to integer - if ($precision === true) { + if (true === $precision) { $precision = 0; } $this->output = ($this->days * $this->hoursPerDay * 60 * 60) + ($this->hours * 60 * 60) + ($this->minutes * 60) + $this->seconds; $result = intval($this->output()) / 60; - return $precision !== false ? round($result, $precision) : $result; + return false !== $precision ? round($result, $precision) : $result; } /** @@ -218,11 +178,10 @@ public function toMinutes($duration = null, $precision = false) * - 42 minutes would be "0:42:00" * - 28 seconds would be "0:00:28" * - * @param int|float|string|null $duration A string or number, representing a duration - * @param bool $zeroFill A boolean, to force zero-fill result or not (see example) - * @return string + * @param float|int|string|null $duration A string or number, representing a duration + * @param bool $zeroFill A boolean, to force zero-fill result or not (see example) */ - public function formatted($duration = null, $zeroFill = false) + public function formatted(float|int|string|null $duration = null, bool $zeroFill = false): string { if (null !== $duration) { $this->parse($duration); @@ -232,7 +191,7 @@ public function formatted($duration = null, $zeroFill = false) if ($this->seconds > 0) { if ($this->seconds < 10 && ($this->minutes > 0 || $hours > 0 || $zeroFill)) { - $this->output .= '0' . $this->seconds; + $this->output .= '0'.$this->seconds; } else { $this->output .= $this->seconds; } @@ -246,25 +205,25 @@ public function formatted($duration = null, $zeroFill = false) if ($this->minutes > 0) { if ($this->minutes <= 9 && ($hours > 0 || $zeroFill)) { - $this->output = '0' . $this->minutes . ':' . $this->output; + $this->output = '0'.$this->minutes.':'.$this->output; } else { - $this->output = $this->minutes . ':' . $this->output; + $this->output = $this->minutes.':'.$this->output; } } else { if ($hours > 0 || $zeroFill) { - $this->output = '00' . ':' . $this->output; + $this->output = '00'.':'.$this->output; } } if ($hours > 0) { - $this->output = $hours . ':' . $this->output; + $this->output = $hours.':'.$this->output; } else { if ($zeroFill) { - $this->output = '0' . ':' . $this->output; + $this->output = '0'.':'.$this->output; } } - return $this->output(); + return (string) $this->output(); } /** @@ -272,40 +231,37 @@ public function formatted($duration = null, $zeroFill = false) * * For example, one hour and 42 minutes would be "1h 42m" * - * @param int|float|string $duration A string or number, representing a duration - * @return string + * @param float|int|string|null $duration A string or number, representing a duration */ - public function humanize($duration = null) + public function humanize(float|int|string|null $duration = null): string { if (null !== $duration) { $this->parse($duration); } - if ($this->seconds > 0 || ($this->seconds === 0.0 && $this->minutes === 0 && $this->hours === 0 && $this->days === 0)) { - $this->output .= $this->seconds . 's'; + if ($this->seconds > 0 || (0.0 === $this->seconds && 0 === $this->minutes && 0 === $this->hours && 0 === $this->days)) { + $this->output .= $this->seconds.'s'; } if ($this->minutes > 0) { - $this->output = $this->minutes . 'm ' . $this->output; + $this->output = $this->minutes.'m '.$this->output; } if ($this->hours > 0) { - $this->output = $this->hours . 'h ' . $this->output; + $this->output = $this->hours.'h '.$this->output; } if ($this->days > 0) { - $this->output = $this->days . 'd ' . $this->output; + $this->output = $this->days.'d '.$this->output; } - return trim($this->output()); + return trim((string) $this->output()); } - /** - * @param float $number - * @return array|float[]|int[] + * @return list */ - private function numberBreakdown($number) + private function numberBreakdown(float $number): array { $negative = 1; @@ -314,20 +270,16 @@ private function numberBreakdown($number) $number *= -1; } - return array( + return [ floor($number) * $negative, - ($number - floor($number)) * $negative - ); + ($number - floor($number)) * $negative, + ]; } - /** * Resets the Duration object by clearing the output and values. - * - * @access private - * @return void */ - private function reset() + private function reset(): void { $this->output = ''; $this->seconds = 0.0; @@ -338,11 +290,8 @@ private function reset() /** * Returns the output of the Duration object and resets. - * - * @access private - * @return string */ - private function output() + private function output(): int|string|float { $out = $this->output; @@ -351,4 +300,3 @@ private function output() return $out; } } - diff --git a/test/DurationTest.php b/test/DurationTest.php deleted file mode 100755 index 0564564..0000000 --- a/test/DurationTest.php +++ /dev/null @@ -1,370 +0,0 @@ -d = new Duration; - } - - public function secondsSampleData() - { - return array( - array(false, null), - array(false, ' '), - array(0, '0 s'), - array(1, '1 s'), - array(1, '1 sec'), - array(3, '3S'), - array(7, '7 S'), - array(51, '51seconds'), - array(4, '4 Sec.'), - array(15, '15 SEcONDs'), - array(1, '1.0 s'), - array(1.5689, '1.5689 S'), - array(1.00342, '1.00342 S'), - ); - } - - /** - * @dataProvider secondsSampleData - */ - public function testGettingValueFromSecondSuffixes($expectedSeconds, $secStr) - { - $this->d->parse($secStr); - $this->assertEquals($expectedSeconds, $this->d->seconds); - } - - public function minutesSampleData() - { - return array( - array(0, '0m'), - array(1, '1 m'), - array(4, '4 min'), - array(6, '6M'), - array(14, '14 Ms'), - array(31, '31 minutes'), - array(9, '9Min.'), - array(11, '11 MINUTE') - ); - } - - /** - * @dataProvider minutesSampleData - */ - public function testGettingValueFromMinuteSuffixes($intVal, $minStr) - { - $this->d->parse($minStr); - $this->assertEquals($intVal, $this->d->minutes); - } - - public function hoursSampleData() - { - return array( - array(0, '0h'), - array(1, '1 h'), - array(1, '1 hr'), - array(1, '1H'), - array(24, '24 Hrs'), - array(3, '3hours'), - array(6, '6HoUr'), - array(14, '14 HOURs'), - array(36, '36h') - ); - } - - /** - * @dataProvider hoursSampleData - */ - public function testGettingValueFromHourSuffixes($intVal, $hrStr) - { - $this->d->parse($hrStr); - $this->assertEquals($intVal, $this->d->hours); - } - - public function daysSampleData() - { - return array( - array(0, '0d'), - array(1, '1 d'), - array(1, '1 D'), - array(1, '1D'), - array(24, '24 ds'), - array(3, '3days'), - array(6, '6DaY'), - array(14, '14 DAYs') - ); - } - - /** - * @dataProvider daysSampleData - */ - public function testGettingValueFromDaySuffixes($intVal, $dayStr) - { - $this->d->parse($dayStr); - $this->assertEquals($intVal, $this->d->days); - } - - - public function testConvertingSecondsToFormattedString() - { - $this->assertEquals('0', $this->d->formatted(0)); - $this->assertEquals('4', $this->d->formatted(4)); - $this->assertEquals('9', $this->d->formatted(9)); - $this->assertEquals('42', $this->d->formatted(42)); - $this->assertEquals('1:02', $this->d->formatted(62)); - $this->assertEquals('1:09', $this->d->formatted(69)); - $this->assertEquals('1:42', $this->d->formatted(102)); - $this->assertEquals('10:47', $this->d->formatted(647)); - $this->assertEquals('1:00:00', $this->d->formatted(3600)); - $this->assertEquals('1:00:01', $this->d->formatted(3601)); - $this->assertEquals('1:00:11', $this->d->formatted(3611)); - $this->assertEquals('1:01:00', $this->d->formatted(3660)); - $this->assertEquals('1:01:14', $this->d->formatted(3674)); - $this->assertEquals('1:04:25', $this->d->formatted(3865)); - $this->assertEquals('1:09:09', $this->d->formatted(4149)); - - // microseconds - $this->assertEquals('0.0', $this->d->formatted(0.0)); - $this->assertEquals('4.987', $this->d->formatted(4.987)); - $this->assertEquals('9.123', $this->d->formatted(9.123)); - $this->assertEquals('42.672', $this->d->formatted(42.672)); - $this->assertEquals('1:02.23', $this->d->formatted(62.23)); - $this->assertEquals('1:09.9', $this->d->formatted(69.9)); - $this->assertEquals('1:42.62394', $this->d->formatted(102.62394)); - $this->assertEquals('10:47.5', $this->d->formatted(647.5)); - $this->assertEquals('1:00:00.954', $this->d->formatted(3600.954)); - $this->assertEquals('1:00:01.5123', $this->d->formatted(3601.5123)); - $this->assertEquals('1:00:11.0412368456', $this->d->formatted(3611.0412368456)); - $this->assertEquals('1:01:00.56945', $this->d->formatted(3660.56945)); - $this->assertEquals('1:01:14.3', $this->d->formatted(3674.3)); - $this->assertEquals('1:04:25.0005598', $this->d->formatted(3865.0005598)); - $this->assertEquals('1:09:09.123', $this->d->formatted(4149.123)); - } - - public function testConvertingSecondsToFormattedStringZeroFilled() - { - $this->assertEquals('0:00:00', $this->d->formatted(0, true)); - $this->assertEquals('0:00:04', $this->d->formatted(4, true)); - $this->assertEquals('0:00:09', $this->d->formatted(9, true)); - $this->assertEquals('0:00:42', $this->d->formatted(42, true)); - $this->assertEquals('0:01:02', $this->d->formatted(62, true)); - $this->assertEquals('0:01:09', $this->d->formatted(69, true)); - $this->assertEquals('0:01:42', $this->d->formatted(102, true)); - $this->assertEquals('0:10:47', $this->d->formatted(647, true)); - $this->assertEquals('1:00:00', $this->d->formatted(3600, true)); - $this->assertEquals('1:00:01', $this->d->formatted(3601, true)); - $this->assertEquals('1:00:11', $this->d->formatted(3611, true)); - $this->assertEquals('1:01:00', $this->d->formatted(3660, true)); - $this->assertEquals('1:01:14', $this->d->formatted(3674, true)); - $this->assertEquals('1:04:25', $this->d->formatted(3865, true)); - $this->assertEquals('1:09:09', $this->d->formatted(4149, true)); - - // microseconds - $this->assertEquals('0:00:04.542', $this->d->formatted(4.542, true)); - $this->assertEquals('1:09:09.0987', $this->d->formatted(4149.0987, true)); - } - - public function testConvertingFormattedStringsToSeconds() - { - $this->assertEquals(0, $this->d->toSeconds('0')); - $this->assertEquals(4, $this->d->toSeconds('4')); - $this->assertEquals(9, $this->d->toSeconds('9')); - $this->assertEquals(42, $this->d->toSeconds('42')); - $this->assertEquals(62, $this->d->toSeconds('1:02')); - $this->assertEquals(69, $this->d->toSeconds('1:09')); - $this->assertEquals(102, $this->d->toSeconds('1:42')); - $this->assertEquals(647, $this->d->toSeconds('10:47')); - $this->assertEquals(3600, $this->d->toSeconds('1:00:00')); - $this->assertEquals(3601, $this->d->toSeconds('1:00:01')); - $this->assertEquals(3611, $this->d->toSeconds('1:00:11')); - $this->assertEquals(3660, $this->d->toSeconds('1:01:00')); - $this->assertEquals(3674, $this->d->toSeconds('1:01:14')); - $this->assertEquals(3865, $this->d->toSeconds('1:04:25')); - $this->assertEquals(4149, $this->d->toSeconds('1:09:09')); - - // microseconds - $this->assertEquals(4.6, $this->d->toSeconds('4.6')); - $this->assertEquals(9.5, $this->d->toSeconds('9.5')); - $this->assertEquals(42.1, $this->d->toSeconds('42.1')); - $this->assertEquals(62.96, $this->d->toSeconds('1:02.96')); - $this->assertEquals(69.23, $this->d->toSeconds('1:09.23')); - $this->assertEquals(102.55, $this->d->toSeconds('1:42.55')); - $this->assertEquals(647.999, $this->d->toSeconds('10:47.999')); - $this->assertEquals(3600.9987, $this->d->toSeconds('1:00:00.9987')); - $this->assertEquals(3601.000111, $this->d->toSeconds('1:00:01.000111')); - $this->assertEquals(3611.0999, $this->d->toSeconds('1:00:11.0999')); - $this->assertEquals(3660.500001, $this->d->toSeconds('1:01:00.500001')); - $this->assertEquals(3674.00001, $this->d->toSeconds('1:01:14.00001')); - $this->assertEquals(3865.499999, $this->d->toSeconds('1:04:25.499999')); - $this->assertEquals(4149.499999, $this->d->toSeconds('1:09:09.499999')); - - // precision - $this->assertEquals(0, $this->d->toSeconds('0', 0)); - $this->assertEquals(5, $this->d->toSeconds('4.6', 0)); - $this->assertEquals(10, $this->d->toSeconds('9.5', 0)); - $this->assertEquals(42.1, $this->d->toSeconds('42.1', 1)); - $this->assertEquals(63, $this->d->toSeconds('1:02.96', 1)); - $this->assertEquals(69.23, $this->d->toSeconds('1:09.23')); - $this->assertEquals(102.55, $this->d->toSeconds('1:42.55', 2)); - $this->assertEquals(648, $this->d->toSeconds('10:47.999', 2)); - $this->assertEquals(3601, $this->d->toSeconds('1:00:00.9987', 2)); - $this->assertEquals(3601, $this->d->toSeconds('1:00:01.000111', 3)); - $this->assertEquals(3611.0999, $this->d->toSeconds('1:00:11.0999', 4)); - $this->assertEquals(3660.5, $this->d->toSeconds('1:01:00.500001', 2)); - $this->assertEquals(3674, $this->d->toSeconds('1:01:14.00001', 2)); - $this->assertEquals(3865.5, $this->d->toSeconds('1:04:25.499999', 3)); - $this->assertEquals(4149.499997, $this->d->toSeconds('1:09:09.4999971', 6)); - } - - public function testConvertingFormattedStringsToMinutes() - { - $this->assertEquals(0, $this->d->toMinutes('0')); - $this->assertEquals(4 / 60, $this->d->toMinutes('4')); - $this->assertEquals(9 / 60, $this->d->toMinutes('9')); - $this->assertEquals(42 / 60, $this->d->toMinutes('42')); - $this->assertEquals(62 / 60, $this->d->toMinutes('1:02')); - $this->assertEquals(69 / 60, $this->d->toMinutes('1:09')); - $this->assertEquals(102 / 60, $this->d->toMinutes('1:42')); - $this->assertEquals(647 / 60, $this->d->toMinutes('10:47')); - $this->assertEquals(3600 / 60, $this->d->toMinutes('1:00:00')); - $this->assertEquals(3601 / 60, $this->d->toMinutes('1:00:01')); - $this->assertEquals(3611 / 60, $this->d->toMinutes('1:00:11')); - $this->assertEquals(3660 / 60, $this->d->toMinutes('1:01:00')); - $this->assertEquals(3674 / 60, $this->d->toMinutes('1:01:14')); - $this->assertEquals(3865 / 60, $this->d->toMinutes('1:04:25')); - $this->assertEquals(4149 / 60, $this->d->toMinutes('1:09:09')); - - // to integer - BC - $this->assertEquals(0, $this->d->toMinutes('0', true)); - $this->assertEquals(0, $this->d->toMinutes('4', true)); - $this->assertEquals(0, $this->d->toMinutes('9', true)); - $this->assertEquals(1, $this->d->toMinutes('42', true)); - $this->assertEquals(1, $this->d->toMinutes('1:02', true)); - $this->assertEquals(1, $this->d->toMinutes('1:09', true)); - $this->assertEquals(2, $this->d->toMinutes('1:42', true)); - $this->assertEquals(11, $this->d->toMinutes('10:47', true)); - $this->assertEquals(60, $this->d->toMinutes('1:00:00', true)); - $this->assertEquals(60, $this->d->toMinutes('1:00:01', true)); - $this->assertEquals(60, $this->d->toMinutes('1:00:11', true)); - $this->assertEquals(61, $this->d->toMinutes('1:01:00', true)); - $this->assertEquals(61, $this->d->toMinutes('1:01:14', true)); - $this->assertEquals(64, $this->d->toMinutes('1:04:25', true)); - $this->assertEquals(65, $this->d->toMinutes('1:04:55', true)); - $this->assertEquals(69, $this->d->toMinutes('1:09:09', true)); - - // precision - $this->assertEquals(0, $this->d->toMinutes('0', 0)); - $this->assertEquals(0, $this->d->toMinutes('4', 0)); - $this->assertEquals(0, $this->d->toMinutes('9', 0)); - $this->assertEquals(1, $this->d->toMinutes('42', 0)); - $this->assertEquals(1, $this->d->toMinutes('1:02', 0)); - $this->assertEquals(1, $this->d->toMinutes('1:09', 0)); - $this->assertEquals(2, $this->d->toMinutes('1:42', 0)); - $this->assertEquals(11, $this->d->toMinutes('10:47', 0)); - $this->assertEquals(60, $this->d->toMinutes('1:00:00', 0)); - $this->assertEquals(60, $this->d->toMinutes('1:00:01', 0)); - $this->assertEquals(60, $this->d->toMinutes('1:00:11', 0)); - $this->assertEquals(61, $this->d->toMinutes('1:01:00', 0)); - $this->assertEquals(61, $this->d->toMinutes('1:01:14', 0)); - $this->assertEquals(64, $this->d->toMinutes('1:04:25', 0)); - $this->assertEquals(65, $this->d->toMinutes('1:04:55', 0)); - $this->assertEquals(69, $this->d->toMinutes('1:09:09', 0)); - - $this->assertEquals(0, $this->d->toMinutes('0', 1)); - $this->assertEquals(0.1, $this->d->toMinutes('4', 1)); - $this->assertEquals(0.15, $this->d->toMinutes('9', 2)); - $this->assertEquals(0.7, $this->d->toMinutes('42', 3)); - $this->assertEquals(1, $this->d->toMinutes('1:02', 1)); - $this->assertEquals(1.15, $this->d->toMinutes('1:09', 2)); - $this->assertEquals(1.7, $this->d->toMinutes('1:42', 3)); - $this->assertEquals(10.8, $this->d->toMinutes('10:47', 1)); - $this->assertEquals(60, $this->d->toMinutes('1:00:00', 2)); - $this->assertEquals(60.017, $this->d->toMinutes('1:00:01', 3)); - $this->assertEquals(60.2, $this->d->toMinutes('1:00:11', 1)); - $this->assertEquals(61, $this->d->toMinutes('1:01:00', 2)); - $this->assertEquals(61.233, $this->d->toMinutes('1:01:14', 3)); - $this->assertEquals(64.42, $this->d->toMinutes('1:04:25', 2)); - $this->assertEquals(64.92, $this->d->toMinutes('1:04:55', 2)); - $this->assertEquals(69.15, $this->d->toMinutes('1:09:09', 2)); - } - - public function testConvertSecondsToHumanizedString() - { - $this->assertEquals('0s', $this->d->humanize(0)); - $this->assertEquals('4s', $this->d->humanize(4)); - $this->assertEquals('42s', $this->d->humanize(42)); - $this->assertEquals('1m 2s', $this->d->humanize(62)); - $this->assertEquals('1m 42s', $this->d->humanize(102)); - $this->assertEquals('10m 47s', $this->d->humanize(647)); - $this->assertEquals('1h', $this->d->humanize(3600)); - $this->assertEquals('1h 5s', $this->d->humanize(3605)); - $this->assertEquals('1h 1m', $this->d->humanize(3660)); - $this->assertEquals('1h 1m 5s', $this->d->humanize(3665)); - $this->assertEquals('3d', $this->d->humanize(259200)); - $this->assertEquals('2d 11h 30m', $this->d->humanize(214200)); - - $this->assertEquals('4.0596s', $this->d->humanize(4.0596)); - $this->assertEquals('2d 11h 30m 0.9542s', $this->d->humanize(214200.9542)); - - } - - /** - * @depends testGettingValueFromSecondSuffixes - * @depends testGettingValueFromMinuteSuffixes - * @depends testGettingValueFromHourSuffixes - */ - public function testConvertHumanizedStringToSeconds() - { - $this->assertEquals(0, $this->d->toSeconds('0s')); - $this->assertEquals(4, $this->d->toSeconds('4s')); - $this->assertEquals(42, $this->d->toSeconds('42s')); - $this->assertEquals(72, $this->d->toSeconds('1m 12s')); - $this->assertEquals(102, $this->d->toSeconds('1m 42s')); - $this->assertEquals(647, $this->d->toSeconds('10m 47s')); - $this->assertEquals(3600, $this->d->toSeconds('1h')); - $this->assertEquals(3605, $this->d->toSeconds('1h 5s')); - $this->assertEquals(3660, $this->d->toSeconds('1h 1m')); - $this->assertEquals(3665, $this->d->toSeconds('1h 1m 5s')); - $this->assertEquals(86400, $this->d->toSeconds('1d')); - $this->assertEquals(214200, $this->d->toSeconds('2d 11h 30m')); - $this->assertEquals(214214, $this->d->toSeconds('2d 11h 30m 14s')); - } - - public function testConvertHumanizedStringToSeconds7HourDay() - { - $d = new Duration(null, 7); - - $this->assertEquals(0, $d->toSeconds('0d')); - $this->assertEquals(25200, $d->toSeconds('1d')); - $this->assertEquals(91800, $d->toSeconds('2d 11h 30m')); - } - - public function testSupportDecimals() - { - $d = new Duration(null, 6); - - $this->assertEquals(0, $d->toMinutes('0d')); - $this->assertEquals(6 * 60, $d->toMinutes('1d')); - $this->assertEquals((6 + 3) * 60, $d->toMinutes('1.5d')); - $this->assertEquals(60, $d->toMinutes('1h')); - $this->assertEquals(60 + 30, $d->toMinutes('1.5h')); - $this->assertEquals((12 * 60) + 60 + 30, $d->toMinutes('2d 1.5h')); - } - - public function testConvertHumanizedWithSupportDecimals() - { - $t = '1.5d 1.5h 2m 5s'; - - $this->assertEquals('1d 4h 32m 5s', (new Duration($t, 6))->humanize(), "Test humanize with: {$t}"); - $this->assertEquals('10:32:05', (new Duration($t, 6))->formatted(), "Test formatted with: {$t}"); - $this->assertEquals(37925, (new Duration($t, 6))->toSeconds(), "Test toSeconds with: {$t}"); - $this->assertEquals(37925/60, (new Duration($t, 6))->toMinutes(), "Test toMinutes with: {$t}"); - $this->assertEquals(632, (new Duration($t, 6))->toMinutes(null, 0), "Test toMinutes with: {$t}"); - } - -} diff --git a/tests/DurationTest.php b/tests/DurationTest.php new file mode 100755 index 0000000..2831964 --- /dev/null +++ b/tests/DurationTest.php @@ -0,0 +1,331 @@ + $this->duration = new Duration); + +dataset('seconds sample data', [ + [false, null], + [false, ' '], + [0, '0 s'], + [1, '1 s'], + [1, '1 sec'], + [3, '3S'], + [7, '7 S'], + [51, '51seconds'], + [4, '4 Sec.'], + [15, '15 SEcONDs'], + [1, '1.0 s'], + [1.5_689, '1.5689 S'], + [1.00_342, '1.00342 S'], +]); + +dataset('minutes sample data', [ + [0, '0m'], + [1, '1 m'], + [4, '4 min'], + [6, '6M'], + [14, '14 Ms'], + [31, '31 minutes'], + [9, '9Min.'], + [11, '11 MINUTE'], +]); + +dataset('hours sample data', [ + [0, '0h'], + [1, '1 h'], + [1, '1 hr'], + [1, '1H'], + [24, '24 Hrs'], + [3, '3hours'], + [6, '6HoUr'], + [14, '14 HOURs'], + [36, '36h'], +]); + +dataset('days sample data', [ + [0, '0d'], + [1, '1 d'], + [1, '1 D'], + [1, '1D'], + [24, '24 ds'], + [3, '3days'], + [6, '6DaY'], + [14, '14 DAYs'], +]); + +test('Getting Value From Second Suffixes', function ($expectedSeconds, $secStr) { + $this->duration->parse($secStr); + assertEquals($expectedSeconds, $this->duration->seconds); +}) + ->with('seconds sample data'); + +test('Getting Value From Minute Suffixes', function ($intVal, $minStr) { + $this->duration->parse($minStr); + assertEquals($intVal, $this->duration->minutes); +}) + ->with('minutes sample data'); + +test('Getting Value From Hour Suffixes', function ($intVal, $hrStr) { + $this->duration->parse($hrStr); + assertEquals($intVal, $this->duration->hours); +}) + ->with('hours sample data'); + +test('Getting Value From Day Suffixes', function ($intVal, $dayStr) { + $this->duration->parse($dayStr); + assertEquals($intVal, $this->duration->days); +}) + ->with('days sample data'); + +test('Converting Seconds To Formatted String', function () { + assertEquals('0', $this->duration->formatted(0)); + assertEquals('4', $this->duration->formatted(4)); + assertEquals('9', $this->duration->formatted(9)); + assertEquals('42', $this->duration->formatted(42)); + assertEquals('1:02', $this->duration->formatted(62)); + assertEquals('1:09', $this->duration->formatted(69)); + assertEquals('1:42', $this->duration->formatted(102)); + assertEquals('10:47', $this->duration->formatted(647)); + assertEquals('1:00:00', $this->duration->formatted(3_600)); + assertEquals('1:00:01', $this->duration->formatted(3_601)); + assertEquals('1:00:11', $this->duration->formatted(3_611)); + assertEquals('1:01:00', $this->duration->formatted(3_660)); + assertEquals('1:01:14', $this->duration->formatted(3_674)); + assertEquals('1:04:25', $this->duration->formatted(3_865)); + assertEquals('1:09:09', $this->duration->formatted(4_149)); + + // microseconds + assertEquals('0', $this->duration->formatted(0.0)); + assertEquals('4.987', $this->duration->formatted(4.987)); + assertEquals('9.123', $this->duration->formatted(9.123)); + assertEquals('42.672', $this->duration->formatted(42.672)); + assertEquals('1:02.23', $this->duration->formatted(62.23)); + assertEquals('1:09.9', $this->duration->formatted(69.9)); + assertEquals('1:42.62394', $this->duration->formatted(102.62_394)); + assertEquals('10:47.5', $this->duration->formatted(647.5)); + assertEquals('1:00:00.954', $this->duration->formatted(3_600.954)); + assertEquals('1:00:01.5123', $this->duration->formatted(3_601.5_123)); + assertEquals('1:00:11.0412368456', $this->duration->formatted(3_611.0_412_368_456)); + assertEquals('1:01:00.56945', $this->duration->formatted(3_660.56_945)); + assertEquals('1:01:14.3', $this->duration->formatted(3_674.3)); + assertEquals('1:04:25.0005598', $this->duration->formatted(3_865.0_005_598)); + assertEquals('1:09:09.123', $this->duration->formatted(4_149.123)); +}); + +test('Converting Seconds To Formatted String Zero Filled', function () { + assertEquals('0:00:00', $this->duration->formatted(0, true)); + assertEquals('0:00:04', $this->duration->formatted(4, true)); + assertEquals('0:00:09', $this->duration->formatted(9, true)); + assertEquals('0:00:42', $this->duration->formatted(42, true)); + assertEquals('0:01:02', $this->duration->formatted(62, true)); + assertEquals('0:01:09', $this->duration->formatted(69, true)); + assertEquals('0:01:42', $this->duration->formatted(102, true)); + assertEquals('0:10:47', $this->duration->formatted(647, true)); + assertEquals('1:00:00', $this->duration->formatted(3_600, true)); + assertEquals('1:00:01', $this->duration->formatted(3_601, true)); + assertEquals('1:00:11', $this->duration->formatted(3_611, true)); + assertEquals('1:01:00', $this->duration->formatted(3_660, true)); + assertEquals('1:01:14', $this->duration->formatted(3_674, true)); + assertEquals('1:04:25', $this->duration->formatted(3_865, true)); + assertEquals('1:09:09', $this->duration->formatted(4_149, true)); + + // microseconds + assertEquals('0:00:04.542', $this->duration->formatted(4.542, true)); + assertEquals('1:09:09.0987', $this->duration->formatted(4_149.0987, true)); +}); + +test('Converting Formatted Strings To Seconds', function () { + assertEquals(0, $this->duration->toSeconds('0')); + assertEquals(4, $this->duration->toSeconds('4')); + assertEquals(9, $this->duration->toSeconds('9')); + assertEquals(42, $this->duration->toSeconds('42')); + assertEquals(62, $this->duration->toSeconds('1:02')); + assertEquals(69, $this->duration->toSeconds('1:09')); + assertEquals(102, $this->duration->toSeconds('1:42')); + assertEquals(647, $this->duration->toSeconds('10:47')); + assertEquals(3_600, $this->duration->toSeconds('1:00:00')); + assertEquals(3_601, $this->duration->toSeconds('1:00:01')); + assertEquals(3_611, $this->duration->toSeconds('1:00:11')); + assertEquals(3_660, $this->duration->toSeconds('1:01:00')); + assertEquals(3_674, $this->duration->toSeconds('1:01:14')); + assertEquals(3_865, $this->duration->toSeconds('1:04:25')); + assertEquals(4_149, $this->duration->toSeconds('1:09:09')); + + // microseconds + assertEquals(4.6, $this->duration->toSeconds('4.6')); + assertEquals(9.5, $this->duration->toSeconds('9.5')); + assertEquals(42.1, $this->duration->toSeconds('42.1')); + assertEquals(62.96, $this->duration->toSeconds('1:02.96')); + assertEquals(69.23, $this->duration->toSeconds('1:09.23')); + assertEquals(102.55, $this->duration->toSeconds('1:42.55')); + assertEquals(647.999, $this->duration->toSeconds('10:47.999')); + assertEquals(3_600.9_987, $this->duration->toSeconds('1:00:00.9987')); + assertEquals(3_601.000_111, $this->duration->toSeconds('1:00:01.000111')); + assertEquals(3_611.0_999, $this->duration->toSeconds('1:00:11.0999')); + assertEquals(3_660.500_001, $this->duration->toSeconds('1:01:00.500001')); + assertEquals(3_674.00_001, $this->duration->toSeconds('1:01:14.00001')); + assertEquals(3_865.499_999, $this->duration->toSeconds('1:04:25.499999')); + assertEquals(4_149.499_999, $this->duration->toSeconds('1:09:09.499999')); + + // precision + assertEquals(0, $this->duration->toSeconds('0', 0)); + assertEquals(5, $this->duration->toSeconds('4.6', 0)); + assertEquals(10, $this->duration->toSeconds('9.5', 0)); + assertEquals(42.1, $this->duration->toSeconds('42.1', 1)); + assertEquals(63, $this->duration->toSeconds('1:02.96', 1)); + assertEquals(69.23, $this->duration->toSeconds('1:09.23')); + assertEquals(102.55, $this->duration->toSeconds('1:42.55', 2)); + assertEquals(648, $this->duration->toSeconds('10:47.999', 2)); + assertEquals(3_601, $this->duration->toSeconds('1:00:00.9987', 2)); + assertEquals(3_601, $this->duration->toSeconds('1:00:01.000111', 3)); + assertEquals(3_611.0_999, $this->duration->toSeconds('1:00:11.0999', 4)); + assertEquals(3_660.5, $this->duration->toSeconds('1:01:00.500001', 2)); + assertEquals(3_674, $this->duration->toSeconds('1:01:14.00001', 2)); + assertEquals(3_865.5, $this->duration->toSeconds('1:04:25.499999', 3)); + assertEquals(4_149.499_997, $this->duration->toSeconds('1:09:09.4999971', 6)); +}); + +test('Converting Formatted Strings To Minutes', function () { + assertEquals(0, $this->duration->toMinutes('0')); + assertEquals(4 / 60, $this->duration->toMinutes('4')); + assertEquals(9 / 60, $this->duration->toMinutes('9')); + assertEquals(42 / 60, $this->duration->toMinutes('42')); + assertEquals(62 / 60, $this->duration->toMinutes('1:02')); + assertEquals(69 / 60, $this->duration->toMinutes('1:09')); + assertEquals(102 / 60, $this->duration->toMinutes('1:42')); + assertEquals(647 / 60, $this->duration->toMinutes('10:47')); + assertEquals(3_600 / 60, $this->duration->toMinutes('1:00:00')); + assertEquals(3_601 / 60, $this->duration->toMinutes('1:00:01')); + assertEquals(3_611 / 60, $this->duration->toMinutes('1:00:11')); + assertEquals(3_660 / 60, $this->duration->toMinutes('1:01:00')); + assertEquals(3_674 / 60, $this->duration->toMinutes('1:01:14')); + assertEquals(3_865 / 60, $this->duration->toMinutes('1:04:25')); + assertEquals(4_149 / 60, $this->duration->toMinutes('1:09:09')); + + // to integer - BC + assertEquals(0, $this->duration->toMinutes('0', true)); + assertEquals(0, $this->duration->toMinutes('4', true)); + assertEquals(0, $this->duration->toMinutes('9', true)); + assertEquals(1, $this->duration->toMinutes('42', true)); + assertEquals(1, $this->duration->toMinutes('1:02', true)); + assertEquals(1, $this->duration->toMinutes('1:09', true)); + assertEquals(2, $this->duration->toMinutes('1:42', true)); + assertEquals(11, $this->duration->toMinutes('10:47', true)); + assertEquals(60, $this->duration->toMinutes('1:00:00', true)); + assertEquals(60, $this->duration->toMinutes('1:00:01', true)); + assertEquals(60, $this->duration->toMinutes('1:00:11', true)); + assertEquals(61, $this->duration->toMinutes('1:01:00', true)); + assertEquals(61, $this->duration->toMinutes('1:01:14', true)); + assertEquals(64, $this->duration->toMinutes('1:04:25', true)); + assertEquals(65, $this->duration->toMinutes('1:04:55', true)); + assertEquals(69, $this->duration->toMinutes('1:09:09', true)); + + // precision + assertEquals(0, $this->duration->toMinutes('0', 0)); + assertEquals(0, $this->duration->toMinutes('4', 0)); + assertEquals(0, $this->duration->toMinutes('9', 0)); + assertEquals(1, $this->duration->toMinutes('42', 0)); + assertEquals(1, $this->duration->toMinutes('1:02', 0)); + assertEquals(1, $this->duration->toMinutes('1:09', 0)); + assertEquals(2, $this->duration->toMinutes('1:42', 0)); + assertEquals(11, $this->duration->toMinutes('10:47', 0)); + assertEquals(60, $this->duration->toMinutes('1:00:00', 0)); + assertEquals(60, $this->duration->toMinutes('1:00:01', 0)); + assertEquals(60, $this->duration->toMinutes('1:00:11', 0)); + assertEquals(61, $this->duration->toMinutes('1:01:00', 0)); + assertEquals(61, $this->duration->toMinutes('1:01:14', 0)); + assertEquals(64, $this->duration->toMinutes('1:04:25', 0)); + assertEquals(65, $this->duration->toMinutes('1:04:55', 0)); + assertEquals(69, $this->duration->toMinutes('1:09:09', 0)); + + assertEquals(0, $this->duration->toMinutes('0', 1)); + assertEquals(0.1, $this->duration->toMinutes('4', 1)); + assertEquals(0.15, $this->duration->toMinutes('9', 2)); + assertEquals(0.7, $this->duration->toMinutes('42', 3)); + assertEquals(1, $this->duration->toMinutes('1:02', 1)); + assertEquals(1.15, $this->duration->toMinutes('1:09', 2)); + assertEquals(1.7, $this->duration->toMinutes('1:42', 3)); + assertEquals(10.8, $this->duration->toMinutes('10:47', 1)); + assertEquals(60, $this->duration->toMinutes('1:00:00', 2)); + assertEquals(60.017, $this->duration->toMinutes('1:00:01', 3)); + assertEquals(60.2, $this->duration->toMinutes('1:00:11', 1)); + assertEquals(61, $this->duration->toMinutes('1:01:00', 2)); + assertEquals(61.233, $this->duration->toMinutes('1:01:14', 3)); + assertEquals(64.42, $this->duration->toMinutes('1:04:25', 2)); + assertEquals(64.92, $this->duration->toMinutes('1:04:55', 2)); + assertEquals(69.15, $this->duration->toMinutes('1:09:09', 2)); +}); + +test('Convert Seconds To Humanized String', function () { + assertEquals('0s', $this->duration->humanize(0)); + assertEquals('4s', $this->duration->humanize(4)); + assertEquals('42s', $this->duration->humanize(42)); + assertEquals('1m 2s', $this->duration->humanize(62)); + assertEquals('1m 42s', $this->duration->humanize(102)); + assertEquals('10m 47s', $this->duration->humanize(647)); + assertEquals('1h', $this->duration->humanize(3_600)); + assertEquals('1h 5s', $this->duration->humanize(3_605)); + assertEquals('1h 1m', $this->duration->humanize(3_660)); + assertEquals('1h 1m 5s', $this->duration->humanize(3_665)); + assertEquals('3d', $this->duration->humanize(259_200)); + assertEquals('2d 11h 30m', $this->duration->humanize(214_200)); + + assertEquals('4.0596s', $this->duration->humanize(4.0_596)); + assertEquals('2d 11h 30m 0.9542s', $this->duration->humanize(214_200.9_542)); + +}); + +test('Convert Humanized String To Seconds', function () { + assertEquals(0, $this->duration->toSeconds('0s')); + assertEquals(4, $this->duration->toSeconds('4s')); + assertEquals(42, $this->duration->toSeconds('42s')); + assertEquals(72, $this->duration->toSeconds('1m 12s')); + assertEquals(102, $this->duration->toSeconds('1m 42s')); + assertEquals(647, $this->duration->toSeconds('10m 47s')); + assertEquals(3_600, $this->duration->toSeconds('1h')); + assertEquals(3_605, $this->duration->toSeconds('1h 5s')); + assertEquals(3_660, $this->duration->toSeconds('1h 1m')); + assertEquals(3_665, $this->duration->toSeconds('1h 1m 5s')); + assertEquals(86_400, $this->duration->toSeconds('1d')); + assertEquals(214_200, $this->duration->toSeconds('2d 11h 30m')); + assertEquals(214_214, $this->duration->toSeconds('2d 11h 30m 14s')); +}) + ->depends( + 'Getting Value From Second Suffixes', + 'Getting Value From Minute Suffixes', + 'Getting Value From Hour Suffixes' + ); + +test('Convert Humanized String To Seconds 7 Hour Day', function () { + $d = new Duration(null, 7); + + assertEquals(0, $d->toSeconds('0d')); + assertEquals(25_200, $d->toSeconds('1d')); + assertEquals(91_800, $d->toSeconds('2d 11h 30m')); +}); + +test('Support Decimals', function () { + $d = new Duration(null, 6); + + assertEquals(0, $d->toMinutes('0d')); + assertEquals(6 * 60, $d->toMinutes('1d')); + assertEquals((6 + 3) * 60, $d->toMinutes('1.5d')); + assertEquals(60, $d->toMinutes('1h')); + assertEquals(60 + 30, $d->toMinutes('1.5h')); + assertEquals((12 * 60) + 60 + 30, $d->toMinutes('2d 1.5h')); +}); + +test('Convert Humanized With Support Decimals', function () { + $t = '1.5d 1.5h 2m 5s'; + + assertEquals('1d 4h 32m 5s', (new Duration($t, 6))->humanize(), "Test humanize with: {$t}"); + assertEquals('10:32:05', (new Duration($t, 6))->formatted(), "Test formatted with: {$t}"); + assertEquals(37_925, (new Duration($t, 6))->toSeconds(), "Test toSeconds with: {$t}"); + assertEquals(37_925 / 60, (new Duration($t, 6))->toMinutes(), "Test toMinutes with: {$t}"); + assertEquals(632, (new Duration($t, 6))->toMinutes(null, 0), "Test toMinutes with: {$t}"); +}); diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..2fe1a60 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,6 @@ +in(__DIR__);