jeroenvanrensen's avatar

Laravel autoload fake storage files as classes

Hi everyone,

I'm creating a package where users can place files in the App\Resources directory.

I want to test if a file there does something.

I'm using the Storage::fake() method for saving classes, but how can I load them?

Thank you! Jeroen

0 likes
8 replies
Sinnbeck's avatar

Maybe try explaining why you want to use storage? Why not just place a class inside the package app/Resources directory and use that for testing?

jeroenvanrensen's avatar

Hi @sinnbeck,

That's possible too, but how can I get that working?

Currently this is my test:

<?php

namespace JeroenvanRensen\MoonPHP\Tests\Resources;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use JeroenvanRensen\MoonPHP\Tests\TestCase;

class ResourceHelpersTest extends TestCase
{
    use RefreshDatabase;

public $model = <<<EOD
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    // 
}
EOD;

public $resource = <<<EOD
<?php

namespace App\Resources;

use JeroenvanRensen\MoonPHP\Resource;
use App\Models\Page;

class Page extends Resource
{
    public $model = PageModel::class;
}
EOD;

    /** @test */
    public function the_resource_class_can_find_all_the_resources()
    {
        $this->withoutExceptionHandling();

        Config::set('filesystems.disks.local.root', app_path());

        Storage::disk('local')->put('Models/Page.php', $this->model);
        Storage::disk('local')->put('Resources/Page.php', $this->resource);

        $resource = \App\Resources\Page;
    }
}

But of course the \App\Resources\Page class is not found.

Sinnbeck's avatar

Make those two classes as actual files that can be used for testing. How are you expecting to load them in your package? Do you just need to load the all classes in a directory to parse or?

jeroenvanrensen's avatar

@sinnbeck I want the user to publish a config file, and there all the classes as resources.

Like this:

<?php

use App\Resources\Page;

return [
    'resources' => [
        Page::class
    ]
];
Sinnbeck's avatar

Ah ok, so some console command to extract all classes from a folder and put them in a php file?

Please or to participate in this conversation.