foundry icon indicating copy to clipboard operation
foundry copied to clipboard

[make:factory] Problems with `--test` flag

Open tomasnorre opened this issue 11 months ago • 13 comments

I'm honestly know sure, if I'm misunderstanding something, doing it wrong or it a bug.

Lets say I have the entity setup from documentation. https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#same-entities-used-in-these-docs

A post and a comment.

If I create factories with the bin/console make:factory --test Post flag it complains that the CommentFactory is not present and as if it should create it for me. If I said yes, it creates it's in the src/Factory directory even if I use the --test flag.

If it's already is created in tests/Facoty is still complains that it's not present as it looks for it in src/Factory/CommentFactory.php but when I answer yes to create if fails, as it's already created.

Is this something that I misunderstand from concept or is this a bug?

Thanks for your help.

tomasnorre avatar Jan 13 '25 17:01 tomasnorre

Hi @tomasnorre

Yeah this sounds like a bug!

nikophil avatar Jan 13 '25 19:01 nikophil

I'm haven't really looked into the code base. But I can see if I can come up with a PR, unless you know exactly where to dig in and solve it fast ;)

tomasnorre avatar Jan 13 '25 20:01 tomasnorre

yes, PR welcome, thanks!

I think there are two problems:

--test flag seem to not be propagated

We're calling here FactoryGenerator::generateFactory() with another MakeFactoryQuery. There must be an error around this, so that the --test flag is not propagated to the next FactoryGenerator::generateFactory() call.

You're asked for factories that already exist

Typically, you're asked here if a new factory should be created. So I suspect this condition to not work properly:

if (!$factoryClass = $this->factoryClassMap->getFactoryForClass($fieldClass)) {

Maybe this is because we're giving it a tagged iterator with foundry.factory, and I think the factories in tests repository might not be tagged. We have to come up with a solution for this, not sure what's the best one.

nikophil avatar Jan 14 '25 07:01 nikophil

Thanks for your pointers. I have fork the repository, but getting quite a number of errors when running the tests. I'll look into that first, I don't want to start of with failing tests.

tomasnorre avatar Jan 14 '25 17:01 tomasnorre

The tests are passing on branch 2.x

have a look into the readme for some insights in order to contribute. Using docker for the database helps a lot :)

For the tests about maker, you can use phpunit --group maker

nikophil avatar Jan 14 '25 18:01 nikophil

usually, I have this .env.local in my local repo, so that the tests are faster:

MONGO_URL="" # disable mongo related tests
PHPUNIT_VERSION="11" # enable phpunit 11
USE_DAMA_DOCTRINE_TEST_BUNDLE="1" # enable dama

nikophil avatar Jan 14 '25 18:01 nikophil

I have following:

╰─❯ php -v                                   
PHP 8.3.15 (cli) (built: Dec 24 2024 06:10:28) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.15, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.15, Copyright (c), by Zend Technologies
    with Xdebug v3.4.0, Copyright (c) 2002-2024, by Derick Rethans

Example when running tests, with the .env.local you just suggested.

$ docker compose up -d
$ ./phpunit
vendor/bin/phpunit -c phpunit-10.xml.dist --extension Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\PhpUnitTestExtension --extension DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension 
PHPUnit 11.5.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.15
Configuration: /home/tomas/Projects/opensource/zenstruck-foundry/phpunit-10.xml.dist

..SSSSFF.FFF.F.....SS.SFFFFFFFFFFF....................I......   61 / 1144 (  5%)
.............................................................  122 / 1144 ( 10%)
.............................................................  183 / 1144 ( 15%)
.............................................................  244 / 1144 ( 21%)
.............................................................  305 / 1144 ( 26%)
.............................................................  366 / 1144 ( 31%)
.............................................................  427 / 1144 ( 37%)
.............................................................  488 / 1144 ( 42%)
.............................................................  549 / 1144 ( 47%)
.............................................................  610 / 1144 ( 53%)
.............................................................  671 / 1144 ( 58%)
.............................................................  732 / 1144 ( 63%)
.............................................................  793 / 1144 ( 69%)
.............................................................  854 / 1144 ( 74%)
.............................................................  915 / 1144 ( 79%)
.............................................................  976 / 1144 ( 85%)
............................................................. 1037 / 1144 ( 90%)
.......I.....

Time: 00:19.476, Memory: 58.50 MB

There were 17 failures:

1) Zenstruck\Foundry\Tests\Integration\Maker\MakeFactoryTest::can_create_factory
File "/home/tomas/Projects/opensource/zenstruck-foundry/tests/Integration/Maker/../../Fixture/Maker/tmp/src/Factory/CategoryFactory.php" is different from expected file "/home/tomas/Projects/opensource/zenstruck-foundry/tests/Fixture/Maker/expected/can_create_factory.php".
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
 '<?php\n
 \n
-/*\n
- * This file is part of the zenstruck/foundry package.\n
- *\n
- * (c) Kevin Bond <[email protected]>\n
- *\n
- * For the full copyright and license information, please view the LICENSE\n
- * file that was distributed with this source code.\n
- */\n
-\n
 namespace App\Factory;\n
 \n
+use Doctrine\ORM\EntityRepository;\n
 use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;\n
+use Zenstruck\Foundry\Persistence\Proxy;\n
+use Zenstruck\Foundry\Persistence\ProxyRepositoryDecorator;\n
 use Zenstruck\Foundry\Tests\Fixture\Entity\Category;\n
 \n
 /**\n
  * @extends PersistentProxyObjectFactory<Category>\n
  */\n
-final class CategoryFactory extends PersistentProxyObjectFactory\n
-{\n
+final class CategoryFactory extends PersistentProxyObjectFactory{\n
     /**\n
      * @see htt...'

/home/tomas/Projects/opensource/zenstruck-foundry/tests/Integration/Maker/MakerTestCase.php:59
/home/tomas/Projects/opensource/zenstruck-foundry/tests/Integration/Maker/MakeFactoryTest.php:75

tomasnorre avatar Jan 14 '25 19:01 tomasnorre

When running.

./phpunit --group maker

Tests: 32, Assertions: 137, Failures: 17, PHPUnit Deprecations: 308, Skipped: 3.

And I'm on 2.x branch. Up to date with the main repository.

I have tried with PHP 8.2, 8.3 and 8.4, all same result.

tomasnorre avatar Jan 14 '25 19:01 tomasnorre

yeah it seems that the problem comes from the last symfony/maker version released yesterday, some changes around php-cs-fixer seem to have been made... please run composer u symfony/maker-bundle:1.61.0 and it should be ok

nikophil avatar Jan 14 '25 19:01 nikophil

Thanks a lot. That solved it for me.

I'll see if I can come up with a fix, or as minimum a test that proves the problem.

tomasnorre avatar Jan 14 '25 19:01 tomasnorre

Possibly related? https://github.com/symfony/maker-bundle/pull/1651

kbond avatar Jan 14 '25 23:01 kbond

The maker-bundle stuff should be fixed, just run composer update.

kbond avatar Jan 15 '25 00:01 kbond

The maker-bundle stuff should be fixed, just run composer update.

The updated maker-bundle works for me in the tests now.

tomasnorre avatar Jan 15 '25 05:01 tomasnorre