jerlandsson's avatar

file exists Condition fails in test, works in manual test

Hello fellow coders.

I'm having a nasty problem where my tests fail when I try to load a "module". But when I try the API manually it works just fine. I've spend at least a couple of hours on this now so I thought that I should ask if someone else could be so nice and "throw an eye" (as we say in Sweden) on this to help me get pass this.

I'm trying to load a file from a //src/config/.php which contains an array that I parse in a later stage.

First off - ModulesTest.php

public function test_installing_a_module_without_activating_it()
    {
        $this->json('POST', $this->testApiPath, [
                'name'     => 'anotherFakeModule',
                'version'  => '1.0',
                'vendor'   => 'Douche'
            ])->seeStatusCode(201);
        $notInstalledModules = $this->get('/modules/scan')->getResult();
        $module = Module::where('name', '=', 'anotherFakeModule')->first();

        $this->assertFalse(in_array('anotherFakeModule', $notInstalledModules));
        $this->assertFalse($module->active);
    }

ModuleConfig.php:

    public function __construct($name, $vendor)
    {
        $this->fs   = new Filesystem();
        $this->name = $name;
        $this->vendor = $vendor;

        $this->load();
    }

    /**
     * Construct the objects fields.
     * @return void
     */
    private function load()
    {
        try {
            $configFile = $this->getConfig();
            if ( ! $this->fs->exists($configFile)) {
                throw new FileNotFoundException('The file (' . $configFile . ') does not exists. Got "vendor" - '.$this->vendor.' and "name" - '.$this->name);
            }

            $this->config = collect($this->fs->getRequire($this->getConfig()));
            $this->validateConfig();

            foreach ($this->config as $name => $value) {
                $this->data[$name] = $value;
            }
            $this->data['path'] = $this->modulePath($this->name);
        } catch (FileNotFoundException $e) {
            echo $e->getMessage();
            exit;
        } catch (InvalidArgumentException $e) {
            echo $e->getMessage();
            exit;
        }
    }

    /**
     * @return String path to current module
     */
    public function modulePath()
    {
        return realpath(base_path('..'.DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR.$this->vendor.DIRECTORY_SEPARATOR.$this->name.DIRECTORY_SEPARATOR.'src'));
    }

    /**
     * Retrieve the path to the config file.
     * @return null|string Path to config
     */
    public function getConfig()
    {
        $path = (! empty($this->name) && ! empty($this->vendor))
            ? $this->modulePath().DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR. $this->name . '.php'
            : null;

        return $path;
    }

When I walk through the code manually with xDebug I get a correct value on $this->vendor and $this->name so I can locate the config file and retrieve its data.

However - when I try to do it via the test, I receive:

Starting test 'ModulesTest::test_installing_a_module_without_activating_it'.
The file () does not exists. Got "vendor" -  and "name" -

Ok, so I think that something's wrong with my ModuleConfig class. But it's not. Because when I debug the load method like this:

 /**
     * Construct the objects fields.
     * @return void
     */
    private function load()
    {
        try {
            $configFile = $this->getConfig();
            var_dump($configFile);
            var_dump( $this->fs->exists($configFile));
            die;
            if ( ! $this->fs->exists($configFile)) {
                // Omitted code
        }
    }

I receive:

Starting test 'ModulesTest::test_installing_a_module_without_activating_it'.
string(87) "/Users/myUSer/code/myProject/modules/Douche/anotherFakeModule/src/config/anotherFakeModule.php"
bool(true)

I don't know where to look. Because it's only the test that fails. Everything else works, and on other modules as well.

0 likes
0 replies

Please or to participate in this conversation.