Black
Black

Reputation: 20212

Php uses other class than specified

I am experiencing a very weird issue.

I have two classes, one is in App\Projects\github\Units\github_login and the other is App\Projects\github\Schema\github_login

The Schema file is just a scheme which shows the skeletal structure of the file App\Projects\github\Units\github_login.

In fact, it should never ever get loaded and executed, it is just for humans.

However, PHP always executes this file instead of App\Projects\github\Units\github_login

namespace App\Http\Controllers;

use App\Models\Selenium;
use App\Models\Project;
use App\Projects\github\Units\github_login;

class PlayController extends BasePlayController
{
    private $github_login;

    function __construct()
    {
        parent::__construct();
        $this->github_login = new github_login();
    }

    /**
    * Play a testcase X times.
    *
    * @param string $Project
    * @param string $Unit
    * @param string $Scenario
    * @param string $Testcase
    * @param int $Times
    * @param int $Delay
    * @return /Illuminate/View/View
    */
    public function PlayCase($Project, $Unit, $Scenario, $Testcase, $Times=1, $Delay=3)
    {
        $SeleniumObj = $this->Selenium;

        $Unit =  $Project .'_'. $Unit;

        switch ($Project) {
            case 'github':
                switch ($Unit) {
                    case 'github_login':
                        switch ($Scenario) {
                            case 'standard':
                                 $this->callCase(array($this->github_login, 'standard'), $Testcase, $Times, $Delay);
                                break;
                            default:
                                $SeleniumObj->logMessage[$SeleniumObj->index]['TestcaseTitle'] = $Scenario;
                                $SeleniumObj->setMessage(2,"Das Szenario '<strong>". $Scenario ."</strong>' existiert nicht!", '');
                        }
                        break;
                }
                break;
            default:
                $SeleniumObj->setTitle('Projekt existiert nicht.');
                $SeleniumObj->setMessage(2,'Das Projekt <strong>'. $Project .'</strong> existiert nicht. (Fix: einfach neuen Testfall anlegen)', '');
        }

        return view('show.report')->with('driver',        $SeleniumObj->driver)
                                  ->with('ScenarioName',  $Scenario)
                                  ->with('logMessage',    $SeleniumObj->logMessage)
                                  ->with('execScriptRet', $SeleniumObj->execScriptRet)
                                  ->with('screenshot',    $SeleniumObj->screenshot)
                                  ->with('PlayAll',       false);
    }

As you can see in the screenshot, it even loads the right one, but how on earth does it execute the one in folder Schema?

enter image description here

I am making selenium tests btw and use the github login page for start.

UPDATE:

BasePlayController - callCase:

namespace App\Http\Controllers;

use App\Models\Selenium;

class BasePlayController
{
    public $Selenium;

    function __construct()
    {
        $this->Selenium = new Selenium();
    }

    public function callCase($thisFunction, $Testcase, $Times=1, $Delay=1)
    {
        $SeleniumObj = $this->Selenium;

        if (is_numeric($Times)) {
            for($i = 1; $i <= $Times; $i++) {

                $thisFunction($SeleniumObj, $Testcase);
                sleep($Delay);
            }
        } else {
            $SeleniumObj->logMessage[$SeleniumObj->index]['TestcaseTitle'] = 'Falscher Parameter für Times.';
            $SeleniumObj->setErrorMessage("Times muss eine Zahl sein! (Gegeben: '<strong>". $Times ."</strong>')", '');
        }
    }

 ...

UPDATE 2

Autloader Entry in vendor\composer\autoload_classmap.php

'App\\Projects\\github\\Units\\github_login' => $baseDir . '/app/Projects/github/Schema/github_login.php',

I tried to change it to

'App\\Projects\\github\\Units\\github_login' => $baseDir . '/app/Projects/github/Units/github_login.php',

but it made no difference.

Upvotes: 1

Views: 68

Answers (2)

lagbox
lagbox

Reputation: 50491

Assuming you have 2 files declaring the same class. Composer is scanning a directory finding classes and classmapping them to their location. It just happens to be since there can only be one by key that its pointing the the duplicate class in Schema. Would be my quick thoughts without digging into the internals.

The file says it is in X namespace and the classname is Y ... that is what it is regardless of where it physically exists.

I guess you can not have 2 files declaring the same class like that (anywhere in a folder that gets classmapped), or make the duplicate not a valid php file for use, maybe making it .phps. ... and redump the autoload.

Upvotes: 1

Black
Black

Reputation: 20212

I found the cause. I had to change the autoloader entry in vendor\composer\autoload_static.php from

'App\\Projects\\github\\Units\\github_login' => __DIR__ . '/../..' . '/app/Projects/github/Schema/github_login.php',

to

'App\\Projects\\github\\Units\\github_login' => __DIR__ . '/../..' . '/app/Projects/github/Units/github_login.php',

But I wonder why I have to do this manually.

Upvotes: 0

Related Questions