spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.77k stars 1.07k forks source link

PhpUnit: #187

Closed Fanamurov closed 8 years ago

Fanamurov commented 8 years ago

I change public directory, site work.

I create test:

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class FormContactTest extends TestCase
{
    public function testFormContactSubmit()
    {
        $this->visit('/page/kontakty');
        $this->type('Имя', 'name');
        $this->type('555-555', 'contact');
        $this->type('Комментарий', 'comment');
        $this->press('submit_contact');
        $this->seePageIs('/page/kontakty');
        $this->see('Форма отправлена');
    }
}

Run:

Caused by
exception 'Spatie\MediaLibrary\Exceptions\UrlCouldNotBeDetermined' with message 'The storage path is not part of the public path' in /Applications/MAMP_SITES/larrock.local/vendor/spatie/laravel-medialibrary/src/UrlGenerator/LocalUrlGenerator.php:19
Stack trace:
#0 /Applications/MAMP_SITES/larrock.local/vendor/spatie/laravel-medialibrary/src/Media.php(61): Spatie\MediaLibrary\UrlGenerator\LocalUrlGenerator->getUrl()

class LocalUrlGenerator:

public function getUrl()
    {
        **if (!string($this->getStoragePath())->startsWith(public_path())) {**
            throw new UrlCouldNotBeDetermined('The storage path is not part of the public path');
        }

        $url = $this->getBaseMediaDirectory().'/'.$this->getPathRelativeToRoot();

        return $this->makeCompatibleForNonUnixHosts($url);
    }

protected function getStoragePath()
    {
        $diskRootPath = $this->config->get('filesystems.disks.'.$this->media->disk.'.root');

       ** return realpath($diskRootPath);**
    }

realpath return FALSE, because $this->config->get('filesystems.disks.'.$this->media->disk.'.root') return '/Applications/MAMP_SITES/larrock.local/public/media'

but work is: /Applications/MAMP_SITES/larrock.local/public_html/media

in config/filesystems.php

'media' => [
            'driver' => 'local',
            'root'   => public_path().'/media',
        ],

in public_html/index.php

$app->bind('path.public', function() {
    return __DIR__;
});

How fix this for phpunit and changed public_path()?

freekmurze commented 8 years ago

If i understand correctly you're trying to rename the public directory to public_html right?

The cleanest way of doing this is probably by making sure public_path() returns "public_html". You can learn how do that on this thread on the Laracasts forum

Fanamurov commented 8 years ago

I just like to Laracasts, everything is working correctly, except MediaLibrary

freekmurze commented 8 years ago

Please check the path to the root of your disk in config/filesystems.php. That should be a directory inside the directory returned by public_path.

Fanamurov commented 8 years ago

I place this code in main front controller:

$diskRootPath = config('filesystems.disks.media.root');
dd(realpath($diskRootPath));

return "/Applications/MAMP_SITES/laravel.local/larrock/public_html/media" (work good)

start phpunit:

return FALSE $diskRootPath = "/Applications/MAMP_SITES/laravel.local/larrock/public/media"

freekmurze commented 8 years ago

what is the content of config/filesystems.php

and does public_path return the right path when running unit tests?

Fanamurov commented 8 years ago

Sorry, here is the solution to the problem of tests

in config/filesystems.php

'media' => [
            'driver' => 'local',
            'root'   => public_path().'/media',
        ],

change to

'media' => [
            'driver' => 'local',
            'root'   => base_path() .'/public_html/media',
        ],

Thank you