【发布时间】:2017-03-16 10:24:31
【问题描述】:
自从我开始使用 Zend Framework 3 以来,我在测试我的控制器时遇到了问题。我正在尝试使用 PhpUnit 5.7 测试我的控制器,而我的控制器依赖于 Zend Form,它使用 Doctrine 的 DoctrineObject 进行了水合。
我试图让它尽可能简单,所以这里有一个让我头疼的设置的最小示例:
控制器:
class IndexController extends AbstractActionController {
private $form;
public function __construct(AlbumForm $form) {
$this->form = $form;
}
public function indexAction() {
return ['form' => $this-form];
}
}
控制器工厂:
class IndexControllerFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, ...) {
$formManager = $container->get('FormElementManager');
return new IndexController($formManager->get(AlbumForm::class));
}
}
albums/index/index.phtml中对应的视图模板:
<?php
$this->form->prepare();
$this->form->setAttribute('action', $this->url(null, [], true));
$albumFieldset = $this->form->get('album');
?>
<?= $this->form()->openTag($this-form) ?>
<div class="form-group">
<?= $this->formRow($albumFieldset->get('name')) ?>
</div>
<?= $this->form()->closeTag() ?>
形式:
class AlbumForm extends Form {
public function init() {
$this->add([
'name' => 'albumFieldset',
'type' => AlbumFieldset::class,
'options' => [
'use_as_base_fieldset' => true,
],
]);
}
}
字段集:
class AlbumFieldset extends Fieldset {
public function init() {
$this->add([
'name' => 'name',
'type' => Text::class,
'options' => [
'label' => 'Name of album',
],
]);
}
}
FieldsetFactory:
class AlbumFieldsetFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, ...) {
$objectManager = $container->get(ObjectManager::class);
$fieldset = new AlbumFieldset();
$fieldset->setHydrator(new DoctrineObject($objectManager));
$fieldset->setObject(new Album());
return $fieldset;
}
}
现在,到目前为止一切正常。
但是,在为此编写测试时,我遇到了麻烦。让我先向您展示我目前所拥有的:
class IndexControllerTest extends AbstractHttpControllerTestCase {
protected function setUp() {
parent::setUp();
$this->configureServiceManager($this->getApplicationServiceLocator());
}
private function configureServiceManager(ServiceManager $services) {
$services->setAllowOverride(true);
$services->setService(ObjectManager::class, $this->mockObjectManager()->reveal());
$services->setService('FormElementManager', $this->mockFormManager()->reveal());
$services->setAllowOverride(false);
}
private $objectManager;
private function mockObjectManager() {
$this->objectManager = $this->prophesize(ObjectManager::class);
return $this->objectManager;
}
private $formManager;
private function mockFormManager() {
$this->formManager = $this->prophesize(FormElementManager::class);
$this->formManager->get(AlbumForm::class)->willReturn($this->mockForm()->reveal());
return $this->formManager;
}
private $form;
private function mockForm() {
$this->form = $this->prophesize(AlbumForm::class);
$this->form->prepare()->willReturn(null);
$this->form->setAttribute('action', Argument::type('string'))->willReturn(null);
$this->form->getAttributes()->willReturn([]);
$this->form->get('album')->willReturn($this->mockAlbumFieldset()->reveal());
return $this->form;
}
private $albumFieldset;
private function mockAlbumFieldset() {
$this->albumFieldset = $this->prophesize(AlbumFieldset::class);
$this->albumFieldset->get('name')->willReturn($this->mockName()->reveal());
return $this->albumFieldset;
}
private $name;
private function mockName() {
$this->name = $this->prophesize(Text::class);
$this->name->getLabel()->willReturn('label');
$this->name->getLabelAttributes()->willReturn(['for' => 'name']);
$this->name->getLabelOption('disable_html_escape')->willReturn(false);
$this->name->getLabelOption('always_wrap')->willReturn(false);
$this->name->getLabelOption('label_position')->willReturn('prepend');
$this->name->getName('album[name]');
$this->name->getAttribute('type')->willReturn('text');
$this->name->hasAttribute('id')->willReturn(true);
$this->name->getAttribute('id')->willReturn('name');
$this->name->getAttributes([])->willReturn([]);
$this->name->getValue()->willReturn(null);
$this->name->getMessages()->willReturn([]);
return $this->name;
}
}
这最终将无错误地运行。但是,我想提请您注意最后几个方法,尤其是mockName()。这些定义中的大多数都是完全默认的,并且几乎没有一个在开头的 AlbumFieldset 中指定(只有名称)。为我可能拥有的每个表单输入写下它们是非常烦人的,并且写下来实际上引入的错误比它解决的要多。例如,我仍然不确定always_wrap 的正确标签选项是什么。实际上我什至不关心那个选项,但我必须在我的测试中写一些关于它的东西,因为否则测试会以'Prophecy\Exception\Call\UnexpectedCallException' with message 'Method call: - getLabelOption("always_wrap") on Double\Zend\Form\Element\Text\P245 was not expected, expected calls were: ... 失败。
因此,我问你:有没有更好的方法来解决这个问题?一种不涉及为我的字段集中的每个字段编写 20 多行的方法。如果它涉及重写我的控制器/字段集/视图模板(等),那完全没问题!
非常感谢任何帮助!另外,这是我编程八年以来第一次在论坛上提问,如果有不清楚的地方请多多包涵。
你的 史蒂芬
PS:我已经尝试过给IndexController null 而不是实际的表单,并在检测到表单为空时简单地中止视图模板。然而,虽然没有那么多设置就可以工作,但我基本上只是避免了视图模板的逻辑。因此,我无法检测到视图模板中的错误。这不是我想要的。
【问题讨论】:
标签: php forms unit-testing zend-framework zend-framework3