Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
schpill committed Jul 7, 2017
1 parent 20ff3f3 commit 572d452
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
29 changes: 29 additions & 0 deletions lib/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -6636,3 +6636,32 @@ public function __call($m, $a)
throw new \BadMethodCallException("Method {$m} does not exist.");
}
}
/**
* Events helpers classes
*
*/

class InternalEvents extends Fire {}

class On
{
public static function __callStatic($m, $a)
{
$event = array_shift($a);
$priority = array_shift($a);

InternalEvents::listen(Inflector::uncamelize($m), $event, $priority);
}
}

class Emit
{
public static function __callStatic($m, $a)
{
$event = Inflector::uncamelize($m);

$args = array_merge([$event], $a);

return forward_static_call_array(['Octo\InternalEvents', 'fire'], $args);
}
}
2 changes: 1 addition & 1 deletion lib/serviceprovider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class ServiceProvider

public function __construct($app = null)
{
$app = !$app ? app() : $app;
$app = !$app ? context('app') : $app;

$this->app = $app;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/SimpleTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php
use Octo\Registry;
use Octo\InternalEvents;
use Octo\On;
use Octo\Emit;
use function Octo\context;

class MyEvent extends Octo\Fire {}
Expand Down Expand Up @@ -81,6 +84,32 @@ public function static_fire_class()
$this->assertEquals(3, $this->app['event_test_value']);
}

/** @test */
public function internal_fire_class()
{
$this->app['event_test_value'] = 0;

$this->assertTrue(InternalEvents::called() instanceof Octo\Fire);
$this->assertTrue(InternalEvents::called() instanceof InternalEvents);
$this->assertEquals('Octo\InternalEvents', InternalEvents::called()->ns());

$this->assertEquals(0, count(Registry::get('fire.events.Octo\InternalEvents', [])));

On::test(function () {
$this->app['event_test_value'] += 2;
});

$this->assertEquals(1, count(Registry::get('fire.events.Octo\InternalEvents', [])));

Emit::test();

$this->assertEquals(2, $this->app['event_test_value']);

Emit::test();

$this->assertEquals(4, $this->app['event_test_value']);
}

/** @test */
public function it_burst()
{
Expand Down
71 changes: 71 additions & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
class UtilsTest extends TestCase
{
/** @test */
public function firstReturnsFirstItemInCollection()
{
$c = $this->coll(['foo', 'bar']);
$this->assertEquals('foo', $c->first());
}

/** @test */
public function lastReturnsLastItemInCollection()
{
$c = $this->coll(['foo', 'bar']);
$this->assertEquals('bar', $c->last());
}

/** @test */
public function firstWithCallback()
{
$data = $this->coll(['foo', 'bar', 'baz']);

$result = $data->first(function ($key, $value) {
return $value === 'bar';
});

$this->assertEquals('bar', $result);
}

/** @test */
public function firstWithCallbackAndDefault()
{
$data = $this->coll(['foo', 'bar']);

$result = $data->first(function ($key, $value) {
return $value === 'baz';
}, 'default');

$this->assertEquals('default', $result);
}

/** @test */
public function shiftReturnsAndRemovesFirstItemInCollection()
{
$c = $this->coll(['foo', 'bar']);

$this->assertEquals('foo', $c->shift());
$this->assertEquals('bar', $c->first());
}

/** @test */
public function popReturnsAndRemovesLastItemInCollection()
{
$c = $this->coll(['foo', 'bar']);

$this->assertEquals('bar', $c->pop());
$this->assertEquals('foo', $c->first());
}

/** @test */
public function it_should_be_uppered()
{
$this->assertEquals('BAR', $this->lib('inflector')->upper('bar'));
}

/** @test */
public function it_should_be_lowered()
{
$this->assertEquals('bar', $this->lib('inflector')->lower('BAR'));
}
}

0 comments on commit 572d452

Please sign in to comment.