jcharcosset's avatar

Test lumen package

I develop external package to be used in lumen.

In my package, I use global method config() and abort() who comes Lumen package .

I include Lumen like a dependency of my project but this classes are not initialize because I haven't bootstrap/app.php in my package.

So, PHPunit returns :

ReflectionException: Class config does not exist

I create dummy class to define this method but when I want variated the value of the config class, I need to create another config file, and another test class to inject this new config class inside.

It's not practical and I guess there's better.

Example :

    public function testCanSeeOtherUserRoles()
    {
        $user = new \UserDummy(true, [
            "superadmin" => [
                'admin'
            ]
        ]);
        
        $this->assertTrue(CheckAuthorization::canSeeOtherUserRoles($user, $user));
    }
static public function canSeeOtherUserRoles(Model $user_parent, Model $user_child)
{
    return self::roleIsParentOfDirectChild($user_parent, $user_child);
}

static public function canShowGroup(array $parent_group, string $child_group)
{
    $groupsHelper = new GroupsHelper();

    foreach ($parent_group as $group) {
        if (in_array($child_group, config('roles.roles'))) {
            return true;
        }
    }

    abort(403);
}
0 likes
4 replies
bobbybouwmann's avatar

Did you call parent::setUp in your setUp method in your test class?

jcharcosset's avatar

I extend \PHPUnit\Framework\TestCaseand I don't redefine setUp method. I still add :

 public function setUp()
    {
        parent::setUp();
    }

in my test and I same problem.

jcharcosset's avatar

Yes sure :

CheckAuthorizationTest.php :

<?php

namespace Tests\Helpers;

use Cronos\Roles\Helpers\CheckAuthorization;

require_once 'Dummies/UserDummy.php';
require_once 'Dummies/configDummy.php';

class CheckAuthorizationTest extends \PHPUnit\Framework\TestCase
{
    public function setUp()
    {
        parent::setUp();
    }

    public function testCanSeeOtherUserRoles()
    {
        $user = new \UserDummy(true, [
            "superadmin", "admin"
        ]);
        $user_2 = new \UserDummy(true, [
            "admin"
        ]);

        $this->assertTrue(CheckAuthorization::canSeeOtherUserRoles($user_2, $user));
    }
}

And my class CheckAuthorization:

<?php

namespace Cronos\Roles\Helpers;

use Illuminate\Database\Eloquent\Model;

class CheckAuthorization
{
    public static function canSeeOtherUserRoles(Model $user_parent, Model $user_child)
    {
        return self::roleIsParentOfDirectChild($user_parent, $user_child);
    }

    private static function roleIsParentOfDirectChild(Model $user_parent, Model $user_child)
    {
        $rolesHelper = new RolesHelper();

        foreach ($user_parent->roles as $role_parent) {
            foreach ($user_child->roles as $role_child) {
                if ($rolesHelper->roleIsParentOfDirectChild($role_parent->name, $role_child->name)) {
                    return true;
                }
            }
        }

        abort(403);
    }
}

I do not run tests from my Lumen app where my package is include, but I run it directly from my package.

Please or to participate in this conversation.