From 9854372027166946619de7f1c46ee33c95b9a870 Mon Sep 17 00:00:00 2001 From: Craig Duncan Date: Sat, 10 Feb 2018 20:42:59 +0000 Subject: [PATCH] Avoid global fallback for mocking built in functions --- tests/TerminalObject/Basic/DumpTest.php | 8 ++- tests/TerminalObject/Basic/VarDumpMock.php | 8 --- .../Helper/SleeperGlobalMock.php | 10 --- tests/TerminalObject/Helper/SleeperTest.php | 62 ++++++++-------- tests/Util/Reader/StdinGlobalMock.php | 25 ------- tests/Util/Reader/StdinTest.php | 71 ------------------- 6 files changed, 33 insertions(+), 151 deletions(-) delete mode 100644 tests/TerminalObject/Basic/VarDumpMock.php delete mode 100644 tests/TerminalObject/Helper/SleeperGlobalMock.php delete mode 100644 tests/Util/Reader/StdinGlobalMock.php delete mode 100644 tests/Util/Reader/StdinTest.php diff --git a/tests/TerminalObject/Basic/DumpTest.php b/tests/TerminalObject/Basic/DumpTest.php index d386c2a3..fb873e07 100644 --- a/tests/TerminalObject/Basic/DumpTest.php +++ b/tests/TerminalObject/Basic/DumpTest.php @@ -3,8 +3,7 @@ namespace League\CLImate\Tests\TerminalObject\Basic; use League\CLImate\Tests\TestBase; - -require_once 'VarDumpMock.php'; +use Mockery; class DumpTest extends TestBase { @@ -14,7 +13,10 @@ class DumpTest extends TestBase */ public function it_can_dump_a_variable() { - $this->shouldWrite("\e[mDUMPED: This thing\e[0m"); + $this->output->shouldReceive("write")->once()->with(Mockery::on(function ($content) { + return (bool) strpos($content, "string(10) \"This thing\""); + })); + $this->shouldHavePersisted(); $this->cli->dump('This thing'); diff --git a/tests/TerminalObject/Basic/VarDumpMock.php b/tests/TerminalObject/Basic/VarDumpMock.php deleted file mode 100644 index 7f3c9ff2..00000000 --- a/tests/TerminalObject/Basic/VarDumpMock.php +++ /dev/null @@ -1,8 +0,0 @@ -usleep($microseconds); -} diff --git a/tests/TerminalObject/Helper/SleeperTest.php b/tests/TerminalObject/Helper/SleeperTest.php index 9265b027..83ae5574 100644 --- a/tests/TerminalObject/Helper/SleeperTest.php +++ b/tests/TerminalObject/Helper/SleeperTest.php @@ -3,61 +3,55 @@ namespace League\CLImate\Tests\TerminalObject\Helper; use League\CLImate\TerminalObject\Helper\Sleeper; -use League\CLImate\Tests\TestBase; +use PHPUnit\Framework\TestCase; -require_once 'SleeperGlobalMock.php'; - -class SleeperTest extends TestBase +class SleeperTest extends TestCase { - /** - * @test - * @doesNotPerformAssertions - */ - public function it_can_slow_down_the_sleeper_speed() + public function setUp(): void { - $sleeper = new Sleeper(); + if (\PHP_OS_FAMILY === "Windows") { + $this->markTestSkipped(); + } + } - $sleeper->speed(50); - self::$functions->shouldReceive('usleep') - ->once() - ->with(100000); + private function assertSleep($expected, $speed) + { + $sleeper = new Sleeper(); + + $sleeper->speed($speed); + $start = microtime(true); $sleeper->sleep(); + $result = (microtime(true) - $start) * 1000000; + + $this->assertGreaterThan($expected * 0.9, $result); + $this->assertLessThan($expected * 1.1, $result); } + /** * @test - * @doesNotPerformAssertions */ - public function it_can_speed_up_the_sleeper_speed() + public function it_can_slow_down_the_sleeper_speed() { - $sleeper = new Sleeper(); - - $sleeper->speed(200); - - self::$functions->shouldReceive('usleep') - ->once() - ->with(25000); + $this->assertSleep(100000, 50); + } - $sleeper->sleep(); + /** + * @test + */ + public function it_can_speed_up_the_sleeper_speed() + { + $this->assertSleep(25000, 200); } /** * @test - * @doesNotPerformAssertions */ public function it_will_ignore_zero_percentages() { - $sleeper = new Sleeper(); - - $sleeper->speed(0); - - self::$functions->shouldReceive('usleep') - ->once() - ->with(50000); - - $sleeper->sleep(); + $this->assertSleep(50000, 0); } diff --git a/tests/Util/Reader/StdinGlobalMock.php b/tests/Util/Reader/StdinGlobalMock.php deleted file mode 100644 index a1686ad3..00000000 --- a/tests/Util/Reader/StdinGlobalMock.php +++ /dev/null @@ -1,25 +0,0 @@ -fopen($resource, $mode); -} - -function fgets($resource, $length) -{ - return TestBase::$functions->fgets($resource, $length); -} - -function fread($resource, $length) -{ - return TestBase::$functions->fread($resource, $length); -} - -function stream_get_contents($resource) -{ - return TestBase::$functions->stream_get_contents($resource); -} diff --git a/tests/Util/Reader/StdinTest.php b/tests/Util/Reader/StdinTest.php deleted file mode 100644 index f4b1d1d1..00000000 --- a/tests/Util/Reader/StdinTest.php +++ /dev/null @@ -1,71 +0,0 @@ -shouldReceive('fopen') - ->once() - ->with('php://stdin', 'r') - ->andReturn('resource'); - - self::$functions->shouldReceive('fgets') - ->once() - ->with('resource', 1024) - ->andReturn('I hear you loud and clear.'); - - $response = $stdin->line(); - - $this->assertSame('I hear you loud and clear.', $response); - } - - /** @test */ - public function it_can_read_multiple_lines_from_stdin() - { - $stdin = new Stdin(); - - self::$functions->shouldReceive('fopen') - ->once() - ->with('php://stdin', 'r') - ->andReturn('resource'); - - self::$functions->shouldReceive('stream_get_contents') - ->once() - ->with('resource') - ->andReturn('I hear you loud and clear.'); - - $response = $stdin->multiLine(); - - $this->assertSame('I hear you loud and clear.', $response); - } - - /** @test */ - public function it_can_several_characters_from_stdin() - { - $stdin = new Stdin(); - - self::$functions->shouldReceive('fopen') - ->once() - ->with('php://stdin', 'r') - ->andReturn('resource'); - - self::$functions->shouldReceive('fread') - ->once() - ->with('resource', 6) - ->andReturn('I hear'); - - $response = $stdin->char(6); - - $this->assertSame('I hear', $response); - } -}