Skip to content

Commit

Permalink
feat(testing): Make service provider parameter in AssertProviderBindi…
Browse files Browse the repository at this point in the history
…ng/Routes null by default

BREAKING CHANGE: $registerServiceProvider is now third parameter instead of second.
  • Loading branch information
pionl committed Sep 29, 2022
1 parent 60e547b commit 0a6deb6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/Testing/Providers/Concerns/AssertProviderBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
trait AssertProviderBindings
{
/**
* @param class-string<ServiceProvider> $registerServiceProvider
* @param array<string, class-string> $expectedMap
* @param class-string<ServiceProvider>|null $registerServiceProvider
*/
public function assertBindings(
Application $application,
?string $registerServiceProvider,
array $expectedMap
array $expectedMap,
?string $registerServiceProvider = null
): void {
if ($registerServiceProvider !== null) {
$application->register($registerServiceProvider);
Expand Down
10 changes: 6 additions & 4 deletions src/Testing/Providers/Concerns/AssertProviderRegistersRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ trait AssertProviderRegistersRoutes
/**
* Asserts correct that routes are correctly registered in correct prefix (pluralized)
*
* @param class-string<AbstractServiceProvider> $registerServiceProvider
* @param bool $onlyGiven Check if only given routes can be
* @param array<string, array<string>|array<int, Closure(Route):void>> $expectUrlsByMethod
* ['GET'=>['web/tests/my-api']]
* @param bool $onlyGiven Check if only given routes can be
* @param class-string<AbstractServiceProvider>|null $registerServiceProvider
* registered
*/
public function assertRoutes(
Application $application,
string $registerServiceProvider,
array $expectUrlsByMethod,
?string $registerServiceProvider = null,
bool $onlyGiven = false
): void {
$application->register($registerServiceProvider, true);
if ($registerServiceProvider !== null) {
$application->register($registerServiceProvider, true);
}

/** @var Router $router */
$router = $application->make(Router::class);
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Health/HealthServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class HealthServiceProviderTest extends TestCase

public function testAlive(): void
{
$this->assertRoutes($this->app, HealthServiceProvider::class, [
$this->assertRoutes($this->app, [
'GET' => [
'api/health/alive' => function (Route $route) {
$this->assertEquals(['api'], $route->gatherMiddleware());
$this->assertEquals(['api'], $route->excludedMiddleware());
},
],
]);
], HealthServiceProvider::class);

$this->get('/api/health/alive')
->assertJson([
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Providers/LaraStrictServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public function testBootResolveFactory(): void

public function testBindingsFromAllServiceProviders(): void
{
$this->assertBindings($this->app, null, [
$this->assertBindings($this->app, [
RunInTransactionActionContract::class => RunInTransactionAction::class,
SafeUniqueSaveActionContract::class => SafeUniqueSaveAction::class,
GetBasePathForStubsActionContract::class => GetBasePathForStubsAction::class,
GetNamespaceForStubsActionContract::class => GetNamespaceForStubsAction::class,
]);
], null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ protected function setUp(): void

public function testBootWithWebOnly(): void
{
$this->assertRoutes($this->app, WithWebServiceProvider::class, [
$this->assertRoutes($this->app, [
'GET' => ['with-webs/3-url-web'],
], true);
], WithWebServiceProvider::class, true);
}

public function testWithoutInterface(): void
{
$this->assertRoutes($this->app, TestingServiceProvider::class, [], true);
$this->assertRoutes($this->app, [], TestingServiceProvider::class, true);
}

public function testWithoutAnyUrl(): void
Expand All @@ -61,33 +61,33 @@ public function testWithoutAnyUrl(): void

return true;
});
$this->assertRoutes($this->app, RoutableWithNoFilesServiceProvider::class, [], true);
$this->assertRoutes($this->app, [], RoutableWithNoFilesServiceProvider::class, true);
}

public function testWithApiAndWeb(): void
{
$this->assertRoutes($this->app, WithBothServiceProvider::class, [
$this->assertRoutes($this->app, [
'GET' => ['api/with-boths/2-url-api', 'with-boths/2-url-web'],
], true);
], WithBothServiceProvider::class, true);
}

public function testWithApiOnly(): void
{
$this->assertRoutes($this->app, WithApiServiceProvider::class, [
$this->assertRoutes($this->app, [
'GET' => ['api/with-apis/1-api'],
], true);
], WithApiServiceProvider::class, true);
}

public function testWithVersionedApiOnly(): void
{
$this->assertRoutes($this->app, WithVersionedApiServiceProvider::class, [
$this->assertRoutes($this->app, [
'GET' => ['api/v1/test/1-api', 'api/v2/test/2-api'],
], true);
], WithVersionedApiServiceProvider::class, true);
}

public function testWithCustomOnly(): void
{
$this->assertRoutes($this->app, WithCustomServiceProvider::class, [
$this->assertRoutes($this->app, [
'GET' => [
'with-customs/1-admin' => function (Route $route) {
$this->assertEquals(['admin'], $route->gatherMiddleware());
Expand All @@ -102,12 +102,12 @@ public function testWithCustomOnly(): void
$this->assertEquals([], $route->gatherMiddleware());
},
],
], true);
], WithCustomServiceProvider::class, true);
}

public function testWithAll(): void
{
$this->assertRoutes($this->app, WithAllServiceProvider::class, [
$this->assertRoutes($this->app, [
'GET' => [
'api/with-alls/2-url-api' => function (Route $route) {
$this->assertEquals(['api'], $route->gatherMiddleware());
Expand All @@ -128,6 +128,6 @@ public function testWithAll(): void
$this->assertEquals([], $route->gatherMiddleware());
},
],
], true);
], WithAllServiceProvider::class, true);
}
}

0 comments on commit 0a6deb6

Please sign in to comment.