-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Core): Add contract for sleep service with no sleep implementati…
…on in tests - When running in tests, SleepService is switched with NoSleepService - Use NoSleepService in your unit tests
- Loading branch information
Showing
10 changed files
with
151 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Core\Contracts; | ||
|
||
interface SleepServiceContract | ||
{ | ||
public function sleep(int $milliSeconds): void; | ||
|
||
public function sleepRandom(int $fromMilliSeconds, int $toMilliSeconds): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Core\Services; | ||
|
||
use LaraStrict\Core\Contracts\SleepServiceContract; | ||
|
||
final class NoSleepService implements SleepServiceContract | ||
{ | ||
public function sleep(int $milliSeconds): void | ||
{ | ||
} | ||
|
||
public function sleepRandom(int $fromMilliSeconds, int $toMilliSeconds): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Core\Services; | ||
|
||
use Closure; | ||
use LaraStrict\Core\Contracts\SleepServiceContract; | ||
use LaraStrict\Core\Services\SleepService; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SleepServiceTest extends TestCase | ||
{ | ||
private const MinMilliseconds = 100; | ||
|
||
public function testSleep(): void | ||
{ | ||
$this->assertIsInRange( | ||
static fn (SleepServiceContract $service) => $service->sleep(milliSeconds: self::MinMilliseconds) | ||
); | ||
} | ||
|
||
public function testSleepRandom(): void | ||
{ | ||
$this->assertIsInRange( | ||
static fn (SleepServiceContract $service) => $service->sleepRandom( | ||
fromMilliSeconds: self::MinMilliseconds, | ||
toMilliSeconds: self::MinMilliseconds + 10 | ||
) | ||
); | ||
} | ||
|
||
protected function assertIsInRange(Closure $run): void | ||
{ | ||
$time = microtime(true); | ||
|
||
$service = new SleepService(); | ||
$run($service); | ||
|
||
$diff = microtime(true) - $time; | ||
$this->assertTrue($diff > 0.08); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
tests/Feature/Testing/Core/Services/NoSleepServiceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Testing\Core\Services; | ||
|
||
use LaraStrict\Testing\Core\Services\NoSleepService; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class NoSleepServiceTest extends TestCase | ||
{ | ||
public function testSleepDoesNothing(): void | ||
{ | ||
$time = microtime(true); | ||
$noSleep = new NoSleepService(); | ||
|
||
$noSleep->sleep(10000); | ||
$noSleep->sleepRandom(10000, 20000); | ||
|
||
$this->assertTrue(microtime(true) - $time < 0.08); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters