From 338fd17da0e358bc1c97738cf44a497fa8a526f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sat, 23 Jan 2021 11:05:40 +0100 Subject: [PATCH] Enhancement: Add example tests --- .github/workflows/integrate.yaml | 3 ++ Makefile | 3 +- test/Example/ExampleTest.php | 83 ++++++++++++++++++++++++++++++++ test/Example/phpunit.xml | 30 ++++++++++++ test/Fixture/Sleeper.php | 49 +++++++++++++++++++ 5 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 test/Example/ExampleTest.php create mode 100644 test/Example/phpunit.xml create mode 100644 test/Fixture/Sleeper.php diff --git a/.github/workflows/integrate.yaml b/.github/workflows/integrate.yaml index 6171d65f..25cb55f8 100644 --- a/.github/workflows/integrate.yaml +++ b/.github/workflows/integrate.yaml @@ -236,6 +236,9 @@ jobs: - name: "Run integration tests with phpunit/phpunit" run: "vendor/bin/phpunit --configuration=test/Integration/phpunit.xml" + - name: "Run example tests with phpunit/phpunit" + run: "vendor/bin/phpunit --configuration=test/Example/phpunit.xml" + code-coverage: name: "Code Coverage" diff --git a/Makefile b/Makefile index 54b55154..d7e3a427 100644 --- a/Makefile +++ b/Makefile @@ -42,11 +42,12 @@ static-code-analysis-baseline: vendor ## Generates a baseline for static code an vendor/bin/psalm --config=psalm.xml --set-baseline=psalm-baseline.xml .PHONY: tests -tests: vendor ## Runs auto-review, unit, and integration tests with phpunit/phpunit +tests: vendor ## Runs auto-review, unit, integration, and example tests with phpunit/phpunit mkdir -p .build/phpunit vendor/bin/phpunit --configuration=test/AutoReview/phpunit.xml vendor/bin/phpunit --configuration=test/Unit/phpunit.xml vendor/bin/phpunit --configuration=test/Integration/phpunit.xml + vendor/bin/phpunit --configuration=test/Example/phpunit.xml vendor: composer.json composer.lock composer validate diff --git a/test/Example/ExampleTest.php b/test/Example/ExampleTest.php new file mode 100644 index 00000000..57c4db38 --- /dev/null +++ b/test/Example/ExampleTest.php @@ -0,0 +1,83 @@ +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + public function testSleeperSleepsOneQuarterOfASecond(): void + { + $milliseconds = 250; + + $sleeper = Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + public function testSleeperSleepsHalfASeconds(): void + { + $milliseconds = 500; + + $sleeper = Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + public function testSleeperSleepsThreeQuartersOfASecond(): void + { + $milliseconds = 750; + + $sleeper = Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + public function testSleeperSleepsOneSecond(): void + { + $milliseconds = 1000; + + $sleeper = Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } +} diff --git a/test/Example/phpunit.xml b/test/Example/phpunit.xml new file mode 100644 index 00000000..7e7abfe7 --- /dev/null +++ b/test/Example/phpunit.xml @@ -0,0 +1,30 @@ + + + + . + + + diff --git a/test/Fixture/Sleeper.php b/test/Fixture/Sleeper.php new file mode 100644 index 00000000..1f9b445f --- /dev/null +++ b/test/Fixture/Sleeper.php @@ -0,0 +1,49 @@ +milliseconds = $seconds; + } + + /** + * @throws \InvalidArgumentException + */ + public static function fromMilliseconds(int $milliseconds): self + { + if (0 > $milliseconds) { + throw new \InvalidArgumentException(\sprintf( + 'Value for milliseconds should be greater than or equal to 0, but %d is not.', + $milliseconds + )); + } + + return new self($milliseconds); + } + + public function milliseconds(): int + { + return $this->milliseconds; + } + + public function sleep(): void + { + \usleep($this->milliseconds * 1000); + } +}