add chain and stopwatch helper functions (#99)

* add chain helper function

* add stopwatch helper function
This commit is contained in:
sunxyw
2022-04-15 23:10:29 +08:00
committed by GitHub
parent ea64b40011
commit 97face2406
2 changed files with 138 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Tests\ZM;
use PHPUnit\Framework\TestCase;
/**
* @internal
*/
class GlobalFunctionsTest extends TestCase
{
public function testChain(): void
{
$mock = $this->getMockBuilder(\stdClass::class)
->addMethods(['foo', 'bar', 'baz'])
->getMock();
$mock->expects($this->once())
->method('foo')
->willReturn('foo');
$mock->expects($this->once())
->method('bar')
->with('foo')
->willReturn('bar');
$mock->expects($this->once())
->method('baz')
->with('bar')
->willReturn('baz');
$result = chain($mock)->foo()->bar(CARRY)->baz(CARRY);
$this->assertEquals('baz', $result);
}
public function testStopwatch(): void
{
$time = stopwatch(static function () {
usleep(10000);
});
$this->assertLessThan(0.1, $time);
}
}