PHP code example of simonvomeyser / laravel-automatic-tests
1. Go to this page and download the library: Download simonvomeyser/laravel-automatic-tests library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
simonvomeyser / laravel-automatic-tests example snippets
//...
public function testAllStaticPages()
{
// Crawls all pages reachable from the root of your application
// Makes sure, that all links return a response code < 400
StaticPagesTester::create()->run();
}
//...
// tests/StaticPagesTest.php
use SimonVomEyser\LaravelAutomaticTests\StaticPagesTester;
it('tests all pages reachable from the frontpage', function () {
StaticPagesTester::create()->run();
});
// tests/feature/StaticPagesTest.php
namespace Tests\Feature;
use SimonVomEyser\LaravelAutomaticTests\StaticPagesTester;
use Tests\TestCase;
class StaticPagesTest extends TestCase
{
public function testAllInternalPagesReachableOnFrontpage()
{
StaticPagesTester::create()->run();
}
}
// ...
$spt = StaticPagesTester::create()
->skipDefaultAssertion()
->run();
// access all uris that were found
// ['/', '/about', ...]
dump($spt->urisHandled)
// access all uris with their testresponses
// ['/' => $response, '/about' => $response, ...]
dump($spt->respones)
// Make own assertions
$spt->responses['/admin']->assertRedirect();
// ...
// ...
StaticPagesTester::create()
->addAssertion(function($response) {
// Example: allow anything except for 5xx errors for all uris
assertTrue($response->status() < 500);
})
->skipDefaultAssertion()
->addAssertion(function($response, $uri) {
// Example: check for redirects only when accessing admin area
if(str_contains($uri, '/admin')) {
$response->assertRedirect()
}
})
->run();
// ...