Skip to content

Commit

Permalink
Add PHPUnit tests (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records authored Dec 25, 2020
1 parent f06d0c9 commit c0be169
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 5 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: test
name: PHPUnit for MQTT

on: [push, pull_request]

Expand All @@ -20,6 +20,21 @@ jobs:
with:
php-version: ${{ matrix.php-versions }}
tools: phpize
ini-values: extension=swoole
coverage: none

- name: Build Swoole
run: |
version=$(curl -s https://api.github.com/repos/swoole/swoole-src/releases/latest | jq '.tag_name' -r)
wget https://github.com/swoole/swoole-src/archive/${version}.tar.gz -O swoole.tar.gz
mkdir -p swoole
tar -xf swoole.tar.gz -C swoole --strip-components=1
rm swoole.tar.gz
cd swoole
phpize
./configure --enable-openssl --enable-mysqlnd --enable-http2
make -j$(nproc)
sudo make install
- name: Setup Deps
run:
Expand All @@ -29,3 +44,7 @@ jobs:
if: ${{ matrix.php-versions != '8.0' }}
run: |
composer cs-check
- name: Run Test Cases
run: |
composer test
2 changes: 1 addition & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[![PHP Version](https://img.shields.io/badge/php-%3E=7.0-blue.svg)](https://www.php.net)
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.4.19-blue.svg)](https://github.com/swoole/swoole-src)

[![GitHub Actions](https://github.com/simps/mqtt/workflows/test/badge.svg)](https://github.com/simps/mqtt/actions)
[![GitHub Actions](https://github.com/simps/mqtt/workflows/PHPUnit%20for%20MQTT/badge.svg)](https://github.com/simps/mqtt/actions)
[![Join Discord community and chat about Simps](https://img.shields.io/discord/740738911625674872.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2&cacheSeconds=60)](https://discord.gg/u4YAqeh)
[![Simps 交流群](https://img.shields.io/badge/QQ%E7%BE%A4-983679945-orange)](https://shang.qq.com/wpa/qunwpa?idkey=587be6cdd66b437f2306b1a6d7263fd8096b70ef1db716a259485219f28d41a3)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Support for `QoS 0`, `QoS 1`, `QoS 2`.
[![PHP Version](https://img.shields.io/badge/php-%3E=7.0-blue.svg)](https://www.php.net)
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.4.19-blue.svg)](https://github.com/swoole/swoole-src)

[![GitHub Actions](https://github.com/simps/mqtt/workflows/test/badge.svg)](https://github.com/simps/mqtt/actions)
[![GitHub Actions](https://github.com/simps/mqtt/workflows/PHPUnit%20for%20MQTT/badge.svg)](https://github.com/simps/mqtt/actions)
[![Join Discord community and chat about Simps](https://img.shields.io/discord/740738911625674872.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2&cacheSeconds=60)](https://discord.gg/u4YAqeh)
[![Simps 交流群](https://img.shields.io/badge/QQ%E7%BE%A4-983679945-orange)](https://shang.qq.com/wpa/qunwpa?idkey=587be6cdd66b437f2306b1a6d7263fd8096b70ef1db716a259485219f28d41a3)

Expand Down
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@
"ext-swoole": ">=4.4.19"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16"
"friendsofphp/php-cs-fixer": "^2.16",
"phpunit/phpunit": "^7.0.0"
},
"autoload": {
"psr-4": {
"Simps\\MQTT\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"SimpsTest\\MQTT\\": "tests/"
}
},
"scripts": {
"cs-check": "/usr/bin/env php ./vendor/bin/php-cs-fixer fix --dry-run",
"cs-fix": "/usr/bin/env php ./vendor/bin/php-cs-fixer fix"
"cs-fix": "/usr/bin/env php ./vendor/bin/php-cs-fixer fix",
"test": "./tests/co-phpunit --colors=always"
}
}
11 changes: 11 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false" bootstrap="tests/bootstrap.php" beStrictAboutTestsThatDoNotTestAnything="false" verbose="true">
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
<php>
<ini name="error_reporting" value="-1" />
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
</php>
</phpunit>
110 changes: 110 additions & 0 deletions tests/V3/PacketTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* This file is part of Simps
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <[email protected]>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*/

declare(strict_types=1);

namespace SimpsTest\MQTT\V3;

use PHPUnit\Framework\TestCase;
use Simps\MQTT\Client;
use Simps\MQTT\Hex\ReasonCode;
use Simps\MQTT\Types;

/**
* @internal
* @coversNothing
*/
class PacketTest extends TestCase
{
public function testConnect()
{
$config = getTestConnectConfig('broker.emqx.io');
$client = new Client($config, SWOOLE_MQTT_CONFIG);
$res = $client->connect();
$this->assertIsArray($res);
$this->assertSame($res['type'], Types::CONNACK);

return $client;
}

/**
* @depends testConnect
*/
public function testSubscribe(Client $client)
{
$topics['simpsmqtt/get'] = 1;
$res = $client->subscribe($topics);
$this->assertIsArray($res);
$this->assertSame($res['type'], Types::SUBACK);
$this->assertSame($res['codes'][0], ReasonCode::GRANTED_QOS_1);

return $client;
}

/**
* @depends testSubscribe
*/
public function testPublish(Client $client)
{
$buffer = $client->publish('simpsmqtt/get', 'hello,simps', 1);
$this->assertIsArray($buffer);
$this->assertSame($buffer['type'], Types::PUBACK);

return $client;
}

/**
* @depends testPublish
*/
public function testRecv(Client $client)
{
$buffer = $client->recv();
$this->assertIsArray($buffer);
$this->assertSame($buffer['type'], Types::PUBLISH);
$this->assertSame($buffer['topic'], 'simpsmqtt/get');
$this->assertSame($buffer['message'], 'hello,simps');

return $client;
}

/**
* @depends testRecv
*/
public function testPing(Client $client)
{
$buffer = $client->ping();
$this->assertIsArray($buffer);
$this->assertSame($buffer['type'], Types::PINGRESP);

return $client;
}

/**
* @depends testRecv
*/
public function testUnsubscribe(Client $client)
{
$status = $client->unSubscribe(['simpsmqtt/get']);
$this->assertIsArray($status);
$this->assertSame($status['type'], Types::UNSUBACK);

return $client;
}

/**
* @depends testUnsubscribe
*/
public function testClose(Client $client)
{
$status = $client->close();
$this->assertTrue($status);
}
}
126 changes: 126 additions & 0 deletions tests/V5/PacketTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* This file is part of Simps
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <[email protected]>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*/

declare(strict_types=1);

namespace SimpsTest\MQTT\V5;

use PHPUnit\Framework\TestCase;
use Simps\MQTT\Client;
use Simps\MQTT\Hex\ReasonCode;
use Simps\MQTT\Types;

/**
* @internal
* @coversNothing
*/
class PacketTest extends TestCase
{
public function testConnect()
{
$config = getTestMQTT5ConnectConfig('broker.emqx.io');
$client = new Client($config, SWOOLE_MQTT_CONFIG);
$res = $client->connect();
$this->assertIsArray($res);
$this->assertSame($res['type'], Types::CONNACK);

return $client;
}

/**
* @depends testConnect
*/
public function testSubscribe(Client $client)
{
$topics['simps-mqtt/user001/get'] = [
'qos' => 1,
'no_local' => true,
'retain_as_published' => true,
'retain_handling' => 2,
];
$topics['simps-mqtt/user001/update'] = [
'qos' => 2,
'no_local' => false,
'retain_as_published' => true,
'retain_handling' => 2,
];
$topics['testtopic/#'] = [
'qos' => 0,
];
$res = $client->subscribe($topics);
$this->assertIsArray($res);
$this->assertSame($res['type'], Types::SUBACK);
$this->assertIsArray($res['codes']);
$this->assertSame($res['codes'][0], ReasonCode::GRANTED_QOS_1);
$this->assertSame($res['codes'][1], ReasonCode::GRANTED_QOS_2);

return $client;
}

/**
* @depends testSubscribe
*/
public function testPublish(Client $client)
{
$buffer = $client->publish('simps-mqtt/user001/get', 'hello,simps', 1);
$this->assertIsArray($buffer);
$this->assertSame($buffer['type'], Types::PUBACK);
$this->assertSame(ReasonCode::getReasonPhrase($buffer['code']), 'Success');
return $client;
}

/**
* @depends testPublish
*/
public function testRecv(Client $client)
{
$buffer = $client->recv();
$this->assertIsArray($buffer);
$this->assertSame($buffer['type'], Types::PUBLISH);
$this->assertIsString($buffer['topic']);

return $client;
}

/**
* @depends testSubscribe
*/
public function testPing(Client $client)
{
$buffer = $client->ping();
$this->assertIsArray($buffer);
$this->assertSame($buffer['type'], Types::PINGRESP);

return $client;
}

/**
* @depends testSubscribe
*/
public function testUnsubscribe(Client $client)
{
$status = $client->unSubscribe(['simps-mqtt/user001/get']);
$this->assertIsArray($status);
$this->assertSame($status['type'], Types::UNSUBACK);
$this->assertSame(ReasonCode::getReasonPhrase($status['code']), 'Success');

return $client;
}

/**
* @depends testUnsubscribe
*/
public function testClose(Client $client)
{
$status = $client->close();
$this->assertTrue($status);
}
}
14 changes: 14 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* This file is part of Simps
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <[email protected]>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*/

declare(strict_types=1);

require_once dirname(__DIR__) . '/examples/bootstrap.php';
Loading

0 comments on commit c0be169

Please sign in to comment.