Class 'Tests\DuskTestCase' not found in ExampleTest.php - laravel

I am trying to run Laravel Dusk tests, but when I run the test, a new Chrome tab pops up with this message.
Fatal error: Class 'Tests\DuskTestCase' not found in path-to-project\tests\Browser\ExampleTest.php on line 9
All I have done so far is run composer require --dev laravel/dusk:^1.0 and php artisan dusk:install.
This is my ExampleTest.php (exactly how Laravel set it up)
<?php
namespace Tests\Browser;
use Laravel\Dusk\Chrome;
use Tests\DuskTestCase;
use Laravel\Dusk\DuskServiceProvider;
class ExampleTest extends DuskTestCase
{
/**
* A basic browser test example.
*
* #return void
*/
public function testBasicExample()
{
$this->browse(function ($browser) {
$browser->visit('/')
->assertSee('Laravel');
});
}
}
DuskTestCase.php is also just as Laravel set it up and has the namespace namespace Tests;.
I am using Laravel 5.4 and Dusk 1.0. I am running the test through PhpStorm, using the work around described here.
Anyone know why DuskTestCase can't seem to be found, even though it appears to be set up correctly? Thanks in advance.

In composer.json:
add "Tests\\": "tests/" in
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Tests\\": "tests/"
}
},
then, run composer dump-autoload to reload your packages.

I had this error due to using out-of-date docs that didn't include this line:
$ php artisan dusk:install

If composer dump-autoload does not solve problem, you can try these steps.
Visit homepage in your browser and check if it renders properly. If not, then you probably have a problem with your webserver configuration (Hint: isn't your project subdirectory of document root?).
You can try Laravel inbuilt server via php artisan serve. If homepage is accessible in your browser now, then you can try dusk again.
In that case, remember to update your .env file to match APP_URL=http://127.0.0.1:8000,
and run php artisan dusk from another cli window, cause php artisan serve needs to be running also.

if you test using a phpstrom then u have set path of phpunit ......
in settings/languages & framework/php/test frameworks and use composer autoloader path and then select a path of your laravel dusk project with autoload.php file.....
set file a vendor/autoload.php file in path to script...

Related

Run Laravel Seeder From Package

I'm working on a package for an internal Laravel application and I'm having trouble running a seeder that exists in the package directory.
In my package's composer.json file located in packages/vendor/packagename, I've added the following:
"autoload": {
"psr-4": {
"Vendor\\PackageName\\": "src/",
"Vendor\\PackageName\\Database\\Factories\\": "database/factories/",
"Vendor\\PackageName\\Database\\Seeders\\": "database/seeders/"
}
},
I have the following file located in "packages/vendor/packagename/database/seeders/DepartmentSeeder.php"
<?php
namespace Vendor\PackageName\Database\Seeders;
use Illuminate\Database\Seeder;
use Vendor\PackageName\Models\Department;
class DepartmentSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Department::factory()->count(10)->create();
}
}
I then attempt to run the following command:
$ php artisan db:seed --class="Vendor\\PackageName\\Database\\Seeders\\DepartmentSeeder"
Target class [Vendor\PackageName\Database\Seeders\DepartmentSeeder] does not exist.
If I move the seeders directory into my src directory and run the following, it works, but I'd rather keep my seeders in my database directory.
$ php artisan db:seed --class="Vendor\\PackageName\\seeders\\DepartmentSeeder"
Does anyone have any idea why the class isn't being found? All my Google search results are purple and I even went to page two =/
Solution
In my particular case, composer dump-autoload wasn't doing the trick. What I ended up doing was running composer update vendor/packagename and whatever the issue was, it was resolved.
Hopefully, this helps anyone else who may have similar issues.
In my particular case, composer dump-autoload wasn't doing the trick. What I ended up doing was running composer update vendor/packagename and whatever the issue was, it was resolved.
Hopefully, this helps anyone else who may have similar issues.

Laravel Autoclosing after clicking one request

I have a problem when declaring autoload in laravel,
i have been using laravel 8 and php 7.4.3 this is the problem
After i get another request, the laravel runner always closing and give me this error
To solve this error, i've always do the
php artisan key:generate
or do another thing like restarting the laravel after dump autoload
composer dump-autoload
php artisan serve
But the problem just solve for one time, and it will be error again until i dump the composer again
How can i solve this?
In your composer.json file you should have the following line
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
And in you artisan file you should have the following
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
The composer.json code should autoload any content coming from the App directory, while the require /vendor/autoloader will load vendor content into your project. Also you must have app.php inside of the bootstrap directory to allow the app to use the Kernal corrently. Hope this helps!

Nova Tool not updating

I'm making a Laravel Nova app. I'm trying to create a Nova Tool to import users.
The tool creates just fine, however when i update the code it does not show.
I've digged a bit into this, and the problem seems to be that the tool in the Vendor folder does not get updated.
When i do npm run dev or npm run prod, the tool files get updated inside the /nova-components/{componentname} folder, and not in the vendor folder, which is getting loaded by Nova.
I'm using Xampp on windows.
Inside my nova service provider:
/**
* Get the tools that should be listed in the Nova sidebar.
*
* #return array
*/
public function tools()
{
return [
new UserImport()
];
}
My composer file:
"require": {
"Vrumona/UserImport": "*"
},
"repositories": [
{
"type": "path",
"url": "./nova"
},
{
"type": "path",
"url": "./nova-components/UserImport"
}
],
How do make sure the Tool gets updated in the composer vendor folder?
I can delete the vendor folder and run composer install, but this is a bit tedious while developping.
Thanks!
It is likely that composer is not symlinking the package but rather mirroring. You can confirm by seeing what the output is when you run composer update -- if you see Mirrored from ... then symlinks are unavailable which will cause the issue that you're seeing.
As you noticed, you can force symlinks in the composer file using:
"options": {
"symlink": true // Will force symlinks
}
And the relevant documentation if needed: https://getcomposer.org/doc/05-repositories.md#path
php artisan cache:clear
php artisan route:clear
php artisan config:clear
php artisan view:clear

Laravel5.5 after use php artisan app:name Class not found

I am new developer on Laravel, now I'm using Laravel version 5.5
I got the problem after used php artisan app:name on my project, I got the problem:
In ProviderRepository.php line 208: Class
'App\Providers\AppServiceProvider' not found
as the captured image below:
As this error, I can not use php artisan commands or composer commands anymore can you guys please help me to solve this problem I am really appreciated for time. Thanks you
Best Regards
Siripong Gaewmaneechot
I would suggest looking in your config files, and the main classes which were generated when you started your laravel project (User class, etc) because they are all set to App\User App..... etc.
So for example, in the image you have in the question, it says it can not find App\AppProviders... - This indicates that somewhere you still have a use statement pointed to App\AppProviders.. but you changed the app name, so it's no longer App. something I do if I made that mistake, is I do a global search in my project files for App\ (you may need to put App\\ in the search because \ is an escape character
So if you did not change the app name immediately after starting the project, some of the paths will not be pointing to the correct directories. Let me know if that makes sense.
The command changes the PSR-4 configuration in composer.json.
Assume it was App before, your composer.json looks like this:
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
After running the command with php artisan app:name Foo, it will look like:
"autoload": {
"psr-4": {
"Foo\\": "app/"
}
},
Therefore the whole namespace has changed and your classes can't be found by the Autoloader. To fix this, you have to either go back to the old name or do a global search and replace to change the namespace from App to Foo.

Laravel 5.5 not found class with correct namespace

My ./app folder looks like:
+-- app
+-- Classes
+-- Events
+-- EventBase.php
+-- EventX.php
There's nothing secret with EventX file:
<?
namespace App\Classes\Events;
class EventX {
// ...
}
EventBase.php represents a Facade that inside it I just try to instantiate an EventX:
public function someMethod() {
new \App\Classes\Events\EventX;
// ...
}
After this line, Framework throw an exception telling that class was not found:
Symfony\Component\Debug\Exception\FatalThrowableError (E_ERROR)
Class 'App\Classes\Events\EventX' not found
Even that:
file_exists(__DIR__ . '\EventX.php'); // true
I already had this issue before when trying to create Facades and solved by moving class file from his current directory and after moving back (yeah, I don't why but it worked).
Something tells me that this is an issue of autoload process, so I tried these command (but still not working):
php artisan cache:clear
php artisan clear-compiled
php artisan config:clear
composer dump-autoload
What can I do in order to investigate the problem?
I think the problem is with the php tag <?
<?php
namespace App\Classes\Events;
class EventX {
// ...
}
PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).
Link
You need to add the new namespace to your composer.json to the key "psr-4"
"psr-4": {
"App\\": "app/",
"Classes\\": "app/classes/"
otherwise composer can't detect your new namespace
Another approach can be found here:
https://stackoverflow.com/a/28360186/6111545

Resources