phpunit test all route (with get method and no params)

Submitted by icfr - 1 year ago

with this code you can test all get page without params (index,create,etc...)

class AllRoutesTest extends TestCase
{
    protected $admin;
    
    public function setUp()
    {
        parent::setUp();
        $this->admin      = App\User::find(1);
        

    }

    /**
     * test all route
     *
     * @group route
     */

    public function testAllRoute()
    {
        $routeCollection = Route::getRoutes();
        $this->withoutEvents();
        $blacklist = [
            'url/that/not/tested',
        ];
        $dynamicReg = "/{\\S*}/"; //used for omitting dynamic urls that have {} in uri (http://laravel-tricks.com/tricks/adding-a-sitemap-to-your-laravel-application#comment-1830836789)
        $this->be($this->admin);
        foreach ($routeCollection as $route) {
            if (!preg_match($dynamicReg, $route->getUri()) &&
                in_array('GET', $route->getMethods()) && 
                !in_array($route->getUri(), $blacklist)
            ) {
                $start = $this->microtimeFloat();
                fwrite(STDERR, print_r('test ' . $route->getUri() . "\n", true));
                $response = $this->call('GET', $route->getUri());
                $end   = $this->microtimeFloat();
                $temps = round($end - $start, 3);
                fwrite(STDERR, print_r('time: ' . $temps . "\n", true));
                $this->assertLessThan(15, $temps, "too long time for " . $route->getUri());
                $this->assertEquals(200, $response->getStatusCode(), $route->getUri() . "failed to load");

            }

        }
    }

    public function microtimeFloat()
    {
        list($usec, $asec) = explode(" ", microtime());

        return ((float) $usec + (float) $asec);

    }
}
comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks