-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
960689c
commit 107b968
Showing
7 changed files
with
290 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace Reedware\LaravelEvents\Tests; | ||
|
||
use Illuminate\Config\Repository; | ||
use Illuminate\Container\Container; | ||
use Illuminate\Filesystem\Filesystem; | ||
use Illuminate\Foundation\Application; | ||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase; | ||
use Reedware\LaravelEvents\EventServiceProvider; | ||
use Reedware\LaravelEvents\NormalizeEvents; | ||
use ReflectionFunction; | ||
|
||
class ConfiguredEventsTest extends TestCase | ||
{ | ||
protected $app; | ||
protected $provider; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->app = new Application(__DIR__); | ||
$this->app->instance('config', $config = new Repository); | ||
|
||
$config->set('events', require realpath(__DIR__ . DIRECTORY_SEPARATOR . 'events.php')); | ||
|
||
$this->app->singleton('files', function() { | ||
return tap(m::mock(Filesystem::class), function($mock) { | ||
$mock->shouldReceive('exists') | ||
->withAnyArgs() | ||
->andReturn(false); | ||
}); | ||
}); | ||
|
||
$this->provider = $this->app->register(EventServiceProvider::class); | ||
$this->app->boot(); | ||
} | ||
|
||
public function testConfiguredEvents() | ||
{ | ||
$config = $this->app->make('config')->get('events'); | ||
$provided = $this->provider->configuredEvents(); | ||
|
||
foreach($config['listen'] as $event => $listeners) { | ||
foreach($listeners as $listener) { | ||
$normalized = NormalizeEvents::normalizeListener($listener, $event); | ||
$this->assertTrue(in_array($normalized, $provided[$event])); | ||
} | ||
} | ||
|
||
foreach($config['subscribe'] as $subscriber => $events) { | ||
foreach($events as $event) { | ||
[$event, $method] = is_array($event) ? $event : [$event, 'handle']; | ||
$normalized = NormalizeEvents::normalizeListener([$subscriber, $method], $event); | ||
$this->assertTrue(in_array($normalized, $provided[$event])); | ||
} | ||
} | ||
|
||
foreach($config['observe'] as $observer => $models) { | ||
foreach($models as $model) { | ||
$observes = NormalizeEvents::getObservableEvents($observer, $model); | ||
foreach($observes as $method) { | ||
$this->assertTrue(in_array([$observer, $method], $provided["eloquent.{$method}: {$model}"])); | ||
} | ||
} | ||
} | ||
|
||
foreach($config['models'] as $model => $observers) { | ||
foreach($observers as $observer) { | ||
$observes = NormalizeEvents::getObservableEvents($observer, $model); | ||
foreach($observes as $method) { | ||
$this->assertTrue(in_array([$observer, $method], $provided["eloquent.{$method}: {$model}"])); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public function testEventRegistration() | ||
{ | ||
$events = $this->provider->configuredEvents(); | ||
$dispatcher = $this->app->make('events'); | ||
|
||
foreach($events as $event => $configured) { | ||
|
||
$registered = array_map(function($listener) { | ||
return $this->unmakeListener($listener); | ||
}, $dispatcher->getListeners($event)); | ||
|
||
foreach($configured as $listener) { | ||
$this->assertTrue( | ||
in_array($listener, $registered), | ||
sprintf('Listener [%s] cannot be found within [%s]', | ||
json_encode($listener), | ||
json_encode($registered) | ||
) | ||
); | ||
} | ||
|
||
} | ||
} | ||
|
||
protected function unmakeListener($listener) | ||
{ | ||
$r = new ReflectionFunction($listener); | ||
|
||
$use = $r->getStaticVariables(); | ||
|
||
return $use['listener']; | ||
} | ||
} | ||
|
||
namespace App\Listeners; | ||
class SendAnotherNotification {} | ||
class UserEventSubscriber {} | ||
|
||
namespace App\Models; | ||
use Illuminate\Database\Eloquent\Model; | ||
class Post extends Model {} | ||
class User extends Model {} | ||
|
||
namespace App\Observers; | ||
class UserObserver { function created(){} } | ||
class PostObserver { function created(){} } | ||
|
||
namespace App\Subscribers; | ||
class InverseUserEventSubscriber {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Reedware\LaravelEvents\Tests; | ||
|
||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase as TestBase; | ||
|
||
abstract class TestCase extends TestBase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
m::close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
return [ | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Event / Listener Mapping | ||
|-------------------------------------------------------------------------- | ||
| | ||
| An event must be explictly listened to by a listener in order for that | ||
| listener to be able to handle its event. You can register events & | ||
| listeners by mapping events their respective listeners below. | ||
| | ||
*/ | ||
|
||
'listen' => [ | ||
|
||
Illuminate\Auth\Events\Registered::class => [ | ||
Illuminate\Auth\Listeners\SendEmailVerificationNotification::class, | ||
App\Listeners\SendAnotherNotification::class, | ||
], | ||
|
||
Illuminate\Auth\Events\Login::class => [ | ||
App\Listeners\UserEventSubscriber::class, | ||
], | ||
|
||
Illuminate\Auth\Events\Logout::class => [ | ||
[App\Listeners\UserEventSubscriber::class, 'handleUserLogout'] | ||
], | ||
|
||
], | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Subscriber / Event Mapping | ||
|-------------------------------------------------------------------------- | ||
| | ||
| If you prefer to list out subscribers with their explicit events, rather | ||
| than listing the subscriber under each event like above, you instead | ||
| can list them here as an alternative. Do what makes sense to you. | ||
| | ||
*/ | ||
|
||
'subscribe' => [ | ||
|
||
App\Subscribers\InverseUserEventSubscriber::class => [ | ||
Illuminate\Auth\Events\Login::class, | ||
[Illuminate\Auth\Events\Logout::class, 'handleUserLogout'] | ||
] | ||
|
||
], | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Observer / Model Mapping | ||
|-------------------------------------------------------------------------- | ||
| | ||
| In certain cases, it may be fitting to listen to one or many events used | ||
| by eloquent models. This can also be done using the observer design | ||
| pattern. Observers can have many models, simply list them below. | ||
| | ||
*/ | ||
|
||
'observe' => [ | ||
|
||
App\Observers\UserObserver::class => [ | ||
App\Models\User::class | ||
] | ||
|
||
], | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Model / Observer Mapping | ||
|-------------------------------------------------------------------------- | ||
| | ||
| If you prefer to list out models with their explicit observers, rather | ||
| than listing each model under each observer like above, you instead | ||
| can list them here as an alternative. Do what makes sense to you. | ||
| | ||
*/ | ||
|
||
'models' => [ | ||
|
||
App\Models\Post::class => [ | ||
App\Observers\PostObserver::class | ||
] | ||
|
||
] | ||
|
||
]; |