Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stenin-nikita committed Apr 8, 2017
1 parent 668c8d3 commit f751ecf
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/.scrutinizer.yml export-ignore
/tests export-ignore
36 changes: 36 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
language: php

php:
- 5.6
- 7.0
- 7.1

env:
global:
- setup=basic

matrix:
fast_finish: true
include:
- php: 5.6
env: setup=lowest
- php: 5.6
env: setup=stable

sudo: false

cache:
directories:
- $HOME/.composer/cache

before_install:
- docker pull centrifugo/centrifugo
- docker run --ulimit nofile=65536:65536 -v /tests:/centrifugo -p 8000:8000 centrifugo/centrifugo centrifugo -c config.json
- travis_retry composer self-update

install:
- if [[ $setup = 'basic' ]]; then travis_retry composer install --prefer-dist --no-interaction --no-suggest; fi
- if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --no-suggest --prefer-stable; fi
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --no-suggest --prefer-stable --prefer-lowest; fi

script: vendor/bin/phpunit
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@
"predis/predis": "^1.1",
"illuminate/broadcasting": "^5.3"
},
"require-dev": {
"orchestra/testbench": "^3.4",
"phpunit/phpunit": "^6.1"
},
"autoload": {
"psr-4": {
"LaraComponents\\Centrifuge\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"LaraComponents\\Centrifuge\\Test\\": "tests/"
}
},
"prefer-stable": true
}
28 changes: 28 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Centrifuge Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
96 changes: 96 additions & 0 deletions tests/CentrifugeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace LaraComponents\Centrifuge\Test;

class CentrifugeTest extends TestCase
{
public function testGenerateToken()
{
$timestamp = 1491650279;
$user_id = 1;
$info = json_encode([
'first_name' => 'Nikita',
'last_name' => 'Stenin'
]);
$client = '0c951315-be0e-4516-b99e-05e60b0cc307';
$channel = 'test-channel';

$clientToken1 = $this->centrifuge->generateToken($user_id, $timestamp);
$this->assertEquals('558880399d21bd215e4d1558dd95efad9b82f829a1d44910fb611eeeffac3c50', $clientToken1);

$clientToken2 = $this->centrifuge->generateToken($user_id, $timestamp, $info);
$this->assertEquals('37722e22cee00160d777fd8d594dd831a9d5404016d5515a2cffc7c102ea67ee', $clientToken2);

$channelSign = $this->centrifuge->generateToken($client, $channel);
$this->assertEquals('02fc39b64c252108e80cfdcab2ef774f13a181f5149d21cebabd6eca08d231d2', $channelSign);
}

public function testGenerateApiSign()
{
$json = json_encode(['method' => 'publish', 'params' => [
'channel' => 'test-channel',
]]);

$apiSign = $this->centrifuge->generateApiSign($json);
$this->assertEquals('11950468714031c2e0b95e284482ba6e8d9e17e1a6115eb0db6feded7581ebba', $apiSign);
}

public function testCentrifugeApi()
{
$publish = $this->centrifuge->publish('test-channel', ['event' => 'test-event']);
$this->assertEquals($publish, [
'method' => 'publish',
'error' => null,
'body' => null,
]);

$broadcast = $this->centrifuge->broadcast(['test-channel-1', 'test-channel-2'], ['event' => 'test-event']);
$this->assertEquals($broadcast, [
'method' => 'broadcast',
'error' => null,
'body' => null,
]);

$presence = $this->centrifuge->presence('test-channel');
$this->assertEquals($presence, [
'method' => 'presence',
'error' => null,
'body' => [
'channel' => 'test-channel',
'data' => [],
],
]);

$history = $this->centrifuge->history('test-channel');
$this->assertEquals($history['method'], 'history');
$this->assertEquals($history['error'], null);
$this->assertEquals($history['body']['channel'], 'test-channel');

$channels = $this->centrifuge->channels();
$this->assertEquals($channels, [
"method" => "channels",
"error" => null,
"body" => [
"data" => [],
],
]);

$unsubscribe = $this->centrifuge->unsubscribe(1);
$this->assertEquals($unsubscribe, [
'method' => 'unsubscribe',
'error' => null,
'body' => null,
]);

$disconnect = $this->centrifuge->disconnect(1);
$this->assertEquals($disconnect, [
'method' => 'disconnect',
'error' => null,
'body' => null,
]);

$stats = $this->centrifuge->stats();
$this->assertEquals($stats['method'], 'stats');
$this->assertEquals($stats['error'], null);
}
}
39 changes: 39 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace LaraComponents\Centrifuge\Test;

use Orchestra\Testbench\TestCase as Orchestra;
use LaraComponents\Centrifuge\CentrifugeServiceProvider;

class TestCase extends Orchestra
{
/**
* @var \LaraComponents\Centrifuge\Centrifuge
*/
protected $centrifuge;

public function setUp()
{
parent::setUp();
$this->centrifuge = $this->app->make('centrifuge');
}

protected function getPackageProviders($app)
{
return [
CentrifugeServiceProvider::class
];
}

protected function getEnvironmentSetUp($app)
{
$app['config']->set('broadcasting.default', 'centrifuge');
$app['config']->set('broadcasting.connections.centrifuge', [
'driver' => 'centrifuge',
'secret' => 'f95bf295-bee6-4259-8912-0a58f4ecd30e',
'url' => 'http://localhost:8000',
'redis_api' => false,
'redis_connection' => 'default',
]);
}
}
12 changes: 12 additions & 0 deletions tests/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"address": "localhost",
"anonymous": true,
"engine": "memory",
"insecure": true,
"join_leave": true,
"port": 8000,
"presence": true,
"publish": true,
"secret": "f95bf295-bee6-4259-8912-0a58f4ecd30e",
"watch": true
}

0 comments on commit f751ecf

Please sign in to comment.