Laravel 4 phpunit test: the test is marked as "risky" in order not to close its own output buffers

I had a problem while trying to test a simple Illuminate \ Http \ Response object inside a phpunit test. This error:

PHPUnit 4.3-dev by Sebastian Bergmann. Configuration read from /var/MyApp/phpunit.xml ....................R................. Time: 2.71 seconds, Memory: 76.75Mb OK, but incomplete, skipped, or risky tests! Tests: 38, Assertions: 55, Risky: 1. 

where R is displayed when running phpunit --tap as

 # RISKY Test code or tested code did not (only) close its own output buffers 

The class I want to check is:

 class PrettyError { /** * Renders a pretty 500 error page view * * @return string with error view */ public function render() { return Response::make(View::make('viewName')->with('param','value')->render(), 500, array()); } } 

and test

 class Pretty500Test extends BaseTest { /** @var Pretty500 */ private $pretty500; /** * Set dependencies */ public function __construct() { $this->pretty500 = app(PrettyError::class); } public function testRender() { $response = $this->pretty500->render(); //assertions below, never get reached cause tests aborts after above call $this->assertsTrue('500',$response->getStatus()); } } 

Please note that the same error occurs when testing any route in my application using laravel

this->route('GET','routeName')

so this is apparently a common error related to requesting a response. As far as I can tell, there is nothing in my application that would do something scared with output buffering (purposefully), and this is the only thing I can think of that can ruin the standard Response class work.

I am using phpunit 4.3-dev, laravel 4.2.1 and mocking tests.

I did not find much on the Internet in terms of this particular risky error code, so I am not okay without additional help.

+8
phpunit laravel laravel-4
source share
5 answers

Invalid private ternary operator (?)

I had the same problem with a risky test when testing in Laravel 5. The problem was that in my navigation (default layout file) I had code similar to the following:

 <li{{ Request::is('clients*')?' class="active"':'' }}><a href="/clients">Clients</a></li> 

The test did not like this section because the ternary operator ( ? ) Was not closed enough. I replaced it with the following to make it work (brackets around the ternary operator section added):

 <li{{ (Request::is('clients*')?' class="active"':'') }}><a href="/clients">Clients</a></li> 

I found this by removing some of the blade syntax in parts from the requested view, the expanded layout, and the files that were included. At the time the syntax was removed above, it worked, so an error occurred.

Empty exit / section

The same error occurred when I passed an empty value for the lesson, for example, in the following scenario:

layout.blade.php

 <h1>@yield('title')</h1> 

page.blade.php

 @extends('layout') @section('title','') //<-- This empty value raised the error 

Other things to consider

It may be understandable, but just in case you made the same mistake: you should check all involved files.

I performed the following test, which visits the login page, enters the email and password and presses the login button:

Tests /AuthenticationTest.php

 <?php class AuthenticationTest extends TestCase { // [...] public function testLoginSuccessful() { $password = str_random(); $user = factory(App\User::class)->create(['password' => $password]); $this->visit(route('login')) ->type($user->email, 'email') ->type($password, 'password') ->press('Login') ->seePageIs(route('dashboard2')); } } 

application /Http/routes.php

 <?php Route::get('auth/login', 'Auth\AuthController@getLogin')->name('login'); Route::post('auth/login', 'Auth\AuthController@postLogin'); 

application / Http / Controllers / Auth / AuthController.php

 <?php // [...] class AuthController extends Controller { // [...] protected $redirectPath = '/dashboard2'; // <-- check this file as well } 

Therefore, I had to especially carefully check the following files, where I made changes:

  • application /Http/routes.php
  • app / Http / Controllers / Auth / AuthController.php -> getLogin function
  • resources /views/auth/login.blade.php (returned to Auth \ AuthContoller @getLogin)
  • app / Http / Controllers / Auth / AuthController.php -> postLogin function
  • resources / views / dashboard 2.blade.php (returned on successful login)
+8
source share

Occurs also in Laravel 5.
PHPUnit will throw this if the response format is incorrect.

This may be due to:

  • Laravel Application Errors: Check Logs
  • Blade syntax is incorrect: for example, in my case there was excessive Blade @stop , which was not reviewed by Laravel (and was not registered)
+6
source share

It turns out that the problem is related to the view in question, using

Input::old()

inside the form. Having tried just to access Input::old('foo') inside any phpunit test, I got

RuntimeException: Session store not set on request.

So the real culprit was in the session repository in my unit tests, instead of any presentation / presentation problem. The problem was solved by setting up the session store for the current request instance inside the testRender () method:

\Illuminate\Support\Facades\Request::setSession($this->app['session.store'])

+1
source share

I have the same error message from PhpUnit you like. I wrote "@stop" in the file with the wrong line (blade). I edited by moving the right "@stop" button to the right line and than the PHPUnit tests are passed!

Try checking your @section @stop block in your blade files.

+1
source share

I had a very similar problem. The problem was that I tried to get the non object property (NULL) in the view, but instead of the PHP error "Trying to get the non-object property," phpunit showed "OK, but incomplete, missed or risky tests!" ..

So, if you have the same problem, check for errors in the view file.

0
source share

All Articles