Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Anonymous194's avatar

PhP Unit, risky test? What's the cause of it and how to avoid

Hi,

I followed documentation for laravel php unit tests on laravel website. However, the tests I have give me this output:

PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

RRRRRR                                                              6 / 6 (100%)

Time: 8.53 seconds, Memory: 16.00MB

There were 6 risky tests:

1) Tests\Unit\ResponseTest::testHomePage
Test code or tested code did not (only) close its own output buffers

2) Tests\Unit\ResponseTest::testMyProfile
Test code or tested code did not (only) close its own output buffers

3) Tests\Unit\ResponseTest::testEditProfile
Test code or tested code did not (only) close its own output buffers

4) Tests\Unit\ResponseTest::testMyPages
Test code or tested code did not (only) close its own output buffers

5) Tests\Unit\ResponseTest::testCreateEntity
Test code or tested code did not (only) close its own output buffers

6) Tests\Unit\ResponseTest::testEditEntity
Test code or tested code did not (only) close its own output buffers

OK, but incomplete, skipped, or risky tests!
Tests: 6, Assertions: 5, Risky: 6.

Here's my test:

<?php

namespace Tests\Unit;

use Tests\TestCase;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ResponseTest extends TestCase
{
    public function testHomePage()
    {
        $user = User::find(14);
        $response = $this->actingAs($user)
                                    ->get('/');
        $response->assertSuccessful();
    }
    public function testMyProfile()
    {
        $user = User::find(14);
        $response = $this->actingAs($user)
                                    ->get('/profile');
        $response->assertSuccessful();
    }
    public function testEditProfile()
    {
        $user = User::find(14);
        $response = $this->actingAs($user)
                                    ->get('/profile/edit');
        $response->assertSuccessful();
    }
    public function testMyPages()
    {
        $user = User::find(14);
        $response = $this->actingAs($user)
                                    ->get('/profile/entity');
        $response->assertSuccessful();
    }
    public function testCreateEntity()
    {
        $user = User::find(14);
        $response = $this->actingAs($user)
                                    ->get('/profile/entity/create');
        $response->assertSuccessful();
    }
    public function testEditEntity()
    {
        $user = User::find(14);
        $response = $this->actingAs($user)
                                    ->get('/profile/entity/edit/104');
        $response->assertSuccessful();
    }
}

Now the problem is what is causing this issue? I can't see anything wrong with my code

master.blade

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <title>@yield('title')</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="theme-color" content="#e62c56">
    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <!-- CSS Styles -->
    <link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet">
    <link href="{{ mix ('css/bootstrap.css')}}" rel="stylesheet" type="text/css">
    <link href="{{ mix ('css/style.css')}}" rel="stylesheet" type="text/css">
    <link href="{{ mix ('css/global.css')}}" rel="stylesheet" type="text/css">
    <link href="{{ mix ('css/navigation.css')}}" rel="stylesheet" type="text/css">
    <link href="{{ mix ('css/footer.css')}}" rel="stylesheet" type="text/css">
    <link href="{{ mix ('css/cards.css')}}" rel="stylesheet" type="text/css">

    <!-- JavaScript Scripts -->
    <script type="text/javascript" src="{{ mix ('js/global.js') }}"></script>
    @yield('extrafiles')
    <script async src="https://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.min.js"></script>
    <script async src="https://cdn.jsdelivr.net/autocomplete.js/0/autocomplete.min.js"></script>
    <script async src="https://cdn.jsdelivr.net/algoliasearch.helper/2/algoliasearch.helper.min.js"></script>
    <script  src="https://cdn.jsdelivr.net/npm/instantsearch.js@1/dist/instantsearch.min.js"></script>

    <!-- Favicon Icons -->
    <link rel="apple-touch-icon" sizes="180x180" href="https://s3.eu-west-2.amazonaws.com/liveandnow-production/public/img/apple-touch-icon.jpg">
    <link rel="icon" type="image/png" sizes="32x32" href="https://s3.eu-west-2.amazonaws.com/liveandnow-production/public/img/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="https://s3.eu-west-2.amazonaws.com/liveandnow-production/public/img/favicon-16x16.png">
    <link rel="manifest" href="https://s3.eu-west-2.amazonaws.com/liveandnow-production/public/img/manifest.json">
    <link rel="shortcut icon" href="https://s3.eu-west-2.amazonaws.com/liveandnow-production/public/img/favicon.ico">
    <meta name="msapplication-config" content="https://s3.eu-west-2.amazonaws.com/liveandnow-production/public/img/browserconfig.xml">
    <meta name="theme-color" content="#ffffff">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="msapplication-TileImage" content="https://s3.eu-west-2.amazonaws.com/liveandnow-production/public/img/ms-icon-144x144.png">
</head>

    <body class="loading">
    @include('partials._googleanalytics')
    <!-- Loading Gif -->
    <div class="se-pre-con"></div>
    <div class="pace pace-inactive">
        <div class="pace-progress" data-progress-text="100%" data-progress="99" style="width: 100%;">
            <div class="pace-progress-inner"></div>
        </div>
        <div class="pace-activity"></div>
    </div>
        <!-- sidebar -->
        <div class="sidebar" id="sidebar" role="navigation">
            @include('_mainNavigation')
        </div>

    @include('partials.errors')
    <div class="content-wrapper">
    @yield('content')
    </div>
    @include('_bottomNav')
    <div  class="prompt flex-va" id="cookie-prompt">
        <div class="col-xs-10">
            <span>
            <p>This site uses cookies to offer you a better browsing experience. By continuing to use this site you are agreeing to the use and storage of these cookies. Find out more <a href="/cookies">here</a> </p>
            </span>
        </div>
        <button class="prompt-button" id="cookie-prompt-close">Ok</button>
    </div>
</div>
    </body>
</html>

editProfile.blade (Used in editprofile test for example)

@extends('master') @section('title', 'Live Oldham')
@section('content')
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Edit Profile</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ action('UpdateController@updateUser') }}">
                        {{ method_field('PATCH') }}
                        {{ csrf_field() }}
                        <input type="hidden" name="profile_id" value="{{ $user->id }}">
                        <input type="hidden" name="deleted" value="deleted">
                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label for="name" class="col-md-4 control-label">Name</label>
                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control" name="name" placeholder="{{ $user->name }}" value="{{ $user->name or old('name') }}">
                            @if ($errors->has('name'))
                            <span class="help-block">
                                <strong>{{ $errors->first('name') }}</strong>
                            </span>
                            @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label for="email" class="col-md-4 control-label">Email</label>
                            <div class="col-md-6">
                                <input id="email" type="text" class="form-control" name="email" placeholder="{{ $user->email }}" value="{{ $user->email or old('email') }}">
                            @if ($errors->has('email'))
                                <span class="help-block">
                            <strong>{{ $errors->first('email') }}</strong>
                            </span>
                            @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('current_password') ? ' has-error' : '' }}">
                            <label for="password" class="col-md-4 control-label">Current Password</label>
                            <div class="col-md-6">
                            <input id="current_password" type="password" class="form-control" name="current_password">
                            @if ($errors->has('current_password'))
                                <span class="help-block">
                            <strong>{{ $errors->first('current_password') }}</strong>
                            </span>
                            @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label for="password" class="col-md-4 control-label"> New Password</label>
                            <div class="col-md-6">
                            <input id="password" type="password" class="form-control" name="password">
                            @if ($errors->has('password'))
                                <span class="help-block">
                            <strong>{{ $errors->first('password') }}</strong>
                            </span>
                            @endif
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
                        <div class="col-md-6">
                            <input id="password-confirm" type="password" class="form-control" name="password_confirmation">
                        </div>
                        </div>
                        <div class="form-group{{ $errors->has('telephone') ? ' has-error' : '' }}">
                            <label for="telephone" class="col-md-4 control-label">Telephone</label>
                            <div class="col-md-6">
                                <input id="telephone" type="text" class="form-control" name="telephone" placeholder="{{ $user->telephone }}" value="{{ $user->telephone or old('telephone') }}">
                            @if ($errors->has('telephone'))
                            <span class="help-block">
                                <strong>{{ $errors->first('telephone') }}</strong>
                            </span>
                            @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('twitter') ? ' has-error' : '' }}">
                            <label for="twitter" class="col-md-4 control-label">Twitter</label>
                            <div class="col-md-6">
                                <input id="twitterp" type="text" class="form-control" name="twitter" placeholder="{{ $user->twitter_personal }}" value="{{ $user->twitter_personal or old('twitter_personal') }}">
                            @if ($errors->has('twitter'))
                            <span class="help-block">
                                <strong>{{ $errors->first('twitter') }}</strong>
                            </span>
                            @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('facebook') ? ' has-error' : '' }}">
                            <label for="facebook" class="col-md-4 control-label">Facebook</label>
                            <div class="col-md-6">
                                <input id="facebookp" type="text" class="form-control" name="facebook" placeholder="{{ $user->facebook_personal }}" value="{{ $user->facebook_personal or old('facebook_personal') }}">
                            @if ($errors->has('facebook'))
                            <span class="help-block">
                                <strong>{{ $errors->first('facebook') }}</strong>
                            </span>
                            @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('instagram') ? ' has-error' : '' }}">
                            <label for="instagram" class="col-md-4 control-label">Instagram</label>
                            <div class="col-md-6">
                                <input id="instagramp" type="text" class="form-control" name="instagram" placeholder="{{ $user->instagram_personal }}" value="{{ $user->instagram_personal or old('instagram_personal') }}">
                            @if ($errors->has('instagram'))
                            <span class="help-block">
                                <strong>{{ $errors->first('instagram') }}</strong>
                            </span>
                            @endif
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                            <button type="submit" class="btn btn-warning">
                                <span class="fa fa-pencil-square-o">
                                </span>
                                Update Profile
                            </button>
                            <button type="button" id="modal-open" data-toggle="modal" data-target="#profileModal" class="btn btn-danger pull-right">
                                <span class="fa fa-trash">
                                </span>
                                Delete Profile
                            </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@include('modals.userModal')
@endsection
0 likes
9 replies
tykus's avatar

Can share any one of the Blade templates (if applicable)?

tykus's avatar

You never ended this section @section('title', 'Live Oldham'), give that a try?

1 like
Anonymous194's avatar

I did, I get 500 @tykus I never ever had to close that section and if I did I get error

tykus's avatar
tykus
Best Answer
Level 104

Hmmm.... I suspect it might be template related, but I might be wrong. For example, it could be a problem with the ternary operators - try wrapping the entire ternary in parentheses.

For a sanity check (if this hasn't worked) I would remove all markup in one of the templates and see if that test passes to isolate the issue. Then bring back sections of the markup until the test fails again.

1 like
Anonymous194's avatar

@tykus you were right, my master blade has partials.errors and _mainNavigation included, both of those files didn't have @endsection. After doing that it's all fine. Quite clever I wonder how it does that

lemaur's avatar

Dear @tykus and @Anonymous194,

I had the same problem and it seems that we need to close the section only if we pass a string value or a result of a function.

See below: @section('title', $post->title) @section('image', $post->cover()) @endsection @section('og_type', 'article') @endsection

But I would like to understand why this happens.

Do you have an idea?

Thanks in advance.

1 like

Please or to participate in this conversation.