Skip to content

Commit

Permalink
Avoid global fallback for mocking built in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
duncan3dc committed Oct 30, 2024
1 parent 7b2c188 commit 9854372
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 151 deletions.
8 changes: 5 additions & 3 deletions tests/TerminalObject/Basic/DumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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');
Expand Down
8 changes: 0 additions & 8 deletions tests/TerminalObject/Basic/VarDumpMock.php

This file was deleted.

10 changes: 0 additions & 10 deletions tests/TerminalObject/Helper/SleeperGlobalMock.php

This file was deleted.

62 changes: 28 additions & 34 deletions tests/TerminalObject/Helper/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
25 changes: 0 additions & 25 deletions tests/Util/Reader/StdinGlobalMock.php

This file was deleted.

71 changes: 0 additions & 71 deletions tests/Util/Reader/StdinTest.php

This file was deleted.

0 comments on commit 9854372

Please sign in to comment.