【发布时间】:2018-09-21 00:10:59
【问题描述】:
开始使用单元测试表单,我正在尝试测试一个包含表单类型集合的表单。我的测试未能断言在提交表单后处理了 FeatureOption 对象。
如何使用我提供的所有数据全面测试此表单?
$ ./vendor/bin/simple-phpunit tests/Unit/Form/ PHPUnit 6.5.7
运行时:PHP 7.2.4-1+ubuntu16.04.1+deb.sury.org+1 和 Xdebug 2.7.0alpha2-dev 配置:/var/www/phpunit.xml.dist
测试测试/Unit/Form/F
1 / 1 (100%)时间:599 毫秒,内存:6.00MB
有 1 次失败:
1) App\Tests\Unit\Form\FeatureFormTest::submitValidData 断言两个对象相等时失败。
--- 预期
+++ 实际 @@@@ - 0 => App\Entity\FeatureOption 对象 (...)/var/www/tests/Unit/Form/FeatureFormTest.php:55
失败!测试:1,断言:2,失败:1。
src\Entity\Feature.php:
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Feature Entity
*
* @ORM\Entity()
* @ORM\Table(name = "feature")
*/
class Feature extends AbstractEntity
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->options = new ArrayCollection();
}
/**
* ID
*
* @var integer
*
* @ORM\Id
* @ORM\Column(name = "feature_id", type = "integer")
* @ORM\GeneratedValue(strategy = "AUTO")
*/
protected $id;
/**
* Name
*
* @var string
*
* @ORM\Column(name = "feature_name", type = "string", length = 128)
*/
protected $name;
/**
* Options
*
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity = "FeatureOption", mappedBy = "feature", cascade = { "persist", "remove" }, fetch = "EAGER")
*/
protected $options;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Feature
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add option
*
* @param \App\Entity\FeatureOption $option
*
* @return Feature
*/
public function addOption(FeatureOption $option)
{
$option->setFeature($this);
$this->options[] = $option;
return $this;
}
/**
* Remove option
*
* @param \App\Entity\FeatureOption $option
*/
public function removeOption(FeatureOption $option)
{
$this->options->removeElement($option);
}
/**
* Get options
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getOptions()
{
return $this->options;
}
}
src\Entity\FeatureOption.php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Feature Entity
*
* @ORM\Entity()
* @ORM\Table(name = "feature_option")
*/
class FeatureOption extends AbstractEntity
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
}
/**
* ID
*
* @var integer
*
* @ORM\Id
* @ORM\Column(name = "feature_option_id", type = "integer")
* @ORM\GeneratedValue(strategy = "AUTO")
*/
protected $id;
/**
* Value
*
* @var string
*
* @ORM\Column(name = "feature_option_value", type = "string", length = 256)
*/
protected $value;
/**
* Feature
*
* @var Feature
*
* @ORM\ManyToOne(targetEntity = "Feature", inversedBy = "options")
* @ORM\JoinColumn(name = "feature_id", referencedColumnName = "feature_id")
*/
protected $feature;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set value
*
* @param string $value
*
* @return FeatureOption
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* Set feature
*
* @param \App\Entity\Feature $feature
*
* @return FeatureOption
*/
public function setFeature(Feature $feature = null)
{
$this->feature = $feature;
return $this;
}
/**
* Get feature
*
* @return \App\Entity\Feature
*/
public function getFeature()
{
return $this->feature;
}
}
src\Form\FeatureForm.php
namespace App\Form;
use App\Form\Type\FeatureOptionType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class FeatureForm extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$translationDomain = "admin.feature";
$builder->add("name", TextType::class, [
"label" => "feature.name",
"required" => true,
"translation_domain" => $translationDomain,
]);
$builder->add("options", CollectionType::class, [
"label" => "feature.options",
"entry_type" => FeatureOptionType::class,
"by_reference" => false,
"allow_add" => true,
"allow_delete" => true,
'prototype' => true,
"translation_domain" => $translationDomain,
]);
}
}
src\Form\Type\FeatureOptionType.php
namespace App\Form\Type;
use App\Entity\FeatureOption;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class FeatureOptionType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$translationDomain = "admin.feature";
$builder->add("value", TextType::class, [
"label" => "feature.option.value",
"required" => true,
"translation_domain" => $translationDomain,
]);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => FeatureOption::class,
));
}
}
测试\Unit\Form\FeatureFormTest.php
namespace App\Tests\Unit\Form;
use App\Entity\Feature;
use App\Entity\FeatureOption;
use App\Form\FeatureForm;
use App\Form\Type\FeatureOptionType;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
class FeatureFormTest extends TypeTestCase
{
/**
* @test
*/
public function submitValidData()
{
$date = new \DateTime();
$featureOption = new FeatureOption();
$featureOption->setValue("Indestructable");
$formData = [
"name" => "Durability",
"options" => new ArrayCollection(array($featureOption)),
];
$featureComparedToForm = new Feature();
$featureComparedToForm->setCreatedAt($date);
$featureComparedToForm->setName($formData["name"]);
foreach ($formData["options"] as $option) {
$featureComparedToForm->addOption($option);
}
$featureHandledByForm = new Feature();
$featureHandledByForm->setCreatedAt($date);
$form = $this->factory->create(FeatureForm::class, $featureHandledByForm);
$form->submit($formData);
static::assertTrue($form->isSynchronized());
static::assertEquals($featureComparedToForm, $featureHandledByForm);
$view = $form->createView();
foreach (array_keys($formData) as $key) {
static::assertArrayHasKey($key, $view->children);
}
}
/**
* {@inheritdoc}
*/
public function getExtensions()
{
$featureOptionType = new FeatureOptionType();
return [
new PreloadedExtension([$featureOptionType], []),
];
}
}
【问题讨论】:
标签: unit-testing symfony collections symfony4