Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble in testing authorization policy in Laravel 5.5

I'm having trouble testing authorization policy, it's showing a risky test, I don't know how to fix this. This is a newly installed laravel 5.5

PHPUnit 6.5.13 by Sebastian Bergmann and contributors.

R.                                                                  2 / 2 (100%)

Time: 99 ms, Memory: 16.00MB

There was 1 risky test:

1) Tests\Feature\ExampleTest::testBasicTest
Test code or tested code did not (only) close its own output buffers

OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 2, Risky: 1.

This is my test code:

public function testBasicTest()
{
    $this->get('/home')
        ->assertStatus(403);
}

When I use dd($this->get('/home')->getContent());, I get an error something like this..

file_get_contents([internal]): failed to open stream: No such file or directory
in Frame.php line 122

This is my home controller

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $this->authorize('create', User::class);
        return view('home');
    }
}

This is my UserPolicy.php

<?php

namespace App\Policies;

use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class UserPolicy
{
    use HandlesAuthorization;

    /**
     * Create a new policy instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function create(User $user)
    {
        return true;
    }
}

This is my AuthServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use App\User;
use App\Policies\UserPolicy;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        User::class => UserPolicy::class,
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        //
    }
}

Additional: I saw this: https://phpunit.readthedocs.io/en/7.4/risky-tests.html And I tried setting all of this to false, but the risky is still showing.

like image 786
aceraven777 Avatar asked Oct 22 '18 10:10

aceraven777


1 Answers

Manage to solve my problem by myself, I just ran composer update.

Seems that the problem is in the package filp/whoops v2.3.0, that's causing an exception. They managed to fix this in v2.3.1.

like image 142
aceraven777 Avatar answered Oct 07 '22 23:10

aceraven777