upscale/swoole-launchpad 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

upscale/swoole-launchpad

Composer 安装命令:

composer require upscale/swoole-launchpad

包简介

Swoole server process management

README 文档

README

This library extends the process management capabilities of Swoole / Open Swoole.

Features:

  • Swoole server launch in child process
  • Swoole server process termination
  • PHPUnit testing framework compatibility

Installation

The library is to be installed via Composer as a dependency:

composer require upscale/swoole-launchpad

Usage

PHPUnit Tests

The library is particularly useful in PHPUnit-based automated tests:

vendor/bin/phpunit --process-isolation
class HttpServerTest extends \PHPUnit\Framework\TestCase
{
    protected \Swoole\Http\Server $server;

    protected \Upscale\Swoole\Launchpad\ProcessManager $processManager;

    protected int $pid;

    protected function setUp(): void
    {
        $this->server = new \Swoole\Http\Server('127.0.0.1', 8080);
        $this->server->set([
            'log_file' => '/dev/null',
            'log_level' => 4,
            'worker_num' => 1,
        ]);
        
        $this->processManager = new \Upscale\Swoole\Launchpad\ProcessManager();
    }

    protected function tearDown(): void
    {
        $this->processManager->kill($this->pid);
    }

    public function testResponseStatus()
    {
        $this->server->on('request', function ($request, $response) {
            $response->status(404);
            $response->end();
        });
        $this->pid = $this->processManager->spawn($this->server);

        $result = `curl http://127.0.0.1:8080/ -s -i`;
        $this->assertStringStartsWith('HTTP/1.1 404 Not Found', $result);
    }
    
    public function testResponseBody()
    {
        $this->server->on('request', function ($request, $response) {
            $response->end('Success');
        });
        $this->pid = $this->processManager->spawn($this->server);

        $result = `curl http://127.0.0.1:8080/ -s -i`;
        $this->assertStringStartsWith('HTTP/1.1 200 OK', $result);
        $this->assertStringEndsWith('Success', $result);
    }
}

More compact version:

class HttpServerTest extends \Upscale\Swoole\Launchpad\Tests\TestCase
{
    protected function setUp(): void
    {
        parent::setUp();
    
        $this->server = new \Swoole\Http\Server('127.0.0.1', 8080);
        $this->server->set([
            'log_file' => '/dev/null',
            'log_level' => 4,
            'worker_num' => 1,
        ]);
    }

    public function testResponseStatus()
    {
        $this->server->on('request', function ($request, $response) {
            $response->status(404);
            $response->end();
        });
        $this->spawn($this->server);

        $result = $this->curl('http://127.0.0.1:8080/');
        $this->assertStringStartsWith('HTTP/1.1 404 Not Found', $result);
    }
    
    public function testResponseBody()
    {
        $this->server->on('request', function ($request, $response) {
            $response->end('Success');
        });
        $this->spawn($this->server);

        $result = $this->curl('http://127.0.0.1:8080/');
        $this->assertStringStartsWith('HTTP/1.1 200 OK', $result);
        $this->assertStringEndsWith('Success', $result);
    }
}

Make sure to autoload the test classes in your composer.json:

{
    "require-dev": {
        "phpunit/phpunit": "^9.5",
        "upscale/swoole-launchpad": "^2.0"
    },
    "autoload-dev": {
        "psr-4": {
            "Upscale\\Swoole\\Launchpad\\Tests\\": "vendor/upscale/swoole-launchpad/tests/"
        }
    }
}

Contributing

Pull Requests with fixes and improvements are welcome!

License

Copyright © Upscale Software. All rights reserved.

Licensed under the Apache License, Version 2.0.

统计信息

  • 总下载量: 9.29k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2019-11-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固