Skip to content

Commit

Permalink
Added setLoop and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbonneau committed Nov 23, 2015
1 parent b76a79c commit 8ef08db
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This library allows use of [React EventLoop](https://github.com/reactphp/event-l
get a reference to the event loop, it will automatically be started at the end of your script.

## Usage Example

Note, there is no need to call run() on the loop as it will run automatically at the conclusion of the script.
(You can still run it sooner if you would like, and it will no longer automatically run at the end.)

```php
\EventLoop\addPeriodicTimer(1, function () {
echo "Hello\n";
Expand All @@ -15,6 +19,12 @@ get a reference to the event loop, it will automatically be started at the end o

// just get a reference for use in other places
$loop = \EventLoop\getLoop();

// you can also set the loop using
// if you call this more than once with a different loop instance,
// the library will throw an exception as there can only be one loop
// and setting it to a different loop would invalidate all previous "getLoop" calls
\EventLoop\setLoop($myLoop);
```
## Installation
```composer require voryx/event-loop```
20 changes: 20 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Global EventLoop">
<directory>tests/</directory>
</testsuite>
</testsuites>

</phpunit>
24 changes: 19 additions & 5 deletions src/EventLoop.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
<?php


namespace EventLoop;


use React\EventLoop\Factory;
use React\EventLoop\LibEventLoop;
use React\EventLoop\Timer\Timer;
use React\EventLoop\LoopInterface;

class EventLoop
{
/** @var LibEventLoop */
static private $loop;

static public function setLoop(LoopInterface $loop) {
if (static::$loop === null) {
static::$loop = $loop;
}

if (static::$loop !== $loop) {
throw new \Exception("Two different loop instances created. Something is not right.");
}

static::registerLoopRunner();
}

static public function getLoop() {
if (static::$loop) {
return static::$loop;
}

static::$loop = Factory::create();

static::registerLoopRunner();

return static::$loop;
}

static private function registerLoopRunner() {
$hasBeenRun = false;

register_shutdown_function(function () use (&$hasBeenRun) {
Expand All @@ -31,7 +47,5 @@ static public function getLoop() {
static::$loop->nextTick(function () use (&$hasBeenRun) {
$hasBeenRun = true;
});

return static::$loop;
}
}
9 changes: 9 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

namespace EventLoop;

use React\EventLoop\LoopInterface;
use React\EventLoop\Timer\TimerInterface;

/**
* @param LoopInterface $loop
* @throws \Exception
*/
function setLoop(LoopInterface $loop) {
EventLoop::setLoop($loop);
}

/**
* @return \React\EventLoop\LibEventLoop
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace EventLoop\Tests;

class TestCase extends \PHPUnit_Framework_TestCase
{

}
59 changes: 59 additions & 0 deletions tests/Unit/EventLoopTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace EventLoop\Tests\Unit;

use EventLoop\EventLoop;
use EventLoop\Tests\TestCase;
use React\EventLoop\Factory;

class EventLoopTest extends TestCase
{
private function resetStaticLoop() {
$ref = new \ReflectionClass(EventLoop::class);
$prop = $ref->getProperty('loop');
$prop->setAccessible(true);
$prop->setValue(null);
$prop->setAccessible(false);
}

public function setup() {
$this->resetStaticLoop();
}

public function testSetLoop()
{
$loop = Factory::create();

\EventLoop\setLoop($loop);

$this->assertSame($loop, \EventLoop\getLoop());
}

public function testSetLoopSameInstance()
{
$loop = \EventLoop\getLoop();

\EventLoop\setLoop($loop);

$this->assertSame($loop, \EventLoop\getLoop());
}

public function testGetLoopWithoutSet() {
$loop = \EventLoop\getLoop();

$this->assertSame($loop, \EventLoop\getLoop());
}

/**
* @expectedException \Exception
*/
public function testSettingDifferentInstance() {
\EventLoop\getLoop();

\EventLoop\setLoop(Factory::create());
}

public function testGetLoopTwice() {
$this->assertSame(\EventLoop\getLoop(), \EventLoop\getLoop());
}
}
17 changes: 17 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

$files = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../vendor/autoload.php',
__DIR__ . '/../../../../vendor/autoload.php',

];

foreach ($files as $file) {
if (file_exists($file)) {
$loader = require_once $file;
$loader->addPsr4('EventLoop\\Tests\\', __DIR__);
break;
}
}

0 comments on commit 8ef08db

Please sign in to comment.