From 4533291324cb9aaa5398861341362361b123f14b Mon Sep 17 00:00:00 2001 From: james Date: Tue, 3 Jun 2025 14:47:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20tests/CalculatorTest.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/CalculatorTest.php | 134 +++++++++++---------------------------- 1 file changed, 37 insertions(+), 97 deletions(-) diff --git a/tests/CalculatorTest.php b/tests/CalculatorTest.php index 0dae905..307046c 100644 --- a/tests/CalculatorTest.php +++ b/tests/CalculatorTest.php @@ -1,110 +1,50 @@ toBe($class::class); + protected function setUp(): void + { + $this->handler = new MyWebSocketHandler(); + } - //restore old request class - Http::requestClass($oldRequestClass); -}); + public function testOnConnect() + { + $connection = $this->createMock(TcpConnection::class); + $worker = $this->createMock(Worker::class); -it('tests ::input', function () { - //test 413 payload too large - testWithConnectionClose(function (TcpConnection $tcpConnection) { - expect(Http::input(str_repeat('jhdxr', 3333), $tcpConnection)) - ->toBe(0); - }, '413 Payload Too Large'); + // 使用 ob_start + ob_get_clean 捕获输出 + ob_start(); + $this->handler->onConnect($connection); + $output = ob_get_clean(); - //example request from ChatGPT :) - $buffer = "POST /path/to/resource HTTP/1.1\r\n" . - "Host: example.com\r\n" . - "Content-Type: application/json\r\n" . - "Content-Length: 27\r\n" . - "\r\n" . - '{"key": "value", "foo": "bar"}'; + $this->assertStringContainsString('New connection', $output); + } - //unrecognized method - testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) { - expect(Http::input(str_replace('POST', 'MIAOWU', $buffer), $tcpConnection)) - ->toBe(0); - }, '400 Bad Request'); + public function testOnMessage() + { + $connection = $this->createMock(TcpConnection::class); + $connection->expects($this->once()) + ->method('send') + ->with($this->equalTo('Hello world')); - //content-length exceeds connection max package size - testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) { - $tcpConnection->maxPackageSize = 10; - expect(Http::input($buffer, $tcpConnection)) - ->toBe(0); - }, '413 Payload Too Large'); -}); + $this->handler->onMessage($connection, 'world'); + } -it('tests ::encode for non-object response', function () { - /** @var TcpConnection $tcpConnection */ - $tcpConnection = Mockery::mock(TcpConnection::class); - $tcpConnection->headers = [ - 'foo' => 'bar', - 'jhdxr' => ['a', 'b'], - ]; - $extHeader = "foo: bar\r\n" . - "jhdxr: a\r\n" . - "jhdxr: b\r\n"; + public function testOnClose() + { + $connection = $this->createMock(TcpConnection::class); - expect(Http::encode('xiami', $tcpConnection)) - ->toBe("HTTP/1.1 200 OK\r\n" . - "Server: workerman\r\n" . - "{$extHeader}Connection: keep-alive\r\n" . - "Content-Type: text/html;charset=utf-8\r\n" . - "Content-Length: 5\r\n\r\nxiami"); -}); + ob_start(); + $this->handler->onClose($connection); + $output = ob_get_clean(); -it('tests ::encode for ' . Response::class, function () { - /** @var TcpConnection $tcpConnection */ - $tcpConnection = Mockery::mock(TcpConnection::class); - $tcpConnection->headers = [ - 'foo' => 'bar', - 'jhdxr' => ['a', 'b'], - ]; - $extHeader = "foo: bar\r\n" . - "jhdxr: a\r\n" . - "jhdxr: b\r\n"; - - $response = new Response(body: 'xiami'); - - expect(Http::encode($response, $tcpConnection)) - ->toBe("HTTP/1.1 200 OK\r\n" . - "Server: workerman\r\n" . - "{$extHeader}Connection: keep-alive\r\n" . - "Content-Type: text/html;charset=utf-8\r\n" . - "Content-Length: 5\r\n\r\nxiami"); -}); - -it('tests ::decode', function () { - /** @var TcpConnection $tcpConnection */ - $tcpConnection = Mockery::mock(TcpConnection::class); - - //example request from ChatGPT :) - $buffer = "POST /path/to/resource HTTP/1.1\r\n" . - "Host: example.com\r\n" . - "Content-Type: application/json\r\n" . - "Content-Length: 27\r\n" . - "\r\n" . - '{"key": "value", "foo": "bar"}'; - - $value = expect(Http::decode($buffer, $tcpConnection)) - ->toBeInstanceOf(Request::class) - ->value; - - //test cache - expect($value == Http::decode($buffer, $tcpConnection)) - ->toBeTrue(); -}); \ No newline at end of file + $this->assertStringContainsString('Connection closed', $output); + } +} \ No newline at end of file