Skip to content

Add test #904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: tests

on:
push:
branches:
- master
- feature/tests
pull_request:
schedule:
- cron: '0 0 * * *'

jobs:
linux_tests:
runs-on: ubuntu-22.04

strategy:
fail-fast: true
matrix:
php: [8.1, 8.2]
stability: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: json
ini-values: error_reporting=E_ALL
tools: composer:v2
coverage: xdebug

- name: Install dependencies
uses: nick-fields/retry@v2
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress
# command: composer install --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/pest --coverage

9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,14 @@
"minimum-stability": "dev",
"conflict": {
"ext-swow": "<v1.0.0"
},
"require-dev": {
"pestphp/pest": "2.x-dev",
"mockery/mockery": "2.0.x-dev"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
6 changes: 6 additions & 0 deletions tests/Feature/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

test('example', function () {
expect(true)->toBeTrue();
// expect('3')->toBe(3);
});
31 changes: 31 additions & 0 deletions tests/Feature/UdpConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
//example from manual
use Workerman\Connection\AsyncUdpConnection;
use Workerman\Timer;
use Workerman\Worker;

it('tests udp connection', function () {
/** @noinspection PhpObjectFieldsAreOnlyWrittenInspection */
$server = new Worker('udp://0.0.0.0:9292');
$server->onMessage = function ($connection, $data) {
expect($data)->toBe('hello');
$connection->send('xiami');
};
$server->onWorkerStart = function () {
//client
Timer::add(1, function () {
$client = new AsyncUdpConnection('udp://127.0.0.1:1234');
$client->onConnect = function ($client) {
$client->send('hello');
};
$client->onMessage = function ($client, $data) {
expect($data)->toBe('xiami');
//terminal this test
terminate_current_test();
};
$client->connect();
}, null, false);
};
Worker::runAll();
})->skipOnWindows() //require posix, multiple workers
->skip(message: 'this test needs to run isolated process while pest not support doing so yet');
65 changes: 65 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

// uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

use Workerman\Connection\TcpConnection;

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}

function testWithConnectionClose(Closure $closure, string $dataContains = null, $connectionClass = TcpConnection::class): void
{
$tcpConnection = Mockery::spy($connectionClass);
$closure($tcpConnection);
if ($dataContains) {
$tcpConnection->shouldHaveReceived('close', function ($actual) use ($dataContains) {
return str_contains($actual, $dataContains);
});
} else {
$tcpConnection->shouldHaveReceived('close');
}
}

function terminate_current_test()
{
posix_kill(posix_getppid(), SIGINT);
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
//
}
44 changes: 44 additions & 0 deletions tests/Unit/Connection/UdpConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use Workerman\Connection\UdpConnection;
use Symfony\Component\Process\PhpProcess;

$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();

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()
->toContain('transport')
->toContain('getRemoteIp')
->toContain('remotePort')
->toContain('getRemoteAddress')
->toContain('getLocalIp')
->toContain('getLocalPort')
->toContain('isIpV4')
->toContain('isIpV6');

$udpConnection->close('bye');
if (is_resource($socketClient)) {
fclose($socketClient);
}
});
20 changes: 20 additions & 0 deletions tests/Unit/Protocols/FrameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Workerman\Protocols\Frame;

it('tests ::input', function () {
expect(Frame::input('foo'))->toBe(0);
expect(Frame::input("\0\0\0*foobar"))
->toBe(42);
});

it('tests ::decode', function () {
$buffer = pack('N', 5) . 'jhdxr';
expect(Frame::decode($buffer))
->toBe('jhdxr');
});

it('tests ::encode', function () {
expect(Frame::encode('jhdxr'))
->toBe(pack('N', 9) . 'jhdxr');
});
57 changes: 57 additions & 0 deletions tests/Unit/Protocols/Http/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

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');

//headers
$response->header('abc', '123');
$response->withHeader('X-foo', 'baz');
$response->withHeaders(['def' => '456']);
expect((string)$response)
->toContain('X-foo: baz')
->toContain('abc: 123')
->toContain('def: 456');
$response->withoutHeader('def');
expect((string)$response)->not->toContain('def: 456');
expect($response->getHeader('abc'))
->toBe('123');

$response->withStatus(202, 'some reason');
expect($response->getReasonPhrase())->toBe('some reason');

$response->withProtocolVersion('1.0');
$response->withBody('hello, world');
expect((string)$response)
->toContain('HTTP/1.0')
->toContain('hello, world')
->toContain('Content-Type: ')
->toContain('Content-Length: 12')
->not()->toContain('Transfer-Encoding: ');


//cookie
$response->cookie('foo', 'bar', httpOnly: true, domain: 'xia.moe');
expect((string)$response)
->toContain('Set-Cookie: foo=bar; Domain=xia.moe; HttpOnly');
});

it('tests file', function (){
//todo may have to redo the simple test,
// as the implementation of headers is a different function for files.
// or actually maybe the Response is the one should be rewritten to reuse?
$response = new Response();
$tmpFile = tempnam(sys_get_temp_dir(), 'test');
rename($tmpFile, $tmpFile .'.jpg');
$tmpFile .= '.jpg';
file_put_contents($tmpFile, 'hello, xiami');
$response->withFile($tmpFile, 0, 12);
expect((string)$response)
->toContain('Content-Type: image/jpeg')
->toContain('Last-Modified: ');
});
15 changes: 15 additions & 0 deletions tests/Unit/Protocols/Http/ServerSentEventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use Workerman\Protocols\Http\ServerSentEvents;

it('tests ' . ServerSentEvents::class, function () {
$data = [
'event' => 'ping',
'data' => 'some thing',
'id' => 1000,
'retry' => 5000,
];
$sse = new ServerSentEvents($data);
$expected = "event: {$data['event']}\ndata: {$data['data']}\n\nid: {$data['id']}\nretry: {$data['retry']}\n";
expect((string)$sse)->toBe($expected);
});
Loading