generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from tonysm/better-testing
Better Testing
- Loading branch information
Showing
10 changed files
with
542 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Tonysm\TurboLaravel\Testing; | ||
|
||
use Closure; | ||
use Illuminate\Support\Collection; | ||
use PHPUnit\Framework\Assert; | ||
|
||
class AssertableTurboStream | ||
{ | ||
/** @var Collection */ | ||
public $turboStreams; | ||
|
||
public function __construct(Collection $turboStreams) | ||
{ | ||
$this->turboStreams = $turboStreams; | ||
} | ||
|
||
public function has(int $expectedTurboStreamsCount): self | ||
{ | ||
Assert::assertCount($expectedTurboStreamsCount, $this->turboStreams); | ||
|
||
return $this; | ||
} | ||
|
||
public function hasTurboStream(Closure $callback = null): self | ||
{ | ||
$attrs = collect(); | ||
|
||
$matches = $this->turboStreams | ||
->mapInto(TurboStreamMatcher::class) | ||
->filter(function ($matcher) use ($callback, $attrs) { | ||
if (! $matcher->matches($callback)) { | ||
$attrs->add($matcher->attrs()); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
Assert::assertTrue( | ||
$matches->count() === 1, | ||
sprintf( | ||
'Expected to find a matching Turbo Stream for `%s`, but %s', | ||
$attrs->unique()->join(' '), | ||
trans_choice('{0} none was found.|[2,*] :count were found.', $matches->count()), | ||
) | ||
); | ||
|
||
return $this; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Testing/ConvertTestResponseToTurboStreamCollection.php
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,18 @@ | ||
<?php | ||
|
||
namespace Tonysm\TurboLaravel\Testing; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Testing\TestResponse; | ||
|
||
class ConvertTestResponseToTurboStreamCollection | ||
{ | ||
public function __invoke(TestResponse $response): Collection | ||
{ | ||
$parsed = simplexml_load_string(<<<XML | ||
<xml>{$response->content()}</xml> | ||
XML); | ||
|
||
return collect(json_decode(json_encode($parsed), true)['turbo-stream']); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Tonysm\TurboLaravel\Testing; | ||
|
||
use Tonysm\TurboLaravel\Turbo; | ||
|
||
/** | ||
* @mixin \Illuminate\Foundation\Testing\Concerns\MakesHttpRequests | ||
*/ | ||
trait InteractsWithTurbo | ||
{ | ||
public function turbo(): self | ||
{ | ||
return $this->withHeader('Accept', Turbo::TURBO_STREAM_FORMAT); | ||
} | ||
|
||
public function turboNative(): self | ||
{ | ||
return $this->withHeader('User-Agent', 'Turbo Native Android; Mozilla: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.3 Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/43.4'); | ||
} | ||
} |
Oops, something went wrong.