Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage includeUncoveredFiles="false">
<report>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/13.0/phpunit.xsd" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
Expand All @@ -17,7 +12,7 @@
</php>
<source>
<include>
<directory suffix=".php">src/</directory>
<directory>src/</directory>
</include>
</source>
</phpunit>
19 changes: 19 additions & 0 deletions src/InteractsWithServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ protected function assertHasShared(string ...$services): void
$this->assertHasSingletons(...$services);
}

/**
* Assert an event has registered certain listeners.
*
* @param class-string $event
* @param class-string ...$listeners
*/
protected function assertHasListeners(string $event, string ...$listeners): void
{
$list = $this->app->make('events')->getRawListeners();

static::assertNotEmpty($list[$event] ?? null, "The is no listeners registered for the [$event] event.");

foreach ($listeners as $listener) {
static::assertContains(
$listener, $list[$event], "The [$listener] listener was not registered for [$event] event.",
);
}
}

/**
* Assert that the config file is merged into the application using the given key.
*/
Expand Down
34 changes: 34 additions & 0 deletions tests/InteractsWithServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use Closure;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\SessionGuard;
use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
Expand Down Expand Up @@ -150,6 +151,39 @@ public function test_assert_singletons_fails_if_not_registered(): void
$this->assertHasSingletons('foo');
}

public function test_assert_has_listeners(): void
{
$this->app->make('events')->listen('foo', 'foo-listener');

$this->assertHasListeners('foo');
$this->assertHasListeners('foo', 'foo-listener');
}

public function test_assert_has_listeners_with_closure(): void
{
$this->app->make('events')->listen(fn (Login $event) => true);

$this->assertHasListeners(Login::class);
}

public function test_assert_has_listeners_errors_with_no_listeners(): void
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('The is no listeners registered for the [Illuminate\Auth\Events\Login] event.');

$this->assertHasListeners(Login::class);
}

public function test_assert_has_listeners_errors_with_listener_not_listed(): void
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('The [invalid] listener was not registered for [Illuminate\Auth\Events\Login] event.');

$this->app->make('events')->listen(Login::class, 'foo-listener');

$this->assertHasListeners(Login::class, 'invalid');
}

public function test_assert_merged_config(): void
{
File::expects('getRequire')->with('foo/bar.php')->andReturn(['foo' => 'bar']);
Expand Down