Skip to content

Commit 543e869

Browse files
docs: improve testing docs and provide a pest example (#947)
1 parent f01be32 commit 543e869

File tree

1 file changed

+60
-15
lines changed

1 file changed

+60
-15
lines changed

README.md

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -589,32 +589,77 @@ To localize your post url see the example in [POST is not working](#post-is-not-
589589

590590
## Testing
591591

592-
During the test setup, the called route is not yet known. This means no language can be set.
593-
When a request is made during a test, this results in a 404 - without the prefix set the localized route does not seem to exist.
592+
In a typical request lifecycle, your application is bootstrapped automatically — allowing this package to detect the active route and set the appropriate locale.
593+
However, when running tests, the application is bootstrapped before any request is made. As a result, the package can’t determine the current route, which often leads to a `404` error.
594594

595-
To fix this, you can use this function to manually set the language prefix:
595+
To handle this, you can manually define the locale prefix in your tests by refreshing the application with a specific locale:
596+
597+
### PHPUnit
596598
```php
597-
// TestCase.php
598-
protected function refreshApplicationWithLocale($locale)
599+
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
600+
use Mcamara\LaravelLocalization\LaravelLocalization;
601+
602+
abstract class TestCase extends BaseTestCase
599603
{
600-
self::tearDown();
601-
putenv(LaravelLocalization::ENV_ROUTE_KEY . '=' . $locale);
602-
self::setUp();
604+
protected function refreshApplicationWithLocale(string $locale): void
605+
{
606+
self::tearDown();
607+
putenv(LaravelLocalization::ENV_ROUTE_KEY . '=' . $locale);
608+
self::setUp();
609+
}
610+
611+
protected function tearDown(): void
612+
{
613+
putenv(LaravelLocalization::ENV_ROUTE_KEY);
614+
parent::tearDown();
615+
}
603616
}
617+
```
604618

605-
protected function tearDown(): void
619+
```php
620+
final class HomeControllerTest extends TestCase
606621
{
607-
putenv(LaravelLocalization::ENV_ROUTE_KEY);
608-
parent::tearDown();
622+
public function it_can_visit_the_home_page()
623+
{
624+
$this->refreshApplicationWithLocale('en');
625+
626+
$response = $this->get('/en');
627+
628+
$response->assertStatus(200);
629+
}
609630
}
631+
```
610632

611-
// YourTest.php
612-
public function testBasicTest()
633+
### Pest
634+
```php
635+
// Pest.php
636+
use Mcamara\LaravelLocalization\LaravelLocalization;
637+
638+
function refreshApplicationWithLocale(string $locale): void
613639
{
614-
$this->refreshApplicationWithLocale('en');
615-
// Testing code
640+
/** @var \Tests\TestCase $test */
641+
$test = test();
642+
643+
$test->tearDown();
644+
putenv(LaravelLocalization::ENV_ROUTE_KEY . '=' . $locale);
645+
$test->setUp();
616646
}
647+
648+
pest()->afterEach(function () {
649+
putenv(LaravelLocalization::ENV_ROUTE_KEY);
650+
});
617651
```
652+
```php
653+
// YourTest.php
654+
test('it can visit the home page', function () {
655+
refreshApplicationWithLocale('en');
656+
657+
$response = $this->get('/en');
658+
659+
$response->assertStatus(200);
660+
});
661+
```
662+
618663

619664
## Collaborators
620665
- [Adam Nielsen (iwasherefirst2)](https://github.com/iwasherefirst2)

0 commit comments

Comments
 (0)