diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
new file mode 100644
index 0000000..94fed74
--- /dev/null
+++ b/.github/workflows/tests.yml
@@ -0,0 +1,22 @@
+name: Tests
+
+on:
+ push:
+ pull_request:
+
+jobs:
+ tests:
+ runs-on: ubuntu-latest
+ strategy:
+ fail-fast: false
+ matrix:
+ php: ['7.1', '8.5']
+
+ steps:
+ - uses: actions/checkout@v4
+ - uses: shivammathur/setup-php@v2
+ with:
+ php-version: ${{ matrix.php }}
+ coverage: none
+ - run: composer update --prefer-dist --no-interaction
+ - run: vendor/bin/phpunit
diff --git a/README.md b/README.md
index b951479..1cc7f06 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,6 @@
[](https://travis-ci.org/Jord-JD/php-reading-time)
[](https://coveralls.io/github/Jord-JD/php-reading-time?branch=master)
-[](https://styleci.io/repos/128542116)
This PHP library lets you easily calculate the reading time for a piece of text.
diff --git a/composer.json b/composer.json
index eb14088..235a38e 100644
--- a/composer.json
+++ b/composer.json
@@ -13,7 +13,7 @@
"php": ">=7.1"
},
"require-dev": {
- "phpunit/phpunit": "^7.0 || ^8.0",
+ "phpunit/phpunit": "^7.0 || ^8.0 || ^9.6",
"php-coveralls/php-coveralls": "^2.0"
},
"autoload": {
@@ -31,7 +31,7 @@
},
"extra": {
"branch-alias": {
- "dev-master": "3.0-dev"
+ "dev-master": "4.0-dev"
}
}
}
diff --git a/phpunit.xml b/phpunit.xml
index 889606b..98d6d59 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -11,7 +11,6 @@
./tests/Unit
- ./tests/Integration
@@ -25,4 +24,4 @@
-
\ No newline at end of file
+
diff --git a/src/ReadingTime.php b/src/ReadingTime.php
index 88d41b9..7dc3ec4 100644
--- a/src/ReadingTime.php
+++ b/src/ReadingTime.php
@@ -2,30 +2,36 @@
namespace JordJD\ReadingTime;
+use InvalidArgumentException;
+
class ReadingTime
{
private $content;
private $wordsPerMinute = 200;
- public function __construct($content)
+ public function __construct(string $content)
{
$this->content = $content;
}
- public function setWordsPerMinute($wordsPerMinute)
+ public function setWordsPerMinute(int $wordsPerMinute): self
{
+ if ($wordsPerMinute <= 0) {
+ throw new InvalidArgumentException('Words per minute must be greater than zero.');
+ }
+
$this->wordsPerMinute = $wordsPerMinute;
return $this;
}
- public function minutes()
+ public function minutes(): int
{
- return str_word_count($this->content) / $this->wordsPerMinute;
+ return (int) ceil(str_word_count($this->content) / $this->wordsPerMinute);
}
- public function seconds()
+ public function seconds(): int
{
- return $this->minutes() * 60;
+ return (int) ceil((str_word_count($this->content) / $this->wordsPerMinute) * 60);
}
}
diff --git a/tests/Unit/ReadingTimeTest.php b/tests/Unit/ReadingTimeTest.php
index 6489522..8b07718 100644
--- a/tests/Unit/ReadingTimeTest.php
+++ b/tests/Unit/ReadingTimeTest.php
@@ -11,28 +11,28 @@ public function testLargeTextReadingTimeMinutes()
{
$text = file_get_contents(__DIR__.'/data/large.txt');
- $this->assertEquals(9.7699999999999996, (new ReadingTime($text))->minutes());
+ $this->assertSame(10, (new ReadingTime($text))->minutes());
}
public function testLargeTextReadingTimeSeconds()
{
$text = file_get_contents(__DIR__.'/data/large.txt');
- $this->assertEquals(586.19999999999993, (new ReadingTime($text))->seconds());
+ $this->assertSame(587, (new ReadingTime($text))->seconds());
}
public function testSmallTextReadingTimeMinutes()
{
$text = file_get_contents(__DIR__.'/data/small.txt');
- $this->assertEquals(0.40999999999999998, (new ReadingTime($text))->minutes());
+ $this->assertSame(1, (new ReadingTime($text))->minutes());
}
public function testSmallTextReadingTimeSeconds()
{
$text = file_get_contents(__DIR__.'/data/small.txt');
- $this->assertEquals(24.599999999999998, (new ReadingTime($text))->seconds());
+ $this->assertSame(25, (new ReadingTime($text))->seconds());
}
public function testSmallTextReadingTimeSecondsDifferentWordPerMinute()
@@ -40,6 +40,13 @@ public function testSmallTextReadingTimeSecondsDifferentWordPerMinute()
$wordPerMinute = 240;
$text = file_get_contents(__DIR__.'/data/small.txt');
- $this->assertEquals(20.5, (new ReadingTime($text))->setWordsPerMinute($wordPerMinute)->seconds());
+ $this->assertSame(21, (new ReadingTime($text))->setWordsPerMinute($wordPerMinute)->seconds());
+ }
+
+ public function testWordsPerMinuteMustBePositive()
+ {
+ $this->expectException(\InvalidArgumentException::class);
+
+ (new ReadingTime('Some text'))->setWordsPerMinute(0);
}
}