Skip to content

Commit

Permalink
feat(testing): Add AssertProviderBindings for testing service provide…
Browse files Browse the repository at this point in the history
…r bindings
  • Loading branch information
pionl committed Sep 28, 2022
1 parent 8b7201c commit 9733d67
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Testing/AbstractExpectationCallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use LogicException;

/**
* @template T of object
* @template TExpectation of object
*/
abstract class AbstractExpectationCallMap
{
/**
* @var array<T>
* @var array<TExpectation>
*/
private array $expectationMap = [];

/**
* @param array<T|null> $expectationMap
* @param array<TExpectation|null> $expectationMap
*/
public function __construct(
array $expectationMap,
Expand All @@ -28,7 +28,7 @@ public function __construct(
}

/**
* @param T $expectation
* @param TExpectation $expectation
*
* @return $this
*/
Expand All @@ -42,9 +42,9 @@ public function addExpectation(object $expectation): self
/**
* Resets the current expectation map
*
* @param array<T> $expectationMap
* @param array<TExpectation> $expectationMap
*
* @return static<T>
* @return static<TExpectation>
*/
public function setExpectationMap(array $expectationMap): self
{
Expand All @@ -55,7 +55,7 @@ public function setExpectationMap(array $expectationMap): self
}

/**
* @return T
* @return TExpectation
*/
protected function getExpectation(): object
{
Expand Down
32 changes: 32 additions & 0 deletions src/Testing/Providers/Concerns/AssertProviderBindings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Testing\Providers\Concerns;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use PHPUnit\Framework\Assert;

trait AssertProviderBindings
{
/**
* @param class-string<ServiceProvider> $registerServiceProvider
* @param array<string, class-string> $expectedMap
*/
public function assertBindings(
Application $application,
?string $registerServiceProvider,
array $expectedMap
): void {
if ($registerServiceProvider !== null) {
$application->register($registerServiceProvider);
}

foreach ($expectedMap as $binding => $expectedClass) {
$object = $application->make($binding);

Assert::assertInstanceOf($expectedClass, $object);
}
}
}

0 comments on commit 9733d67

Please sign in to comment.