Skip to content

Commit

Permalink
Merge pull request #13 from ingenerator/1.0/allow-assert-not-slept
Browse files Browse the repository at this point in the history
Allow asserting that the StoppedMockClock never slept
  • Loading branch information
acoulton authored Jul 9, 2019
2 parents f9a3c31 + d12bf93 commit ee96205
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
### Unreleased

### v1.1.0 (2019-07-09)

* Allow asserting that the StoppedMockClock never slept

### v1.0.0 (2019-04-03)

* First major release from 0.2.0
Expand Down
5 changes: 5 additions & 0 deletions src/DateTime/Clock/StoppedMockClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,9 @@ public function assertSlept(array $expected, $msg = '')
Assert::assertSame($expected, $this->sleeps, $msg);
}

public function assertNeverSlept($msg = '')
{
Assert::assertNull($this->sleeps, $msg);
}

}
25 changes: 17 additions & 8 deletions test/unit/DateTime/Clock/StoppedMockClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace test\unit\Ingenerator\PHPUtils\unit\DateTime\Clock;

use Ingenerator\PHPUtils\DateTime\Clock\StoppedMockClock;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;

class StoppedMockClockTest extends TestCase
Expand Down Expand Up @@ -147,14 +148,8 @@ public function test_assert_slept_fails_if_not_slept_for_expected_intervals($cal
{
$clock = StoppedMockClock::atNow();
$callback($clock);
$e = NULL;
try {
$clock->assertSlept($expected, $msg);
} catch (\Exception $e) {
}
$this->assertInstanceOf(\Exception::class, $e, 'Should have thrown');
// Do it like this to make it type-safe for old and new phpunit
$this->assertContains('ExpectationFailedException', \get_class($e), 'Should be assertion exception');
$this->expectException(ExpectationFailedException::class);
$clock->assertSlept($expected, $msg);
}

public function test_assert_slept_passes_if_slept_for_expected_intervals()
Expand All @@ -167,4 +162,18 @@ public function test_assert_slept_passes_if_slept_for_expected_intervals()
$clock->assertSlept([50000, 20000, 15000])
);
}

public function test_assert_never_slept_passes_if_never_slept()
{
$clock = StoppedMockClock::atNow();
$this->assertNull($clock->assertNeverSlept());
}

public function test_assert_never_slept_if_ever_slept()
{
$clock = StoppedMockClock::atNow();
$clock->usleep(500);
$this->expectException(ExpectationFailedException::class);
$clock->assertNeverSlept();
}
}

0 comments on commit ee96205

Please sign in to comment.