Skip to content

Commit

Permalink
feat: unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Sep 28, 2024
1 parent 4ffaedd commit e9613ec
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class Factory
self::WORKERMAN_SWOOLE => SwooleWorkerman5Handler::class,
self::WORKBUNNY_SWOOLE => SwooleHandler::class,
self::RIPPLE_FIBER => RippleHandler::class,
self::WORKERMAN_DEFAULT => DefaultHandler::class,
];

/**
Expand Down Expand Up @@ -126,7 +125,9 @@ public static function get(string $eventLoopClass, bool $available = false, bool
if ($available) {
// 当$returnEventLoopClass=true时,返回的是eventloop classname而不是handler classname
$handlerClass = $handlerClass::isAvailable()
? ($returnEventLoopClass ? $eventLoopClass : $handlerClass)
? ($returnEventLoopClass
? (isset(self::$_handlers[$eventLoopClass])) ? $eventLoopClass : self::WORKERMAN_DEFAULT
: $handlerClass)
: ($returnEventLoopClass ? self::WORKERMAN_DEFAULT : DefaultHandler::class);
}

Expand Down
97 changes: 97 additions & 0 deletions tests/FactoryTest.php
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);
}
}
46 changes: 46 additions & 0 deletions tests/HelpersTest.php
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));
}
}
42 changes: 42 additions & 0 deletions tests/mock/TestHandler.php
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
}
}

0 comments on commit e9613ec

Please sign in to comment.