2

I'm trying to add a form to a controller and it just returns this error:

Fatal error: Class 'PHPUnit\Framework\TestCase' not found in /var/www/site/vendor/symfony/form/Test/FormIntegrationTestCase.php on line 21

Though, I'm not quite sure why. At first glance it would seem that PHPUnit is not found but running cat composer.json in my directory says otherwise:

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.1.3",
        "ext-iconv": "*",
        "sensio/framework-extra-bundle": "^5.1",
        "symfony/apache-pack": "^1.0",
        "symfony/asset": "^4.1",
        "symfony/console": "^4.1",
        "symfony/expression-language": "^4.1",
        "symfony/flex": "^1.0",
        "symfony/form": "^4.1",
        "symfony/framework-bundle": "^4.1",
        "symfony/lts": "^4@dev",
        "symfony/monolog-bundle": "^3.1",
        "symfony/orm-pack": "^1.0",
        "symfony/process": "^4.1",
        "symfony/security-bundle": "^4.1",
        "symfony/serializer-pack": "*",
        "symfony/swiftmailer-bundle": "^3.1",
        "symfony/twig-bundle": "^4.1",
        "symfony/validator": "^4.1",
        "symfony/web-link": "^4.1",
        "symfony/webpack-encore-pack": "*",
        "symfony/yaml": "^4.1"
    },
    "require-dev": {
        "symfony/debug-pack": "*",
        "symfony/dotenv": "^4.1",
        "symfony/maker-bundle": "^1.5",
        "symfony/phpunit-bridge": "^4.1",
        "symfony/profiler-pack": "*",
        "symfony/test-pack": "^1.0",
        "symfony/web-server-bundle": "^4.1"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "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"
        }
    }
}

so I can see it there.. Here's my controller:

<?php
    namespace App\Controller\Bug;

    use App\Entity\Bug;
    use App\Form\Bug\AddType;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;

    class AddController extends Controller
    {
        public function add(Request $request, \Swift_Mailer $mailer)
        {
            # I know setting errors this way isn't best
            # but was faced with White Screen of Death
            ini_set('display_startup_errors', 1);
            ini_set('display_errors', 1);
            error_reporting(-1);

            $bug = new Bug();

            $form = $this->createForm(AddType::class, $bug);
            $form->handleRequest($request);

            $user = $this->getUser();

            if ($form->isSubmitted() && $form->isValid()) {
                $em = $this->getDoctrine()->getManager();

                $bug->setCreatedTs(new \DateTime());
                $bug->setSubmittedBy($user);
                $bug->setStatus('new');

                try {
                    $em->persist($bug);
                    $em->flush();

                    $this->addFlash('success', 'Bug Submitted.');

                    $message = (new \Swift_Message('Incoming Bug'))
                        ->setTo('[email protected]')
                        ->setBody(
                            $this->renderView(
                                'emails/bugs/add.html.twig',
                                array(
                                    'bug' => $bug,
                                    'submitted_by' => $user
                                )
                            ),
                            'text/html'
                        );

                    $mailer->send($message);
                } catch (\Exception $e) {
                    $this->addFlash('danger', 'Processing Error. Please try again.');
                }

                return $this->redirectToRoute('bug_add');
            }

            return $this->render('bug/add.html.twig', array('form' => $form->createView()));
        }
    }

my FormType:

<?php
    namespace App\Form\Bug;

    use App\Entity\Bug;
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
    use Symfony\Component\Form\Extension\Core\Type\TextareaType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\Tests\Extension\Core\Type\TextTypeTest;
    use Symfony\Component\OptionsResolver\OptionsResolver;

    class AddType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('summary', TextTypeTest::class)
                ->add('description', TextareaType::class)
                ->add('priority', ChoiceType::class, array(
                    'choices' => array(
                        'Low' => 'low',
                        'Medium' => 'medium',
                        'High' => 'high',
                        'Site Breaking' => 'extreme'
                    )
                ))
                ->add('website', ChoiceType::class, array(
                    'choices' => array(
                        'Site One' => 'site-one',
                        'Site Two' => 'site-two',
                        'Site Three' => 'site-three'
                    )
                ));
        }

        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array());
        }
    }

my twig file is just a basic implementation of the form:

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

I'm not sure why this error is occuring or how to stop it? I'm not trying anthing with PHPUnit, yet here it rears its ugly head..

1 Answer 1

2

I think you have a problem here:

$builder->add('summary', TextTypeTest::class)

you need to change it to

$builder->add('summary', TextType::class)

So you can delete this use

use Symfony\Component\Form\Tests\Extension\Core\Type\TextTypeTest;

And add this one:

use Symfony\Component\Form\Extension\Core\Type\TextType;
Sign up to request clarification or add additional context in comments.

3 Comments

correct indeed! Damn IDE's and their helpful autocompletes xD ty :)
Glad to help you @ThisGuyHasTwoThumbs
@AlessandroMinoccheri, would you please explain why this fixes the OP's issue and relationship between the original error message and the fix which apparently worked?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.