Codeception Simple Unit Test Not Work To Find My Namespaces - phpunit

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.

Related

Symfony 5 custom reusable bundle installation using flex - how to test and run a private recipe server

I have created a test reusable Symfony 5 bundle and have written a Flex recipe to automatically install and configure it within any project which is private.
My problem is, I have no idea how to run and test this. I cannot find any clear complete instructions anywhere. The official documentation does not specify how this would be done and only specifies how to create the manifest.json file.
https://github.com/symfony/recipes
I found the following info which specifies uploading the recipe to a private repository on GitHub and then activating Symfony Recipe Server for the repository which I have done.
https://blog.mayflower.de/6851-symfony-4-flex-private-recipes.html
but then what?
If understood this correctly, you want to add custom domain from where recipe would be downloaded and installed. Check this project:
Github https://github.com/moay/server-for-symfony-flex
Docs https://server-for-symfony-flex.readthedocs.io/en/latest/
Eventually you get to the point where you add custom endpoint from where to download the recipe like this:
Using the server in your Symfony project is as easy
as adding the proper endpoint to your composer.json:
{
...
"symfony": {
"endpoint": "https://your.domain.com"
}
}
I apologize if this is not in the desired format of an answer.

Warning: Ambiguous class resolution Doctrine

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.

Can't understand gradle jar task code that creates executable fat-jar

I am learning Gradle but I don't understand the jar task code that creates a jar with all the dependencies inside ( taken from Gradle Cookbook ):
jar {
baseName = jarBaseName
manifest { attributes "Main-Class": mainClass }
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
My questions are:
1.The task name is jar. Because it's not written like jar<<{...} I'm assuming that this is run in the configuration phase, and not the execution one. Am I correct?
2.What exactly is configurations.compile? I'm assuming that some kind of dependencies classpath is queried, and each jar is zipTree-ed. Then all of this stuff is merged with the base classpath
. Please elaborate about it
3.The zipTree method, I'm assuming it kind of unarchives each jar but I'm not sure. Am I correct?
Regards,
Yes You're correct. When no action added (mostly with << see docs) the code is run at configuration phase. The code You provided is also run at configuration phase.
configurations.compile refers to a configuration named compile using configurations object (a kind of a configurations container). Prior to gradle 2.0 compile configuration is provided by default with java plugin. AFAIR now it's called javaCompile. According to zipTree You're also correct.
Yes You're.

grunt-coverjs is not working

I am using grunt version "0.4.1" and grunt-coverjs version "0.1.0". I am writing the task as below:
cover: {
compile: {
files: {
'instrumented/testCoverage.js': ['src/file1.js'],
'instrumented/testDir/*.js': ['src/file2.js', 'src/file3.js']
}
}
}
When i run the above task i am getting error as: Object # has no method 'expandFiles'.
I am not sure what is causing the error.
Also, once the task is done, i think it is generated only the instrumented files, how can i generate the coverage report.
That error means the task isn't compatible with Grunt 0.4.x as grunt.file.expandFiles was deprecated. The author of that module can use grunt.file.expand({filter: 'isFile'}, file.src) instead. Although there is likely more updates that need to be done.
I'm sure the author would appreciate a pull request upgrading the module: https://github.com/jgrund/grunt-coverjs/blob/master/tasks/cover.js#L54
Here is the Grunt migration guide: http://gruntjs.com/upgrading-from-0.3-to-0.4

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