initial 2.0.0-a2 commit

This commit is contained in:
jerry
2020-09-29 15:07:43 +08:00
parent 1510e2f0d0
commit f91d24aaaa
59 changed files with 2271 additions and 1475 deletions

View File

@@ -0,0 +1,28 @@
<?php
use PHPUnit\Framework\TestCase;
use ZM\Console\Console;
use ZM\Requests\ZMRequest;
use ZM\Utils\CoroutinePool;
class CoroutinePoolTest extends TestCase
{
public function testStart() {
$this->assertTrue(true);
Console::init(4);
CoroutinePool::setSize("default", 2);
CoroutinePool::defaultSize(50);
for ($i = 0; $i < 59; ++$i) {
CoroutinePool::go(function () use ($i) {
//Console::debug("第 $i 个马上进入睡眠...");
ZMRequest::get("http://localhost:9002/test/ping");
Console::verbose(strval($i));
});
}
}
public function testA() {
$this->assertTrue(true);
}
}

21
test/LightCacheTest.php Normal file
View File

@@ -0,0 +1,21 @@
<?php
use PHPUnit\Framework\TestCase;
use ZM\Store\LightCache;
class LightCacheTest extends TestCase
{
public function testCache() {
LightCache::init([
"size" => 2048,
"max_strlen" => 4096,
"hash_conflict_proportion" => 0.6,
]);
//LightCache::set("bool", true);
$this->assertEquals(true, LightCache::set("2048", 123, 3));
$this->assertArrayHasKey("2048", LightCache::getAll());
sleep(3);
$this->assertArrayNotHasKey("2048", LightCache::getAll());
}
}

View File

@@ -0,0 +1,2 @@
<?php

View File

@@ -0,0 +1,73 @@
<?php
namespace ZMTest\PassedTest;
use Exception;
use Module\Example\Hello;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use ZM\Annotation\AnnotationParser;
use ZM\Annotation\Swoole\OnStart;
use ZM\Console\Console;
class AnnotationParserRegisterTest extends TestCase
{
private $parser;
public function setUp(): void {
if (!defined("WORKING_DIR"))
define("WORKING_DIR", realpath(__DIR__ . "/../../../"));
if (!defined("LOAD_MODE"))
define("LOAD_MODE", 0);
Console::init(2);
$this->parser = new AnnotationParser();
$this->parser->addRegisterPath(WORKING_DIR . "/src/Module/", "Module");
try {
$this->parser->registerMods();
} catch (ReflectionException $e) {
throw $e;
}
}
public function testAnnotation() {
ob_start();
$gen = $this->parser->generateAnnotationEvents();
$m = $gen[OnStart::class][0]->method;
$class = $gen[OnStart::class][0]->class;
$c = new $class();
try {
$c->$m();
} catch (Exception $e) {
}
$result = ob_get_clean();
echo $result;
$this->assertStringContainsString("我开始了!", $result);
}
public function testAnnotation2() {
foreach ($this->parser->generateAnnotationEvents() as $k => $v) {
foreach ($v as $vs) {
$this->assertTrue($vs->method === null || $vs->method != '');
$this->assertTrue(strlen($vs->class) > 0);
}
}
}
public function testAnnotationMap() {
$map = $this->parser->getMiddlewareMap();
$this->assertContainsEquals("timer", $map[Hello::class]["timer"]);
}
public function testMiddlewares() {
$wares = $this->parser->getMiddlewares();
$this->assertArrayHasKey("timer", $wares);
}
public function testReqMapping() {
$mapping = $this->parser->getReqMapping();
$this->assertEquals("index", $mapping["method"]);
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace ZMTest\Testing;
use Doctrine\Common\Annotations\AnnotationException;
use Module\Example\Hello;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use ZM\Annotation\AnnotationParser;
use ZM\Annotation\CQ\CQCommand;
use ZM\Console\Console;
use ZM\Event\EventDispatcher;
use ZM\Event\EventManager;
class EventDispatcherTest extends TestCase
{
public function testDispatch() {
Console::init(2);
if (!defined("WORKING_DIR"))
define("WORKING_DIR", realpath(__DIR__ . "/../../../"));
if (!defined("LOAD_MODE"))
define("LOAD_MODE", 0);
Console::init(2);
$parser = new AnnotationParser();
$parser->addRegisterPath(WORKING_DIR . "/src/Module/", "Module");
try {
$parser->registerMods();
} catch (ReflectionException $e) {
throw $e;
}
EventManager::loadEventByParser($parser);
$dispatcher = new EventDispatcher(CQCommand::class);
$dispatcher->setReturnFunction(function ($result) {
echo $result . PHP_EOL;
});
//$dispatcher->setRuleFunction(function ($v) { return $v->match == "qwe"; });
$dispatcher->setRuleFunction(function ($v) { return $v->match == "你好"; });
//$dispatcher->setRuleFunction(fn ($v) => $v->match == "qwe");
ob_start();
try {
$dispatcher->dispatchEvents();
} catch (AnnotationException $e) {
}
$r = ob_get_clean();
echo $r;
$this->assertStringContainsString("你好啊", $r);
}
}

56
test/test.php Normal file
View File

@@ -0,0 +1,56 @@
<?php
require __DIR__ . "/../vendor/autoload.php";
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$route = new Route('/foo', array('controller' => 'MyController'));
$routes = new RouteCollection();
$routes->add('route_name', $route);
$route = new Route(
'/archive/{month}', // path
array('controller' => 'showArchive', 'asd' => 'feswf'), // default values
array('month' => '[0-9]{4}-[0-9]{2}', 'subdomain' => 'www|m'), // requirements
array(), // options
'', // host
array(), // schemes
array() // methods
);
// ...
$routes->add('date', $route);
$route = new Route('/archive/test');
$routes->add('qwerty', $route);
$route = new Route('/');
$routes->add('root', $route);
$context = new RequestContext();
$matcher = new UrlMatcher($routes, $context);
//$parameters = $matcher->match('/test/foo');var_dump($parameters);
$parameters = $matcher->match('/archive/2012-01');
var_dump($parameters);
// array(
// 'controller' => 'showArchive',
// 'month' => '2012-01',
// 'subdomain' => 'www',
// '_route' => ...
// )
$parameters = $matcher->match('/');
var_dump($parameters);
$sub = new RouteCollection();