symfony / maker-bundle

Symfony Maker Bundle
https://symfony.com/
MIT License
3.33k stars 405 forks source link

Making an ApiTestCase breaks after installing API core #836

Open iisisrael opened 3 years ago

iisisrael commented 3 years ago

Initially, I can create an ApiTestCase, ignoring the message:

 [WARNING] API Platform is required for this test type. Install it with                                                 

           composer require api                                                                                         

The test created, of course, does not run successfully:

PHP Fatal error:  Uncaught Error: Class 'ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase' not found in /var/www/html/tests/TestTest.php:7

However, if I install the required API dependencies, attempting to create an ApiTestCase breaks with this message:

$ bin/console make:test ApiTestCase

In KernelTestCase.php line 24:

  Attempted to load class "TestCase" from namespace "PHPUnit\Framework".  
  Did you forget a "use" statement for another namespace?                 
jrushlow commented 3 years ago

Have you tried running composer update? this sounds like a composer autoloader issue.

David-Baron commented 3 years ago

It's not a maker-bundle issue but maybe an api-platform test misconfigured file. The doc is here: https://api-platform.com/docs/distribution/testing/

iisisrael commented 3 years ago

@jrushlow yes I ran update, same result. @David-Baron this seems like an autoloader issue?

This can be recreated with a fresh Symfony project, which is the case that led to this issue. I'm including here some steps that may not be related, but they're the steps I followed to create this project, so just in case...

mkdir test-api
cd test-api
composer create-project symfony/website-skeleton .
composer config extra.symfony.allow-contrib true
composer require symfony/apache-pack
composer require symfony/orm-pack
composer require api
composer dump-env dev
bin/phpunit
bin/console make:test
...
 > ApiTestCase

The running of bin/phpunit seems important, as that installs all of the PHPUnit dependencies in bin/phpunit/phpunit-8.5-0, and would thus be available for any use statement.

The "working" state can be recreated by repeating the above skipping the composer require api line. Note that, in this state, running bin/console make:test ApiTestCase in a single line rather than selecting the option ends with an error:

 [ERROR] Missing package: to use the make:test command, run:                                                            

         composer require api                                                                                           

whereas running only bin/console make:test and selecting the ApiTestCase option only gives a warning:

 [WARNING] API Platform is required for this test type. Install it with                                                 

           composer require api                                                                                         

and then proceeds to ask for a name for the test.

David-Baron commented 3 years ago

@iisisrael And what is in your composer.json?

iisisrael commented 3 years ago

@David-Baron without the API core:

{
    "type": "project",
    "license": "proprietary",
    "minimum-stability": "dev",
    "prefer-stable": true,
    "require": {
        "php": ">=7.2.5",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "1.11.99.1",
        "doctrine/annotations": "^1.0",
        "doctrine/doctrine-bundle": "^2.2",
        "doctrine/doctrine-migrations-bundle": "^3.0",
        "doctrine/orm": "^2.8",
        "phpdocumentor/reflection-docblock": "^5.2",
        "sensio/framework-extra-bundle": "^5.1",
        "symfony/apache-pack": "^1.0",
        "symfony/asset": "5.2.*",
        "symfony/console": "5.2.*",
        "symfony/dotenv": "5.2.*",
        "symfony/expression-language": "5.2.*",
        "symfony/flex": "^1.3.1",
        "symfony/form": "5.2.*",
        "symfony/framework-bundle": "5.2.*",
        "symfony/http-client": "5.2.*",
        "symfony/intl": "5.2.*",
        "symfony/mailer": "5.2.*",
        "symfony/mime": "5.2.*",
        "symfony/monolog-bundle": "^3.1",
        "symfony/notifier": "5.2.*",
        "symfony/process": "5.2.*",
        "symfony/property-access": "5.2.*",
        "symfony/property-info": "5.2.*",
        "symfony/proxy-manager-bridge": "5.2.*",
        "symfony/security-bundle": "5.2.*",
        "symfony/serializer": "5.2.*",
        "symfony/string": "5.2.*",
        "symfony/translation": "5.2.*",
        "symfony/twig-bundle": "^5.2",
        "symfony/validator": "5.2.*",
        "symfony/web-link": "5.2.*",
        "symfony/yaml": "5.2.*",
        "twig/extra-bundle": "^2.12|^3.0",
        "twig/twig": "^2.12|^3.0"
    },
    "require-dev": {
        "symfony/browser-kit": "^5.2",
        "symfony/css-selector": "^5.2",
        "symfony/debug-bundle": "^5.2",
        "symfony/maker-bundle": "^1.0",
        "symfony/phpunit-bridge": "^5.2",
        "symfony/stopwatch": "^5.2",
        "symfony/var-dumper": "^5.2",
        "symfony/web-profiler-bundle": "^5.2"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": "true",
            "require": "5.2.*"
        }
    }
}

And after composer require api:

diff --git a/composer.json b/composer.json
index 874d046..c27baae 100644
--- a/composer.json
+++ b/composer.json
@@ -7,11 +7,13 @@
         "php": ">=7.2.5",
         "ext-ctype": "*",
         "ext-iconv": "*",
+        "api-platform/core": "^2.6",
         "composer/package-versions-deprecated": "1.11.99.1",
         "doctrine/annotations": "^1.0",
         "doctrine/doctrine-bundle": "^2.2",
         "doctrine/doctrine-migrations-bundle": "^3.0",
         "doctrine/orm": "^2.8",
+        "nelmio/cors-bundle": "^2.1",
         "phpdocumentor/reflection-docblock": "^5.2",
         "sensio/framework-extra-bundle": "^5.1",
         "symfony/apache-pack": "^1.0",

as well as:

--- a/config/bundles.php
+++ b/config/bundles.php
@@ -12,4 +12,6 @@ return [
     Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
     Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
     Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
+    Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true],
+    ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle::class => ['all' => true],
 ];

among other changes.

David-Baron commented 3 years ago

@iisisrael I test your install and it's running. Have you testing the api url? Maybe something missing for the tests, check here: https://api-platform.com/docs/distribution/testing/

bmeynell commented 3 years ago

Same issue here as initial report from @iisisrael:

$ php bin/console make:test ApiTestCase BlogPostTest

In KernelTestCase.php line 24:

  Attempted to load class "TestCase" from namespace "PHPUnit\Framework".
  Did you forget a "use" statement for another namespace?

make:test [<type> [<name>]]

All other types work as expected:

maMykola commented 3 years ago

Seems like the problem in this line https://github.com/symfony/maker-bundle/blob/main/src/Maker/MakeTest.php#L105 The class_exists function is failed due to unavailability to load PHPUnit\Framework\TestCase class, as it located under bin/.phpunit folder but not in vendor/phpunit

iisisrael commented 3 years ago

@maMykola that's correct in the case of the workaround, which is to uninstall the api-platform/core package to enable running the make:test command. That's the source of the warning message.

The problem still remains when the api-platform/core package is installed - running make:test breaks when trying to create an ApiTestCase, with the exception Attemted to load class "TestCase"...

squareRoot commented 3 years ago

temporarily, load phpunit via composer:

    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/",
            "PHPUnit\\": "bin/.phpunit/phpunit/src"
        }
    },
smilesrg commented 3 years ago

I installed composer require --dev symfony/phpunit-bridge then executed bin/console make:test and made a test for API. That's completely strange to couple API tests to an API Platform, I'm not using it In my current project. If Api tests are dependable from API Platform, then this platform needs to be required OR developer shouldn't be able to generate such kind of test

sherlinshaji commented 2 years ago

I installed composer require --dev phpunit/phpunit symfony/test-pack and then run bin/console make:test and select the ApiTestCase and continued .it works for me

AntonioCS commented 2 years ago

Will there be any fix for this? Just got hit by this in very strange circumstances. If I edit a file that is include in the tests (it's in src/) I get hit by this error but then composer update fixes it, but if I change that file again I get the error again...