我正在尝试将一个表单添加到控制器中,它只返回此错误:
致命错误:第21行的/var/www/site/vendor/symfony/form/Test/FormIntegrationTestCase.php中找不到类'PHPUnit \ Framework \ TestCase'
虽然,我不太清楚为什么。乍一看,似乎找不到PHPUnit,但在我的目录中运行{
"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"
}
}
}
说不然:
<?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('t.taylor@sinnis.co.uk')
->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()));
}
}
所以我可以在那里看到..这是我的控制器:
<?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());
}
}
我的FormType:
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
我的twig文件只是表单的基本实现:
pub struct SignedTransaction {
transaction: UnverifiedTransaction,
sender: Address,
public: Option<Public>,
}
impl Deref for SignedTransaction {
type Target = UnverifiedTransaction;
fn deref(&self) -> &Self::Target {
&self.transaction
}
}
pub struct UnverifiedTransaction {
unsigned: Transaction,
v: u64,
r: U256,
s: U256,
hash: H256,
}
impl Deref for UnverifiedTransaction {
type Target = Transaction;
fn deref(&self) -> &Self::Target {
&self.unsigned
}
}
pub struct Transaction {
pub nonce: U256,
pub gas_price: U256,
pub gas: U256,
pub action: Action,
pub value: U256,
pub data: Bytes,
}
pub enum Action {
Create,
Call(Address),
}
我不确定为什么会出现此错误或如何阻止它?我并没有尝试使用PHPUnit,但是在这里它看起来很难看......
答案 0 :(得分:2)
我认为你有问题:
$builder->add('summary', TextTypeTest::class)
您需要将其更改为
$builder->add('summary', TextType::class)
所以你可以删除这个用途
use Symfony\Component\Form\Tests\Extension\Core\Type\TextTypeTest;
并添加以下内容:
use Symfony\Component\Form\Extension\Core\Type\TextType;