Explorar o código

Merge pull request #919 from Chance-fyi/unit-test

Start the socket server in beforeAll
walkor %!s(int64=2) %!d(string=hai) anos
pai
achega
84f059c65c

+ 0 - 1
tests/Feature/ExampleTest.php

@@ -2,5 +2,4 @@
 
 test('example', function () {
     expect(true)->toBeTrue();
-//    expect('3')->toBe(3);
 });

+ 5 - 9
tests/Feature/UdpConnectionTest.php

@@ -4,7 +4,8 @@ use Symfony\Component\Process\PhpProcess;
 use Workerman\Worker;
 
 $serverAddress = 'udp://127.0.0.1:6789';
-beforeAll(function () use ($serverAddress) {
+$process = null;
+beforeAll(function () use ($serverAddress, &$process) {
     $process = new PhpProcess(<<<PHP
         <?php    
         if(!defined('STDIN')) define('STDIN', fopen('php://stdin', 'r'));
@@ -15,9 +16,6 @@ beforeAll(function () use ($serverAddress) {
         
         \$server = new Worker('$serverAddress');
         \$server->onMessage = function (\$connection, \$data) {
-            if(str_starts_with(\$data, 'bye')) {
-                terminate_current_process();
-            }
             \$connection->send('received: '.\$data);
         };
         Worker::\$command = 'start';
@@ -25,13 +23,11 @@ beforeAll(function () use ($serverAddress) {
     PHP
     );
     $process->start();
-    sleep(5);
+    sleep(1);
 });
 
-afterAll(function () use ($serverAddress) {
-    $socket = stream_socket_client(self::$serverAddress, timeout: 1);
-    fwrite($socket, 'bye');
-    fclose($socket);
+afterAll(function () use (&$process) {
+    $process->stop();
 });
 
 it('tests udp connection', function () use ($serverAddress) {

+ 0 - 5
tests/Pest.php

@@ -58,8 +58,3 @@ function testWithConnectionClose(Closure $closure, string $dataContains = null,
         $tcpConnection->shouldHaveReceived('close');
     }
 }
-
-function terminate_current_process()
-{
-    posix_kill(posix_getppid(), SIGINT);
-}

+ 26 - 19
tests/Unit/Connection/UdpConnectionTest.php

@@ -2,32 +2,39 @@
 
 use Workerman\Connection\UdpConnection;
 use Symfony\Component\Process\PhpProcess;
+use Workerman\Protocols\Text;
 
 $remoteAddress = '[::1]:12345';
-$process = new PhpProcess(<<<PHP
-<?php
-\$socketServer = stream_socket_server("udp://$remoteAddress", \$errno, \$errstr, STREAM_SERVER_BIND);
-do{
-    \$data = stream_socket_recvfrom(\$socketServer, 3);
-}while(\$data !== false && \$data !== 'bye');
-PHP
-);
-$process->start();
+$process = null;
+beforeAll(function () use ($remoteAddress, &$process) {
+    $process = new PhpProcess(<<<PHP
+        <?php
+        \$socketServer = stream_socket_server("udp://$remoteAddress", \$errno, \$errstr, STREAM_SERVER_BIND);
+        do{
+            \$data = stream_socket_recvfrom(\$socketServer, 3);
+        }while(\$data !== false && \$data !== 'bye');
+    PHP
+    );
+    $process->start();
+    sleep(1);
+});
+
+afterAll(function () use (&$process) {
+    $process->stop();
+});
 
 it('tests ' . UdpConnection::class, function () use ($remoteAddress) {
 
     $socketClient = stream_socket_client("udp://$remoteAddress");
     $udpConnection = new UdpConnection($socketClient, $remoteAddress);
-    $udpConnection->protocol = \Workerman\Protocols\Text::class;
-    expect($udpConnection->send('foo'))->toBeTrue();
-
-    expect($udpConnection->getRemoteIp())->toBe('::1');
-    expect($udpConnection->getRemotePort())->toBe(12345);
-    expect($udpConnection->getRemoteAddress())->toBe($remoteAddress);
-    expect($udpConnection->getLocalIp())->toBeIn(['::1', '[::1]', '127.0.0.1']);
-    expect($udpConnection->getLocalPort())->toBeInt();
-
-    expect(json_encode($udpConnection))->toBeJson()
+    $udpConnection->protocol = Text::class;
+    expect($udpConnection->send('foo'))->toBeTrue()
+        ->and($udpConnection->getRemoteIp())->toBe('::1')
+        ->and($udpConnection->getRemotePort())->toBe(12345)
+        ->and($udpConnection->getRemoteAddress())->toBe($remoteAddress)
+        ->and($udpConnection->getLocalIp())->toBeIn(['::1', '[::1]', '127.0.0.1'])
+        ->and($udpConnection->getLocalPort())->toBeInt()
+        ->and(json_encode($udpConnection))->toBeJson()
         ->toContain('transport')
         ->toContain('getRemoteIp')
         ->toContain('remotePort')

+ 2 - 2
tests/Unit/Protocols/FrameTest.php

@@ -3,8 +3,8 @@
 use Workerman\Protocols\Frame;
 
 it('tests ::input', function () {
-    expect(Frame::input('foo'))->toBe(0);
-    expect(Frame::input("\0\0\0*foobar"))
+    expect(Frame::input('foo'))->toBe(0)
+        ->and(Frame::input("\0\0\0*foobar"))
         ->toBe(42);
 });
 

+ 6 - 6
tests/Unit/Protocols/Http/ResponseTest.php

@@ -5,9 +5,9 @@ use Workerman\Protocols\Http\Response;
 it('test some simple case', function () {
     $response = new Response(201, ['X-foo' => 'bar'], 'hello, xiami');
 
-    expect($response->getStatusCode())->toBe(201);
-    expect($response->getHeaders())->toBe(['X-foo' => 'bar']);
-    expect($response->rawBody())->toBe('hello, xiami');
+    expect($response->getStatusCode())->toBe(201)
+        ->and($response->getHeaders())->toBe(['X-foo' => 'bar'])
+        ->and($response->rawBody())->toBe('hello, xiami');
 
     //headers
     $response->header('abc', '123');
@@ -18,8 +18,8 @@ it('test some simple case', function () {
         ->toContain('abc: 123')
         ->toContain('def: 456');
     $response->withoutHeader('def');
-    expect((string)$response)->not->toContain('def: 456');
-    expect($response->getHeader('abc'))
+    expect((string)$response)->not->toContain('def: 456')
+        ->and($response->getHeader('abc'))
         ->toBe('123');
 
     $response->withStatus(202, 'some reason');
@@ -36,7 +36,7 @@ it('test some simple case', function () {
 
 
     //cookie
-    $response->cookie('foo', 'bar', httpOnly: true, domain: 'xia.moe');
+    $response->cookie('foo', 'bar', domain: 'xia.moe', httpOnly: true);
     expect((string)$response)
         ->toContain('Set-Cookie: foo=bar; Domain=xia.moe; HttpOnly');
 });

+ 9 - 11
tests/Unit/Protocols/TextTest.php

@@ -15,16 +15,14 @@ test(Text::class, function () {
     });
     //input without "\n"
     expect(Text::input('jhdxr', $connection))
-        ->toBe(0);
-    //input with "\n"
-    expect(Text::input("jhdxr\n", $connection))
-        ->toBe(6);
-
-    //::encode
-    expect(Text::encode('jhdxr'))
-        ->toBe("jhdxr\n");
-
-    //::decode
-    expect(Text::decode("jhdxr\n"))
+        ->toBe(0)
+        //input with "\n"
+        ->and(Text::input("jhdxr\n", $connection))
+        ->toBe(6)
+        //::encode
+        ->and(Text::encode('jhdxr'))
+        ->toBe("jhdxr\n")
+        //::decode
+        ->and(Text::decode("jhdxr\n"))
         ->toBe('jhdxr');
 });