Skip to content

Commit d8337d7

Browse files
committed
add blade assertion
1 parent f3e6372 commit d8337d7

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

README.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This will prevent any method name conflicts with core, your custom or other trai
2424

2525
### Country
2626

27-
`composer require --dev league/iso3166`
27+
`composer require --dev league/iso3166:^3.0`
2828

2929
```php
3030
\Astrotomic\PhpunitAssertions\CountryAssertions::assertCountryName('Germany');
@@ -35,7 +35,7 @@ This will prevent any method name conflicts with core, your custom or other trai
3535

3636
### Email
3737

38-
`composer require --dev egulias/email-validator`
38+
`composer require --dev egulias/email-validator:^3.0`
3939

4040
```php
4141
\Astrotomic\PhpunitAssertions\EmailAssertions::assertValidLoose('gummibeer@astrotomic.info');
@@ -57,7 +57,7 @@ This will prevent any method name conflicts with core, your custom or other trai
5757

5858
### HashID
5959

60-
`composer require --dev hashids/hashids`
60+
`composer require --dev hashids/hashids:^4.0`
6161

6262
```php
6363
\Astrotomic\PhpunitAssertions\HashidAssertions::assertHashIds('3kTMd', 2, 'this is my salt');
@@ -76,7 +76,7 @@ This will prevent any method name conflicts with core, your custom or other trai
7676

7777
### Phone Number
7878

79-
`composer require --dev giggsey/libphonenumber-for-php`
79+
`composer require --dev giggsey/libphonenumber-for-php:^8.12`
8080

8181
```php
8282
\Astrotomic\PhpunitAssertions\PhoneNumberAssertions::assertE164('+498001110550');
@@ -107,7 +107,7 @@ This will prevent any method name conflicts with core, your custom or other trai
107107

108108
### UUID
109109

110-
`composer require --dev ramsey/uuid`
110+
`composer require --dev ramsey/uuid:^4.0`
111111

112112
```php
113113
\Astrotomic\PhpunitAssertions\UuidAssertions::assertUuid('52d08e38-ad24-4960-af02-22e0f7e0db8d');
@@ -123,7 +123,7 @@ This will prevent any method name conflicts with core, your custom or other trai
123123

124124
### HashID
125125

126-
`composer require --dev vinkla/hashids`
126+
`composer require --dev vinkla/hashids:^9.0`
127127

128128
```php
129129
\Astrotomic\PhpunitAssertions\Laravel\HashidAssertions::assertHashIds('3kTMd', 2);
@@ -134,5 +134,17 @@ This will prevent any method name conflicts with core, your custom or other trai
134134

135135
```php
136136
\Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertExists($model);
137-
\Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertSame($model, User::first());
137+
\Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertSame($model, \App\Models\User::first());
138+
```
139+
140+
### Blade
141+
142+
`composer require --dev gajus/dindent:^2.0`
143+
144+
```php
145+
\Astrotomic\PhpunitAssertions\Laravel\BladeAssertions::assertRenderEquals(
146+
"<p>Price: <code>99.99 €</code></p>",
147+
'<p>Price: <code>{{ number_format($price, 2) }} €</code></p>',
148+
['price' => 99.99]
149+
);
138150
```

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"require-dev": {
2323
"egulias/email-validator": "^2.1 || ^3.0",
24+
"gajus/dindent": "^2.0",
2425
"giggsey/libphonenumber-for-php": "^8.12",
2526
"hashids/hashids": "^4.0",
2627
"league/iso3166": "^2.1 || ^3.0",
@@ -30,6 +31,7 @@
3031
},
3132
"suggest": {
3233
"egulias/email-validator": "\\Astrotomic\\PhpunitAssertions\\EmailAssertions (^2.1 || ^3.0)",
34+
"gajus/dindent": "\\Astrotomic\\PhpunitAssertions\\Laravel\\BladeAssertions (^2.0)",
3335
"giggsey/libphonenumber-for-php": "\\Astrotomic\\PhpunitAssertions\\PhoneNumberAssertions (^8.12)",
3436
"hashids/hashids": "\\Astrotomic\\PhpunitAssertions\\HashidAssertions (^4.0)",
3537
"league/iso3166": "\\Astrotomic\\PhpunitAssertions\\CountryAssertions (^2.1 || ^3.0)",

src/Laravel/BladeAssertions.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions\Laravel;
4+
5+
use Gajus\Dindent\Indenter;
6+
use Illuminate\Support\Facades\View;
7+
use Illuminate\Support\Str;
8+
use PHPUnit\Framework\Assert as PHPUnit;
9+
10+
trait BladeAssertions
11+
{
12+
public static function assertRenderEquals(string $expected, string $template, array $data = []): void
13+
{
14+
$indenter = new Indenter();
15+
16+
PHPUnit::assertSame(
17+
$indenter->indent($expected),
18+
$indenter->indent((string) static::render($template, $data))
19+
);
20+
}
21+
22+
23+
protected static function render(string $template, array $data = []): string
24+
{
25+
$tempDirectory = sys_get_temp_dir();
26+
27+
if (! in_array($tempDirectory, View::getFinder()->getPaths())) {
28+
View::addLocation($tempDirectory);
29+
}
30+
31+
$tempFile = tempnam($tempDirectory, 'laravel-blade').'.blade.php';
32+
33+
file_put_contents($tempFile, $template);
34+
35+
return View::make(Str::before(basename($tempFile), '.blade.php'), $data)->render();
36+
}
37+
}

tests/Laravel/BladeAssertionsTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions\Tests\Laravel;
4+
5+
use Astrotomic\PhpunitAssertions\Laravel\BladeAssertions;
6+
7+
final class BladeAssertionsTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
* @dataProvider hundredTimes
12+
*/
13+
public function it_can_validate_render(): void
14+
{
15+
$price = random_int(0, 10000) / 100;
16+
17+
$string = number_format($price, 2);
18+
19+
BladeAssertions::assertRenderEquals(
20+
"<p>Price: <code>{$string} €</code></p>",
21+
'<p>Price: <code>{{ number_format($price, 2) }} €</code></p>',
22+
['price' => $price]
23+
);
24+
}
25+
}

0 commit comments

Comments
 (0)