Warning: Ambiguous class resolution Doctrine - symfony

After running composer update , I keep having the error below:
Warning: Ambiguous class resolution,
"Doctrine\ORM\Persisters\Entity\BasicEntityPersister" was found in
both "$baseDir .
'/engine/Library/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php"
and
"/var/www/html/shop5/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php",
the first will be used. Warning: Ambiguous class resolution,
"Doctrine\Common\Proxy\AbstractProxyFactory" was found in both
"$baseDir .
'/engine/Library/Doctrine/Common/Proxy/AbstractProxyFactory.php" and
"/var/www/html/shop5/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php",
the first will be used.
I have tried to run the following commands but none of them works:
composer dump-autoload -o
composer clearcache
Any idea how to fix this issue ?
Thank you
[shopware5 - php7.0]

This is the normal behavior of Shopware.
The Doctrine library uses the final class statement quite often and in order to get it to work with the Shopware attribute system the classes were replaced through the composer autoloading. You can find the changed files in shopware/engine/Library/Doctrine/Common
FYI: This is the reason why Shopware only works when the composer autoloading is optimized.
composer dump-autoload --optimize
Otherwiese you will encounter random errors from invalid or wrong entities.

To get rid of these warning you should add files with ambiguous classes to exclude-from-classmap in your composer.json:
"autoload": {
...
"exclude-from-classmap": [
...
"vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php",
"vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php"
]
},
Then dump-autoload will ignore these files.
This is the reason why Shopware only works when the composer autoloading is optimized.
I didn't investigate how this is done in Shopware, but this also could be fixed/improved. For composer more precise definitions for namespaces have a precedence. So if you have this in your autoload:
"autoload": {
"psr-0": {
"somevendor\\somepackage\\": "vendor/somevendor/somepackage/",
"somevendor\\somepackage\\db\\": "overrides/somevendor/somepackage/db/"
}
},
And if you request for somevendor\somepackage\db\Entity class, the composer will first search in overrides/somevendor/somepackage/db/Entity.php and only if it can't find it, it will try vendor/somevendor/somepackage/db/Entity.php. This is because definition for somevendor\somepackage\db namespace is more precise than for somevendor\somepackage.
So if you want to override 3rd-party classes in this way, you should define more precise namespaces than 3rd-party library do.

Related

Codeception Simple Unit Test Not Work To Find My Namespaces

I'm trying a basic UnitTest with Codeception. No frameworks are used.
My working root is as:
tests |
|-unit
|-Test.php
includes|
|-general
|-Custom.php
In Custom.php
<?php
namespace Custom;
class General {
public static function check(){}
}
My test case is:
<?php
use Custom\General;
use PHPUnit\Framework\TestCase;
final class Test extends TestCase
{
public function testPushAndPop(): void
{
General::check();
}
}
I also have in my composer.json:
"autoload": {
"psr-4":{
"Custom\\":"includes/general"
}
},
When I run
php vendor/bin/codecept run unit
...
1) Test: Push and pop
Test tests/unit/Test.php:testPushAndPop
[Error] Class 'DB\General' not found
#1 /var/www/html/prj/tests/unit/Test.php:9
Codeception Simple Unit Test Not Work To Find My Namespaces
This is less about Codeception but how PHP autoloading works in general and what the configuration of autoloading in Composer is in specific:
{
"autoload": {
"psr-4":{
"Custom\\":"includes/general"
}
}
}
Even the path-segment includes/general does map on the file-system path includes/general/ (composer should have told you to add / at the end of the path-segment), the error message
[Error] Class 'DB\General' not found
#1 /var/www/html/prj/tests/unit/Test.php:9
shows that the namespace of Custom\\ in the Composer configuration is different to the namespace of the class that is not found (which is DB\\).
So even the framework you have in use (you have one and that is the testing framework) may load the composer auto-loader (highly likely), its just that the class is not found.
As #Naktibalda already highlighted in a comment, it is just a plain autoloader configuration issue.
You are right, but why? The IDE does not claim any error... (your reaction)
These are two pair of shoes.
Your IDE most likely does not rely on the autoloader and just guesses the file from the file-system.
Depending how well maintained and configured your IDE is, it perhaps should and could have highlighted you that.
On the other hand, PHP can only rely on the autoloader, in your case you delegate the autoloading to composer(1), not to your IDE.
So maybe improve on that end as well, composer is more central for your projects development than the IDE can be.
So a suggestion:
Whenever changing the composer.json file, I suggest to run composer validate --strict to check your status, then follow with a composer update.
Run this automatically before running your tests. You may not want to run composer update, then run composer install before running the test-runner if you have it as a development dependency.
Example to bind this for a single test run command within your composer.json:
{
"scripts": {
"test": [
"#composer --no-plugins --version",
"#composer validate --strict",
"#composer --no-plugins -q install --no-scripts --no-progress",
"codecept run unit"
]
}
}
you then have a single call to run all the important things in your Composer based project:
$ composer test
...
Running unit-tests is a good way to verify the autoload configuration by the way. With the composer test-script you ensure it is always up-to-date when running the test-suite, too.
You don't need an IDE for this technically, which makes it more portable and stable giving you the peace of mind for your composer based project to grow.

howto fix composer.json in forked symfony bundle

I'm trying to install symfony-cmf/routing-auto version 2.0.0-RC1 , it requires jms/metadata:1.5.* which is working under Symfony 2x.
My current project works on Symfony 3.3.x which makes this bundle unable to install, so I made a fork on github, changed req. to jms/metadata:1.6.*
and added one line:
"replace": "symfony-cmf/routing-auto:2.0.0-RC1",
in order to test if it will work and I used in console:
composer require mkoniarz/routing-auto:dev-master
but then I got error:
Reading composer.json of mkoniarz/routing-auto (dev-master) Skipped branch dev-master, Invalid argument supplied for foreach()
What else I should fix to get this fork installed by composer?
PS my composer is up to date.
Did you try to remove the composer.json file ? I'd an similar error, i think it can be resolve your problem.
Or you should to try :
composer require symfony-cmf/routing-auto
always check composer.json:
composer.phar validate
then commit if valid :)
error was in "replace" line:
"replace": "symfony-cmf/routing-auto:2.0.0-RC1",
should be:
"replace": { "symfony-cmf/routing-auto":"2.0.0-RC1" },

class PHPUnit\Framework\ExpectationFailedException not found

when I try to run a failed test with this command :
./vendor/bin/phpunit
I get this Fatal Error :
PHPUnit 5.7.20 by Sebastian Bergmann and contributors.
PHP Fatal error: Class 'PHPUnit\Framework\ExpectationFailedException'
not found in /var/www/zend/vendor/zendframework/zend-
test/src/PHPUnit/Controller/AbstractControllerTestCase.php on line 444
Your version of phpunit is probably too old for your version of Zend. The class PHPUnit\Framework\ExpectationFailedException have been renamed in PhpUnit 6.X from PHPUnit_Framework_ExpectationFailedException to ExpectationFailedException
Please check your PhpUnit version: phpunit --version, it should be 6.X. Update it to the last version to avoid this error.
This is "fixed" by a script in Zend\Test called phpunit-class-aliases.php but it's not configured properly IMHO since it's in the autoload-dev section (meaning it doesn't propagate out to other projects.)
So, in your project composer.json, do something like this:
"autoload-dev": {
"files": [
"vendor/zendframework/zend-test/autoload/phpunit-class-aliases.php"
]
},
Then composer install
N.B. Zend\Test has a pull request that fixes this very thing, but they're saying it's PHPUnit's fault (Shame on you PHPUnit 4 for... idunno... having the wrong class name according to Zend\Test) So, I've done it instead: composer require illchuk/phpunit-class-aliases
This is a configuration flaw in zend-test. It consumes classes from Phpunit 6 but per it's Composer requirements, Phpunit before that version are OK to require:
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0",
Most likely as your system because of the PHP version does not satisfy the requirements of Phpunit 6, the next lower version was installed.
As the code in the base test case (https://github.com/zendframework/zend-test/blob/master/src/PHPUnit/Controller/AbstractControllerTestCase.php#L444) makes use of Phpunit 6 classes, I strongly assume that when the configuration flaw is made aware to the Zend-Test project, you won't be even able to install on your system any longer.
Therefore upgrade to a recent PHP version and then run
composer update
If you're stuk with the PHP version, downgrade zend-test to a version that supports an older Phpunit version. I don't know that project well, so it's just a suggestion, I don't know if such a version exists or can't even recommend one.
I filed a report, perhaps using that one class was an oversight or there is a less hard way to resolve the dependency: https://github.com/zendframework/zend-test/issues/50

Creating a Bundle outside src/

I have created a bundle with symfony 2.3 but in this case (cause my teacher asked to me) outside src/ folder so I have ../symfony/fuentes/NameBundle instead of ../symfony/src/NameBundle. The new line appears in AppKernel and my new bundle appears on routing.yml, but when I try to launch the server
Bundle generation
Generating the bundle code: OK
Checking that the bundle is autoloaded: FAILED
Confirm automatic update of your Kernel [yes]?
Enabling the bundle inside the Kernel: OK
Confirm automatic update of the Routing [yes]?
Importing the bundle routing resource: OK
The command was not able to configure everything automatically.
You must do the following changes manually.
- Edit the composer.json file and register the bundle
namespace in the "autoload" section:
I have edit autoload and tried a lot of things (looking for here) but it appears the same error always.
C:\Users\Akenateb\Documents\UOC\AULAMENTOR\Symfony>php app/console server:run 127.0.0.1:8080
PHP Fatal error: Class 'AulaMentor\ExdosBundle\AulaMentorExdosBundle' not found in C:\Users\Akenateb\Documents\UOC\AULAMENTOR\Symfony\app\AppKernel.php on line 20
Can someone help me? I'm really stuck with it.
Thanks in advance.
First of all I want to thanks to the people who has answered. Here is what we have to do if we want to create a bundle outside 'src' folder, for example in 'fuentes' > '..Symfony/fuentes'.
If you have create with 'generate:bundle' I suggest you accept when the generator ask you if you want to create the complete structure, if you have create a bundle with generator goto step 3.
1- Make sure you have registered bundle in AppKernel and It exists there a line like this:
new YourProject\NameprojectBundle\YourProjectNameprojectBundle(),
2- Make sure you have adding a route to your app/config/routing and 'routing.yml' has you new bundle route, like this (you can add a prefix to your url, in this case fuentes):
your_project_name:
resource: "#YourProjectNameprojectBundle/Resources/config/routing.yml"
prefix: /fuentes
3- We edit 'app/autoload.php' and we add this line:
$loader->add('YourProject',realpath(__DIR__.'/../fuentes'));
Finally we can update assets doing with command line:
php app/console assets:install web
Hope it helps to someone.
Best Regards.
Do exactly as said in the comment:
Edit the composer.json file and register the bundle namespace in the "autoload" section
The src folder is automatically loaded using PSR convention. If you set classes outside of the src folder, they have to be declared as well.
In your composer.json file you can add another element in the autoload section. Here's an example that I think will work for your use case:
"autoload": {
"psr-0": {
"NameBundle\\": "fuentes/",
"": "src/"
}
},
Here's how to add simply another bundle outside your Symfony project, when we don't want to mess up with composer.json file.
The 2 first points are the same as in the Michael J.'s answer. Now to the 3rd point:
Say, we need to add a OurCompany/SomeBundle residing in the other project, which relative path is ../../OtherProject/src/OurCompany/SomeBundle to CurrentProject/app dir.
So we add this bundle to the CurrentProject application this way:
$loader->add('OurCompany\\SomeBundle', realpath(__DIR__.'/../../OtherProject/src'));
PLEASE NOTICE how the slashes and backslashes should be used (the remaining / or \ at the end doesn't matter, it's smart enough to figure it out).
And for the whole namespace to be loaded (all bundles namespaces in the other project available in CurrentProject):
$loader->add('OurCompany', realpath(__DIR__.'/../../OtherProject/src'));

How to install a Symfony 2.0 Bundle from Zip file

I tried installing the FixturesBundle as described in http://symfony.com/doc/2.0/bundles/DoctrineFixturesBundle/index.html but my proxy wont let me out.
So I went to https://github.com/doctrine/data-fixtures and download a zip from the latest commit.
I unzipped into the vendor directory and renamed it to doctrine-fixures. I edited the autoload.php and AppKernel.php files as described in the tutorial.
When I run:
php app\console doctrine:fixtures:load
I get the following message:
PHP Fatal error: Class 'Symfony\Bundle\DoctrineFixturesBundle\DoctrineFixturesB
undle' not found in C:\NetbeansProjects\route_rest_service\app\AppKernel.php on
line 20
Fatal error: Class 'Symfony\Bundle\DoctrineFixturesBundle\DoctrineFixturesBundle
' not found in C:\NetbeansProjects\route_rest_service\app\AppKernel.php on line
20
Is there a way to run the installation of bundle pointing it to a zip file?
I´m running Symfony 2.0.9 on Windows 7.
Seems like the doctrine bundle has been moved outside of Symfony scope back to Doctrine.
Please, use https://github.com/doctrine/DoctrineFixturesBundle
I had the same problem and this is how i successfully solved it. I started to download this files data-fixtures and DoctrineFixturesBundle, unzipped both into /vendor/doctrine and created this folder structure:
vendor
- doctrine
- doctrine-fixtures-bundle
- Doctrine
- Bundle
- FixturesBundle
DoctrineFixturesBundle.php
..other files...
vendor
- doctrine
- data-fixtures
- lib
- test
composer.json
..other files...
Then i edited the AppKernel.php and added
public function registerBundles(){
.....
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
.....
}
Edited the composer.json located in the root of the project and added this 2 lines:
"Doctrine\\Bundle\\FixturesBundle": "vendor/doctrine/doctrine-fixtures-bundle",
"Doctrine\\Common\\DataFixtures": "vendor/doctrine/data-fixtures/lib"
Mine, now look like this:
{
"autoload": {
"psr-0": {
"": "src/",
"Doctrine\\Bundle\\FixturesBundle": "vendor/doctrine/doctrine-fixtures-bundle",
"Doctrine\\Common\\DataFixtures": "vendor/doctrine/data-fixtures/lib"
}
}
}
afterwards execute composer dump-autoload -o to recreate the classmap. All this was thanks to the users Wouter J and nifr that answered my question How to install DoctrineFixturesBundle offline
Happy coding!
Yes, it's true that Doctrine is being moved away from the Symfony namespace (see here).
So if you're using the Symfony standard distribution you download from the website (without using git), and you want to have the FixturesBundle installed manually, you have to download the doctrine-fixtures library from here, extract the zip into
YourProject/vendor/doctrine-fixtures
Download the FixturesBundle from here, and extract it in
YourProject/vendor/bundles/Doctrine/Bundle/FixturesBundle
Then you have to register the library's namespace, do it by adding
'Doctrine\\\\Common\\\\DataFixtures' => \__DIR\__.'/../vendor/doctrine-fixtures/lib',
in your autoload.php.
In addition, you'll have to register the Doctrine's namespace, because some part of your application will be using the DoctrineBundle shipped from the Symfony namespace, but the new downloaded FixturesBundle will be using the new Doctrine's namespace, so to make it work, in your autoload.php add the following line (taking care you do it before the Doctrine namespace, remember that more specific rules go first!)
'Doctrine\\\\Bundle' => \__DIR\__.'/../vendor/bundles',
So all you have to do now is to register the new bundle in your AppKernel.php writing
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),

Resources