I'm running phpunit and it gives me an error
PHP Fatal error: Class 'MyApp\MyBundle\Tests\Units\Classes\Action\ActionTestCase' not found in /var/www/src/tests/src/MyApp/MyBundle/Classes/Action/KeywordTest.php on line 7
In that class is
5 use MyApp\MyBundle\Tests\Units\Classes\Action\ActionTestCase as TestCase;
6
7 class KeywordTest extends TestCase {
The thing I can't understand is, ActionTestCase exists, but at MyApp\MyBundle\Classes\Action\ActionTestCase
When I use the autocomplete function of PhpStorm, it uses the path with the \Tests\Units part. That is the only path it finds, and if I double click in there it directs to the correct class. And yet PHPUnit doesn't find it.
Where is this extra path being added? In PhpStorm or Composer or somewhere else?
The composer.json file includes this
"autoload": {
"psr-0": {
"": "src/"
}
}
,
"autoload-dev": {
"psr-4": {
"": "src/tests"
}
},
The phpunit.xml file includes this
<phpunit bootstrap="app/autoload.php">
Moving tests out of src sounds like a good suggestion (regarding Jakub Zalas comment), you can better say if that is an option for you or not.
Regardless of that, the dev autoloader is not correctly configured for that path:
"autoload-dev": {
"psr-4": {
"": "src/tests"
}
},
Would look for the file
src\tests\MyApp\MyBundle\Classes\Action\ActionTestCase.php
but you have the file at
src\tests\src\MyApp\MyBundle\Classes\Action\ActionTestCase.php
^^^
Adding the missing src/ part should make it working:
"autoload-dev": {
"psr-4": {
"": "src/tests/src/"
}
},
Don't forget to dump the autoload again:
composer dump-autoload
after you change the composer.json.
Related
I have a listener called LoginListener which works, but for some reason randomly i get this error when making a request for a route. If i then refresh it doesn't happen, then will randomly happen again.
Expected to find class "App\Security\LoginListener" in file
"/home/nibbrstaging/webuyanyhouse.nibbrstaging.com/api/src/Security/LoginListener.php"
while importing services from resource "../src/*", but it was not
found! Check the namespace prefix used with the resource in
/home/nibbrstaging/webuyanyhouse.nibbrstaging.com/api/config/services.yaml
(which is loaded in resource
"/home/nibbrstaging/webuyanyhouse.nibbrstaging.com/api/config/services.yaml").
I've noticed this a few times when i say edit a controller and add a new route, it will then throw this error. If i then come in and delete a bunch of white space in the new controller method it goes away.
It's a strange issue this, has anyone seen this happen before and how to solve it?
It does this in dev mode as well as prod, here is my auto load config:
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
Hello every body I'm doing inheritance from FOSUserBundle to override some controllers, but At first step when I was creating a simple bundle I found an issue:
bin/console genrate:bundle
Are you planning on sharing this bundle across multiple applications? [no]:
Bundle name: MyVendorFOSUserBundle
Target Directory [src/]: src/MyVendor/
Configuration format (annotation, yml, xml, php) [annotation]:
Bundle generation
> Generating a sample bundle skeleton into app/../src/MyVendor/MyVendorFOSUserBundle
created ./app/../src/MyVendor/MyVendorFOSUserBundle/
created ./app/../src/MyVendor/MyVendorFOSUserBundle/MyVendorFOSUserBundle.php
created ./app/../src/MyVendor/MyVendorFOSUserBundle/Controller/
created ./app/../src/MyVendor/MyVendorFOSUserBundle/Controller/DefaultController.php
created ./app/../tests/MyVendorFOSUserBundle/Controller/
created ./app/../tests/MyVendorFOSUserBundle/Controller/DefaultControllerTest.php
created ./app/../src/MyVendor/MyVendorFOSUserBundle/Resources/views/Default/
created ./app/../src/MyVendor/MyVendorFOSUserBundle/Resources/views/Default/index.html.twig
created ./app/../src/MyVendor/MyVendorFOSUserBundle/Resources/config/
created ./app/../src/MyVendor/MyVendorFOSUserBundle/Resources/config/services.yml
> Checking that the bundle is autoloaded
FAILED
> Enabling the bundle inside app/AppKernel.php
updated ./app/AppKernel.php
OK
> Importing the bundle's routes from the app/config/routing.yml file
updated ./app/config/routing.yml
OK
> Importing the bundle's services.yml from the app/config/config.yml file
updated ./app/config/config.yml
OK
The command was not able to configure everything automatically.
You'll need to make the following changes manually.
- Edit the composer.json file and register the bundle
namespace in the "autoload" section:
At first solution I tried to modify composer.json :*
"autoload": {
"psr-4": {
"": "src/" //modif
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
Does someone have any idea about that
Open composer.json file.
Change this:
"autoload": { "psr-4": { "AppBundle\\": "src/AppBundle" },
to:
"autoload": { "psr-4": { "": "src/" },
Run this command: composer dumpautoload Now composer will generate new autoload.php file
Run command: php bin/console server:run everything works file.
Finally, I found the solution:
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"MyVendorFOSUserBundle\\": "src/MyVendor/MyVendorFOSUserBundle" //Line added
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
Don't forget to run:
composer dumpautoload
When I generate a new bundle I receive the error notice that kind:
"Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "myBundle" from namespace "my\myBundle".
Did you forget a "use" statement for another namespace? in C:\wamp64\www\symfony\app\AppKernel.php"
in autoload section of composer.json replace the entry
"psr-4": { "AppBundle\\": "src/AppBundle" },
for
"psr-4": { "": "src/" },
then execute composer dumpautoload.
that worked for me.
I use Symfony Version 2.8. I get the following error when generating a new bundle.
I generate a new bundle using this command:
php bin/console generate:bundle --namespace=FrontBundle --no-interaction
Then the console show me this results
Bundle generation
Generating a sample bundle skeleton into C:\xampp\htdocs\TRC\app/../src/FrontBundle
created .\app/../src/FrontBundle/
created .\app/../src/FrontBundle/FrontBundle.php
created .\app/../src/FrontBundle/Controller/
created .\app/../src/FrontBundle/Controller/DefaultController.php
created .\app/../tests/FrontBundle/Controller/
created .\app/../tests/FrontBundle/Controller/DefaultControllerTest.php
created .\app/../src/FrontBundle/Resources/views/Default/
created .\app/../src/FrontBundle/Resources/views/Default/index.html.twig
created .\app/../src/FrontBundle/Resources/config/
created .\app/../src/FrontBundle/Resources/config/services.yml
Checking that the bundle is autoloaded
FAILED
Enabling the bundle inside C:\xampp\htdocs\TRC\app\AppKernel.php
updated .\app\AppKernel.php
OK
Importing the bundle's routes from the C:\xampp\htdocs\TRC\app\config\routing.yml file
updated .\app/config/routing.yml
OK
Importing the bundle's services.yml from the C:\xampp\htdocs\TRC\app\config\config.yml file
updated .\app/config/config.yml
OK
The command was not able to configure everything automatically.
You'll need to make the following changes manually.
Edit the composer.json file and register the bundle
namespace in the "autoload" section:
Process finished with exit code 0 at 17:19:48.
Execution time: 719 ms.
Then I add a line to Composer.Json in autoload section:
#...
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"FrontBundle\\": "src/FrontBundle"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
"autoload-dev": {
"files": [
"vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
]
},
#.........
Finally, I use again the console to dump-autoload and when I refresh the page I get the same error.
I have tried to clear cache and force browser to refresh.
"psr-4": {
"AppBundle\\": "src/AppBundle",
"FrontBundle\\": "src/FrontBundle"
},
Or better:
"psr-4": {
"": "src/"
}
I've started to learn Symfony and I am following some tutorials where there is nothing about this:
When I create a new project with symfony installer and run composer install and then php app/console server:start I can open that project in my browser.
BUT! When I create a new bundle with command php app/console generate:bundle I get this error message:
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "BlogBundle" from namespace "BlogBundle".
Did you forget a "use" statement for another namespace? in /home/user/Symfony/myapp/app/AppKernel.php:19
And then I need to go to my composer.json file and append my new generated bundle after AppBundle like this
{
"name": "user/myapp",
"license": "proprietary",
"type": "project",
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"BlogBundle\\": "src/BlogBundle" // <-- this is the new appended one
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
..........
And then when I try to start the server again it works and it shows Hello world in my browser.
So the question is do I have to do this every time (append new generated bundle in composer.json file)?
Yes. This is the reason, why you usually have an namespace above that. Some abbreviation of your name or company for example. This way you only add that namespace for the src folder and all bundles are "found" automatically.
Example:
{
...
"autoload": {
"psr-4": {
"Acme\\": "src"
},
}
...
}
Now of course your bundles need to use that namespace, eg.:
namespace Acme\BlogBundle;
class BlogBundle {
}