Skip to content

Commit

Permalink
update supervisor
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed Mar 21, 2021
1 parent 85c83d5 commit 49e96bf
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor
composer.lock
.php_cs.cache
.phpunit.result.cache
.*.sw?
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "3"

services:
phpunit:
image: hillliu/pmvc-phpunit:5.6
volumes:
- .:/var/www/html
- .:/var/www/supervisor
23 changes: 23 additions & 0 deletions include_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
$path = __DIR__.'/vendor/autoload.php';
require $path;
\PMVC\Load::plug();
\PMVC\addPlugInFolders(['../']);

if (!class_exists('PHPUnit_Framework_TestCase')) {
class PHPUnit_Framework_TestCase extends
\PHPUnit\Framework\TestCase
{
}
}

class PMVC_TestCase extends PHPUnit_Framework_TestCase
{
public function haveString($needle, $haystack) {
if (is_callable([$this, "assertStringContainsString"])) {
$this->assertStringContainsString($needle, $haystack);
} else {
$this->assertContains($needle, $haystack);
}
}
}
12 changes: 12 additions & 0 deletions phpunit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

DIR="$( cd "$(dirname "$0")" ; pwd -P )"
PLUGIN_NAME="supervisor"

docker run --rm \
-v $DIR:/var/www/html \
-v $DIR:/var/www/${PLUGIN_NAME} \
--name phpunit hillliu/pmvc-phpunit:5.6 \
phpunit --no-configuration --bootstrap ./include_test.php ./tests-legacy/

# docker-compose run --rm phpunit phpunit --no-configuration --bootstrap ./include_test.php ./tests-legacy/test.php
6 changes: 3 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
bootstrap="vendor/autoload.php"
bootstrap="include_test.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
>
<testsuites>
<testsuite name="Application Test Suite">
<file>test.php</file>
<directory>./tests/</directory>
</testsuite>
</testsuites>
<php>
Expand Down
4 changes: 3 additions & 1 deletion src/Signal.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

namespace PMVC\PlugIn\supervisor;

class Signal
{
public function __construct()
{
$arr = [SIGHUP, SIGINT, SIGTERM];
foreach ($arr as $sign) {
pcntl_signal($sign, $this);
\pcntl_signal($sign, $this);
}
}

Expand Down
4 changes: 2 additions & 2 deletions supervisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

${_INIT_CONFIG}[_CLASS] = __NAMESPACE__.'\supervisor';

\PMVC\l(__DIR__.'/src/Signal.php');

// storage
const CALLBACKS = 'callbacks';
Expand Down Expand Up @@ -57,6 +56,7 @@ public function __construct()

public function init()
{
\PMVC\l(__DIR__.'/src/Signal.php');
new Signal(); // call it in init to avoid infinity
}

Expand Down Expand Up @@ -95,7 +95,6 @@ public function process(callable $monitorCallBack = null)
$this->_createPidFile();
}
}
\PMVC\l(__DIR__.'/src/Monitor.php');
foreach ($this[CALLBACKS] as $callbackId=>$callback) {
$trigger = \PMVC\get($callback, TRIGGER);
if (strlen($trigger)) {
Expand All @@ -107,6 +106,7 @@ public function process(callable $monitorCallBack = null)
$this->start($callbackId);
}
}
\PMVC\l(__DIR__.'/src/Monitor.php');
new Monitor($monitorCallBack);
}
}
Expand Down
4 changes: 1 addition & 3 deletions test.php → tests-legacy/SupervisorTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
PMVC\Load::plug();
PMVC\addPlugInFolders(['../']);
class SupervisorTest extends PHPUnit_Framework_TestCase
class SupervisorTest extends PMVC_TestCase
{
private $_plug = 'supervisor';

Expand Down
84 changes: 84 additions & 0 deletions tests/SupervisorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

use PMVC_TestCase;

class SupervisorTest extends PMVC_TestCase
{
private $_plug = 'supervisor';

protected function teardown(): void
{
\PMVC\unplug($this->_plug);
}

function testPlugin()
{
ob_start();
print_r(PMVC\plug($this->_plug));
$output = ob_get_contents();
ob_end_clean();
$this->haveString($this->_plug,$output);
}

function testScript()
{
$plug = PMVC\plug($this->_plug);
$s = 'helloScript';
@$plug->script(new fakeChild(), array($s, 0));
$self = $this;
@$plug->process(function() use($plug, $self, $s){
$plug->stop();
$self->assertEquals($s,$plug['callbacks'][0]['args'][0]);
});
}

function testDaemon()
{
$s = 'helloDaemon';
$plug = PMVC\plug($this->_plug);
@$plug->daemon(new fakeDaemon(), [$s, 1]);
@$plug->process(function() use ($plug) {
$plug->forceStop();
$this->assertEquals('daemon',$plug['callbacks'][0]['type']);
});
}

function testTrigger()
{
$plug = PMVC\plug($this->_plug);
$s = 'helloTrigger';
$self = $this;
@$childKey = $plug->script(new fakeChild(), array($s.'3', 3));
@$second = $plug->script(new fakeChild(), array($s.'4', 4), $childKey);
@$third = $plug->script(new fakeChild(), array($s.'5', 5), $second);
@$plug->process(function($callbackId, $pid) use($plug, $self, $second){
static $i = 0;
if (!$i) {
$self->assertTrue(empty($plug['callbacks'][$second]['startTime']), 'Test first');
} else {
$self->assertFalse(empty($plug['callbacks'][$second]['startTime']), 'Test second');
}
$i++;
});
}
}

class fakeChild
{
function __invoke($s, $exit)
{
echo $s."\n";
exit($exit);
}
}

/**
* fakeDaemon without exit
*/
class fakeDaemon
{
function __invoke($s, $exit)
{
echo $s."\n";
}
}

0 comments on commit 49e96bf

Please sign in to comment.