Storage not building the right file url in test? - laravel

I'm writing tests for my application and struggling to understand whi Storage class is not building the expected file url.
I'm having thist test that fails on asserting that expected url equals the builded one, so i put an if in my code and dd() stuff out to understand, and that's the result:
IF in my test
if('http://testurl/storage/path/to/file.test' !=
Storage::disk('default_disk')->url('path/to/file.test')){
dd(
config('filesystems.disks.default_disk'),
Storage::disk('default_disk')->url('path/to/file.test'),
);
}
Output
> php vendor/phpunit/phpunit/phpunit
PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
Runtime: PHP 7.4.3
Configuration: C:\Users\silvi\Desktop\RawFile\phpunit.xml
array:3 [
"driver" => "local"
"root" => "root/"
"url" => "http://testurl/storage"
]
"/storage/path/to/file.test"
Script php vendor/phpunit/phpunit/phpunit handling the test event returned with error code 1
Considerations
So in my test the disk configuration is OK, but the Storage class build the wrong url like it is ignoring the url config section.
Don't think this matters, but (obviously) the storage is fake, made with Storage::fake('default_disk'); in the setUp() method.
Laravel
I'm writing tests for a Laravel package, and used "orchestra/testbench": "^6.2" as dev dependancy, so according to their docs it brings up a Laravel 6 application skeleton.
Update
I tried removing the Storage::fake('default_disk'); and the test passed, having the Storage correctly using my configs.
As gbalduzzi said, if the storage is fake it ignores whatever is in my configs.

When you use fake() to create a disk, Laravel will not consider anymore your configuration from the usual file config/filesystems.php.
It will create a custom, fake, disk.
To solve your problem, you should use the actual disk without creating it using fake(). This is not ideal though, because running your tests will actually add files into the selected storage.
The fact is, you should not be testing that the Storage facades builds the proper url. Your tests should focus on the code written by YOU. It is laravel job to properly test their interfaces/methods/facades.

Related

How in custom package data checking with making plugin unavailable in case of error?

Making custom package with custom data kept in migration/seeder files on laravel 9 site and reading docs https://laravel.com/docs/9.x/packages
I did not find in which way I can run some data checking on plugin installation ?
In file packages/companyname/Mypackage/src/Providers/MypackageProvider.php
I have loaded migration files :
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
I have a custom class, running which I got list of errors in case of data in related tables have logical errors.
I think to make functionality to run this checking process from dashboard of my site
Also I would like in case of such errors to show some errors when I run this checking process manually and maybe (if that is possible)
when I run commands in console of the site :
composer install mypackage
composer dump-autoload
and migrating migration/seeder files of this plugin...
Also is cases of logical errors I would like in the app make a condition like :
if(class_exists(mypackage::class)) {
and make functionality of this plugin unavailable...
How that can be implemented ?
"laravel/framework": "^9.41"
Thanks in advance!
If I understand what you want to do is check some conditions on the user app before your package may be used, but I think you can check these conditions before the user install the package, executing an script before installing the package using a composer script pre-install-cmd. After installing your package you can use the boot method of your service provider and execute all those conditions that have to be meet before using the functionality and abort if they are not presents.
Hope this help.

cannot access Component controller variables inside the component view

I have been running into this very weird issue with Laravel.
I had a problem where one of my component views was not able to read the variables defined in its class. It was kind of strange because I have several components running in my project and they all worked fine, except for this one.
So I created a fresh Laravel project to test some things out (Wanted to check if the problem was on my end, maybe I somehow messed up the project files).
I created a new component on a blank project using php artisan make:component top_nav
pre function basically is used as print_r which is in helper.php
Then I simply added a sql_data variable to the class component like so:
i tried many thing as much as i can do but still i can't access that variable
also clear cache of view
of laravel
change name of components but still can't work
kindly help me..........
you should
return view('components.top_nav', ['sql_data' => $sql_data]);
you are not passing the variable to the view
composer install
npm install && npm run dev
php artisan optimize
I just copied your screenshots into a new laravel installation and ran it with no problems.
Try setting the public property with a default value:
public $sql_data = 'testing'
If you try the above and still have issues, I'd like to confirm the output of AdminLogin::all().
PS:
TopNav instead of top_nav Class name please.

Laravel Vapor not using with the updated build

I'm deploying a Laravel app to Vapor and cache is automatically deleted right after the deploy. I also clear it manually via CLI just to be sure. Still, it's not running with the updated build because it can't find a Class that instead it's there (double checked the namespaces and everything else). On my local machine it's working fine, the class is found and so I can instantiate it.
I tried to clear every possible cache (route, config, view, also run a composer dump-autoload) but nothing seems to work. Any clue?

Including a php file from another server in Laravel controller

Hello i am try to include a PHP class (Payroll.php) found on another server since it requires the main module that has been written in pure PHP in my Laravel sub-module. I was given the url to the php file on the other server and have been tasked with using it to fetch the employees from the main module to be used in my module. Almost all the solutions i found on the internet where for cases where the laravel code and the php code exist on the same server. I tried several approaches such as including the file directly (which is giving me class not found) and using things like guzzle and so on (which failed since it is not an API). Any help would be appreciated.

Laravel, codesleeve asset-pipeline not working

I am trying to get codesleeve asset-pipeline to work on my site. After wasting a full day, I finally got it working yesterday on my local development server. I have now uploaded the project to see if everything works on the live server. Guess what- it doesn't. I am guessing the problem is to do with the environment setting, ie:
$env = $app->detectEnvironment(array(
'local' => array('your-machine-name'),
));
So, I have changed that to my machine name when on my local server, which, as I say, I have working. So on the live server, what should this be? "localhost"? I have tried adding another array key for 'live' with my live server's IP address and domain name, as per this answer: http://tinyurl.com/pg6hwum Nothing works.
Also, according to the tutorials I am following:
When you are in the local environment, you will notice that all of your asset files will be included individually ... Once you are in the production environment, all of your assets will be concatenated into one.
That doesn't seem to be the case for me either, as pipeline seems to be half-working, ie. it is concatenating my js and css files, but somehow messing them up.
I'd really appreciate some pointers, as I have wasted a colossal amount of time with this thing now. Thanks.
First set your environment settings on your bootstrap/start.php file into something like this:
$env = $app->detectEnvironment(array(
'local' => array('http://localhost', '*.local', 'http://local.sitename'),
'development' => array('http://dev.sitename.com'),
'production' => array('http://www.sitename.com', 'http://sitename.com'),
));
Second you need to check what enviroment does your laravel is running to check if your environment setup is working properly. By doing this:
App::environment()
So by doing that..you should have idea now what is causing the problem on codesleeve asset-pipeline.
note: see the documentation about environment for more details here
Asset pipeline will out of the box behave differently when on production compared to another environment (like local). I have put in a proposal to document some of these pain points and troubleshooting for new comers.
If you are using the most current version then there are two big behavior changes when switching environments:
caching - production assets are cached. This means once you load a page you will be stuck with those assets until you do
$> php artisan assets:clean
This is why you should set your environment to local when developing.
minification - assets are minified on production only. This can cause issues though because the minifiers sometimes break on code. There are some workarounds you can do to fix this. Something I normally do when having issues with a certain file (say Twitter Bootstrap) is use the .min.css extension which is then skipped by the minifier and written out as-is.
Note that this behavior is out of the box and can easily be modified by editing your configuration.
$> php artisan config:publish codesleeve/asset-pipeline
Then edit your file app/config/packages/codesleeve/asset-pipeline/config.php. For more information on these options visit the documentation here.
Hope this helps.

Resources