-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
188 additions
and
2 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
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,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Workbunny\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ReflectionClass; | ||
use Webman\Http\Request; | ||
use Workbunny\Tests\mock\TestHandler; | ||
use Workbunny\WebmanCoroutine\CoroutineServerInterface; | ||
use Workbunny\WebmanCoroutine\CoroutineWorkerInterface; | ||
use Workbunny\WebmanCoroutine\Factory; | ||
use Workbunny\WebmanCoroutine\Handlers\DefaultHandler; | ||
use Workbunny\WebmanCoroutine\Handlers\RippleHandler; | ||
use Workbunny\WebmanCoroutine\Handlers\SwooleHandler; | ||
use Workbunny\WebmanCoroutine\Handlers\SwooleWorkerman5Handler; | ||
use Workbunny\WebmanCoroutine\Handlers\SwowHandler; | ||
use Workbunny\WebmanCoroutine\Handlers\SwowWorkerman5Handler; | ||
use Workerman\Connection\ConnectionInterface; | ||
use Workerman\Worker; | ||
|
||
class FactoryTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
// 重置静态属性 | ||
$reflection = new ReflectionClass(Factory::class); | ||
$property = $reflection->getProperty('_handlers'); | ||
$property->setAccessible(true); | ||
$property->setValue($reflection, [ | ||
Factory::WORKERMAN_SWOW => SwowWorkerman5Handler::class, | ||
Factory::WORKBUNNY_SWOW => SwowHandler::class, | ||
Factory::WORKERMAN_SWOOLE => SwooleWorkerman5Handler::class, | ||
Factory::WORKBUNNY_SWOOLE => SwooleHandler::class, | ||
Factory::RIPPLE_FIBER => RippleHandler::class, | ||
Factory::WORKERMAN_DEFAULT => DefaultHandler::class, | ||
]); | ||
} | ||
|
||
public function testRegister() | ||
{ | ||
$result = Factory::register('NewEventLoop', TestHandler::class); | ||
$this->assertTrue($result); | ||
|
||
$reflection = new ReflectionClass(Factory::class); | ||
$property = $reflection->getProperty('_handlers'); | ||
$property->setAccessible(true); | ||
$handlers = $property->getValue(); | ||
$this->assertEquals(TestHandler::class, $handlers['NewEventLoop']); | ||
} | ||
|
||
public function testRegisterExistingHandler() | ||
{ | ||
Factory::register('ExistingEventLoop', TestHandler::class); | ||
$result = Factory::register('ExistingEventLoop', TestHandler::class); | ||
$this->assertNull($result); | ||
} | ||
|
||
public function testUnregister() | ||
{ | ||
Factory::register('EventLoopToRemove', TestHandler::class); | ||
$result = Factory::unregister('EventLoopToRemove'); | ||
$this->assertTrue($result); | ||
|
||
$reflection = new ReflectionClass(Factory::class); | ||
$property = $reflection->getProperty('_handlers'); | ||
$property->setAccessible(true); | ||
$handlers = $property->getValue(); | ||
$this->assertArrayNotHasKey('EventLoopToRemove', $handlers); | ||
} | ||
|
||
public function testRun() | ||
{ | ||
$app = $this->createMock(CoroutineServerInterface::class); | ||
$connection = $this->createMock(ConnectionInterface::class); | ||
$request = $this->createMock(Request::class); | ||
|
||
Factory::register('NewEventLoop', TestHandler::class); | ||
Factory::run($app, $connection, $request, 'NewEventLoop'); | ||
|
||
$result = Factory::run($app, $connection, $request); | ||
$this->assertEquals('response', $result); | ||
} | ||
|
||
public function testStart() | ||
{ | ||
$app = $this->createMock(CoroutineWorkerInterface::class); | ||
$worker = $this->createMock(Worker::class); | ||
|
||
Factory::register('NewEventLoop', TestHandler::class); | ||
Factory::start($app, $worker, 'NewEventLoop'); | ||
|
||
$result = Factory::start($app, $worker); | ||
$this->assertEquals('response', $result); | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Workbunny\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Workbunny\Tests\mock\TestHandler; | ||
use Workbunny\WebmanCoroutine\Factory; | ||
use function Workbunny\WebmanCoroutine\event_loop; | ||
use function Workbunny\WebmanCoroutine\package_installed; | ||
class HelpersTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
require_once __DIR__ . '/../src/helpers.php'; | ||
} | ||
|
||
public function testEventLoopWithExpectedClass() | ||
{ | ||
Factory::register('SomeEventLoopClass', TestHandler::class); | ||
$expectedClass = 'SomeEventLoopClass'; | ||
$result = event_loop($expectedClass); | ||
$this->assertEquals($expectedClass, $result); | ||
Factory::unregister('SomeEventLoopClass'); | ||
} | ||
|
||
public function testEventLoopWithDefaultClass() | ||
{ | ||
// env auto return | ||
$result = event_loop(); | ||
$this->assertEquals(Factory::WORKERMAN_DEFAULT, $result); | ||
// not found class | ||
$result = event_loop('SomeEventLoopClass'); | ||
$this->assertEquals(Factory::WORKERMAN_DEFAULT, $result); | ||
} | ||
|
||
public function testPackageInstalled() | ||
{ | ||
$packageName = 'webman/console'; | ||
$this->assertTrue(package_installed($packageName)); | ||
$packageName = 'nonexistent/package'; | ||
$this->assertFalse(package_installed($packageName)); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Workbunny\Tests\mock; | ||
|
||
use Workbunny\WebmanCoroutine\Handlers\HandlerInterface; | ||
use Workbunny\WebmanCoroutine\CoroutineWorkerInterface; | ||
use Workbunny\WebmanCoroutine\CoroutineServerInterface; | ||
|
||
class TestHandler implements HandlerInterface | ||
{ | ||
public static function isAvailable(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public static function onMessage(CoroutineServerInterface $app, mixed $connection, mixed $request): mixed | ||
{ | ||
return 'response'; | ||
} | ||
|
||
public static function onWorkerStart(CoroutineWorkerInterface $app, mixed $worker): mixed | ||
{ | ||
return 'response'; | ||
} | ||
|
||
public static function coroutineCreate(\Closure $function, ?string $waitGroupId = null): mixed | ||
{ | ||
return $function(); | ||
} | ||
|
||
public static function waitGroupCreate(): string | ||
{ | ||
return 'waitGroupId'; | ||
} | ||
|
||
public static function waitGroupWait(string $waitGroupId, int $timeout = -1): void | ||
{ | ||
// Do nothing | ||
} | ||
} |