mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-26 01:55:34 +08:00
fix EventMapIterator bug and add tests
This commit is contained in:
@@ -5,7 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace ZM\Event;
|
namespace ZM\Event;
|
||||||
|
|
||||||
use Iterator;
|
use Iterator;
|
||||||
use ReturnTypeWillChange;
|
use ZM\Console\Console;
|
||||||
|
|
||||||
class EventMapIterator implements Iterator
|
class EventMapIterator implements Iterator
|
||||||
{
|
{
|
||||||
@@ -22,40 +22,54 @@ class EventMapIterator implements Iterator
|
|||||||
$this->class = $class;
|
$this->class = $class;
|
||||||
$this->method = $method;
|
$this->method = $method;
|
||||||
$this->event_name = $event_name;
|
$this->event_name = $event_name;
|
||||||
$this->nextToValid();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function current()
|
public function current()
|
||||||
{
|
{
|
||||||
|
Console::debug('从 [' . $this->offset . '] 开始获取');
|
||||||
return EventManager::$event_map[$this->class][$this->method][$this->offset];
|
return EventManager::$event_map[$this->class][$this->method][$this->offset];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function next(): void
|
public function next(): void
|
||||||
{
|
{
|
||||||
++$this->offset;
|
Console::debug('下一个offset为 [' . ++$this->offset . ']');
|
||||||
$this->nextToValid();
|
$this->nextToValid();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[ReturnTypeWillChange]
|
|
||||||
public function key()
|
public function key()
|
||||||
{
|
{
|
||||||
return $this->offset;
|
Console::debug('返回key:' . $this->offset);
|
||||||
|
return isset(EventManager::$event_map[$this->class][$this->method][$this->offset]) ? $this->offset : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function valid(): bool
|
public function valid($s = false): bool
|
||||||
{
|
{
|
||||||
return isset(EventManager::$event_map[$this->class][$this->method][$this->offset]);
|
Console::debug(
|
||||||
|
"[{$this->offset}] " .
|
||||||
|
($s ? 'valid' : '') . '存在:' .
|
||||||
|
(!isset(EventManager::$event_map[$this->class][$this->method][$this->offset]) ? Console::setColor('false', 'red') : ('true' .
|
||||||
|
(is_a(EventManager::$event_map[$this->class][$this->method][$this->offset], $this->event_name, true) ? ',是目标对象' : ',不是目标对象')))
|
||||||
|
);
|
||||||
|
return
|
||||||
|
isset(EventManager::$event_map[$this->class][$this->method][$this->offset])
|
||||||
|
&& is_a(EventManager::$event_map[$this->class][$this->method][$this->offset], $this->event_name, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rewind(): void
|
public function rewind(): void
|
||||||
{
|
{
|
||||||
|
Console::debug('回到0');
|
||||||
$this->offset = 0;
|
$this->offset = 0;
|
||||||
|
$this->nextToValid();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function nextToValid()
|
private function nextToValid()
|
||||||
{
|
{
|
||||||
while ($this->valid() && !is_a($this->current(), $this->event_name, true)) {
|
while (
|
||||||
|
isset(EventManager::$event_map[$this->class][$this->method][$this->offset])
|
||||||
|
&& !is_a(EventManager::$event_map[$this->class][$this->method][$this->offset], $this->event_name, true)
|
||||||
|
) {
|
||||||
++$this->offset;
|
++$this->offset;
|
||||||
}
|
}
|
||||||
|
Console::debug('内部偏移offset为 [' . $this->offset . ']');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,11 +15,11 @@ use ZM\ConnectionManager\ManagerGM;
|
|||||||
use ZM\Console\Console;
|
use ZM\Console\Console;
|
||||||
use ZM\Container\Container;
|
use ZM\Container\Container;
|
||||||
use ZM\Container\ContainerInterface;
|
use ZM\Container\ContainerInterface;
|
||||||
|
use ZM\Container\EntryResolutionException;
|
||||||
use ZM\Context\Context;
|
use ZM\Context\Context;
|
||||||
use ZM\Context\ContextInterface;
|
use ZM\Context\ContextInterface;
|
||||||
use ZM\Event\EventManager;
|
use ZM\Event\EventManager;
|
||||||
use ZM\Exception\RobotNotFoundException;
|
use ZM\Exception\RobotNotFoundException;
|
||||||
use ZM\Exception\ZMKnownException;
|
|
||||||
use ZM\Framework;
|
use ZM\Framework;
|
||||||
use ZM\Store\LightCacheInside;
|
use ZM\Store\LightCacheInside;
|
||||||
use ZM\Store\ZMAtomic;
|
use ZM\Store\ZMAtomic;
|
||||||
@@ -183,8 +183,7 @@ function match_args(string $pattern, string $subject)
|
|||||||
/**
|
/**
|
||||||
* 判断当前连接类型是否为传入的$type
|
* 判断当前连接类型是否为传入的$type
|
||||||
*
|
*
|
||||||
* @param string $type 连接类型
|
* @param string $type 连接类型
|
||||||
* @throws ZMKnownException
|
|
||||||
*/
|
*/
|
||||||
function current_connection_is(string $type): bool
|
function current_connection_is(string $type): bool
|
||||||
{
|
{
|
||||||
@@ -737,7 +736,8 @@ function container(): ContainerInterface
|
|||||||
* 解析类实例(使用容器)
|
* 解析类实例(使用容器)
|
||||||
*
|
*
|
||||||
* @template T
|
* @template T
|
||||||
* @param class-string<T> $abstract
|
* @param class-string<T> $abstract
|
||||||
|
* @throws EntryResolutionException
|
||||||
* @return Closure|mixed|T
|
* @return Closure|mixed|T
|
||||||
*/
|
*/
|
||||||
function resolve(string $abstract, array $parameters = [])
|
function resolve(string $abstract, array $parameters = [])
|
||||||
@@ -750,6 +750,7 @@ function resolve(string $abstract, array $parameters = [])
|
|||||||
*
|
*
|
||||||
* @template T
|
* @template T
|
||||||
* @param null|class-string<T> $abstract
|
* @param null|class-string<T> $abstract
|
||||||
|
* @throws EntryResolutionException
|
||||||
* @return Closure|ContainerInterface|mixed|T
|
* @return Closure|ContainerInterface|mixed|T
|
||||||
*/
|
*/
|
||||||
function app(string $abstract = null, array $parameters = [])
|
function app(string $abstract = null, array $parameters = [])
|
||||||
|
|||||||
33
tests/ZM/Event/EventMapIteratorTest.php
Normal file
33
tests/ZM/Event/EventMapIteratorTest.php
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\ZM\Event;
|
||||||
|
|
||||||
|
use Module\Example\Hello;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use ZM\Annotation\CQ\CommandArgument;
|
||||||
|
use ZM\Console\Console;
|
||||||
|
use ZM\Event\EventMapIterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
class EventMapIteratorTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testIterator(): void
|
||||||
|
{
|
||||||
|
Console::setLevel(4);
|
||||||
|
$iterator = new EventMapIterator(Hello::class, 'randNum', CommandArgument::class);
|
||||||
|
$arr = iterator_to_array($iterator);
|
||||||
|
$this->assertArrayNotHasKey(0, $arr);
|
||||||
|
$this->assertArrayNotHasKey(1, $arr);
|
||||||
|
$this->assertArrayHasKey(2, $arr);
|
||||||
|
$this->assertArrayHasKey(3, $arr);
|
||||||
|
$this->assertInstanceOf(CommandArgument::class, $arr[2]);
|
||||||
|
$this->assertInstanceOf(CommandArgument::class, $arr[3]);
|
||||||
|
$iterator = new EventMapIterator(Hello::class, 'closeUnknownConn', CommandArgument::class);
|
||||||
|
$ls = iterator_to_array($iterator);
|
||||||
|
$this->assertCount(0, $ls);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user