PHP-单元测试:如何实现TYPO3 Extbase对象接口
PHP-Unit Tests: how to implement a TYPO3 Extbase object interface
我的控制器 PHP 单元测试有一些问题:
这是我的控制器代码中需要测试的函数
public function newAction(\ReRe\Rere\Domain\Model\Fach $newFach = NULL) {
// Holt die übergebene Modulnummer
if ($this->request->hasArgument('modul')) {
// Holt das Modul-Objekt aus dem Repository
$modul = $this->modulRepository->findByUid($this->request->getArgument('modul'));
}
// Ausgabe in der View
$this->view->assignMultiple(array(
'newFach' => $newFach, self::MODULUID => $modul->getUid(), 'modulname' => $modul->getModulname(), 'modulnummer' => $modul->getModulnr(), 'gueltigkeitszeitraum' => $modul->getGueltigkeitszeitraum()
));
}
这是PHP函数的单元测试代码
public function newActionAssignsTheGivenFachToView() {
$fach = new \ReRe\Rere\Domain\Model\Fach();
$modul = array('');
$MockGetArgument = $this->getMock('ReRe\Rere\Domain\Repository\ModulRepository', array('getArgument'), array(), '', FALSE);
$MockGetArgument->expects($this->any())->method('getArgument')->with('modul');
$mockRequest = $this->getMock('TYPO3\CMS\Extbase\Mvc\Request');
$mockRequest->expects($this->any())->method('hasArgument')->with('modul');
$this->inject($this->subject, 'request', $mockRequest);
$objectManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\ObjectManager', array(), array(), '', FALSE);
$this->inject($this->subject, 'objectManager', $objectManager);
$modulRepository = $this->getMock('ReRe\Rere\Domain\Repository\ModulRepository');
$modulRepository->expects($this->any())->method('findByUid')->will($this->returnValue($modul));
$this->inject($this->subject, 'modulRepository', $modulRepository);
$view = $this->getMock(self::VIEWINTERFACE);
$view->expects($this->any())->method(self::ASSIGN)->with('newFach', $fach);
$this->inject($this->subject, 'view', $view);
$this->subject->newAction($fach);
}
我在 运行 测试
时不断收到此错误
测试用例 newActionAssignsTheGivenFachToView 出错
文件:/Applications/MAMP/typo3_src/typo3/sysext/extbase/Classes/Mvc/Controller/AbstractController.php
线路:162
传递给 TYPO3\CMS\Extbase\Mvc\Controller\AbstractController::injectObjectManager() 的参数 1 必须实现接口 TYPO3\CMS\Extbase\Object\ObjectManagerInterface,给定的 Mock_ObjectManager_fa2fde18 实例,在第 260 行的 /Applications/MAMP/typo3_src/typo3/sysext/core/Tests/BaseTestCase.php 中调用并定义
这是来自 AbstractController.php 的那行被称为
public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $ObjectManager) {
$this->ObjectManager = $ObjectManager;
$this->arguments = $this->ObjectManager->get('TYPO3\CMS\Extbase\Mvc\Controller\Arguments');
}
如何实现这个接口TYPO3\CMS\Extbase\Object\ObjectManagerInterface?
非常感谢每一个答案!几周来我一直在尝试寻找答案:(
2015 年 2 月 18 日更新:问题已解决
配置代码:
$fach = new \ReRe\Rere\Domain\Model\Fach();
$modul = new \ReRe\Rere\Domain\Model\Modul();
$request = $this->getMock(self::REQUEST, array(), array(), '', FALSE);
$request->expects($this->once())->method('hasArgument')->will($this->returnValue($this->subject));
$modulRepository = $this->getMock(self::MODULREPOSITORY, array('findByUid'), array(), '', FALSE);
$modulRepository->expects($this->once())->method('findByUid')->will($this->returnValue($modul));
$this->inject($this->subject, 'modulRepository', $modulRepository);
$request->expects($this->once())->method('getArgument')->will($this->returnValue($this->subject));
$this->inject($this->subject, 'request', $request);
$view = $this->getMock(self::VIEWINTERFACE);
$view->expects($this->once())->method('assignMultiple')->with(array(
'newFach' => $fach,
'moduluid' => $modul->getUid(),
'modulname' => $modul->getModulname(),
'modulnummer' => $modul->getModulnr(),
'gueltigkeitszeitraum' => $modul->getGueltigkeitszeitraum()
));
$this->inject($this->subject, 'view', $view);
$this->subject->newAction($fach);
您在模拟错误的(不存在的)ObjectManager。正确的命名空间是 TYPO3\CMS\Extbase\Object\ObjectManager
.
所以正确的行应该是$objectManager = $this->getMock('TYPO3\CMS\Extbase\Object\ObjectManager', ...);
否则我想知道你为什么要模拟 ObjectManager。你没有在你的方法中使用它。
附带说明:如果请求中没有 'modul',您的代码将失败。然后 $modul
未设置,您将在非对象上调用 getUid()
、getModulname()
、...。
我的控制器 PHP 单元测试有一些问题:
这是我的控制器代码中需要测试的函数
public function newAction(\ReRe\Rere\Domain\Model\Fach $newFach = NULL) {
// Holt die übergebene Modulnummer
if ($this->request->hasArgument('modul')) {
// Holt das Modul-Objekt aus dem Repository
$modul = $this->modulRepository->findByUid($this->request->getArgument('modul'));
}
// Ausgabe in der View
$this->view->assignMultiple(array(
'newFach' => $newFach, self::MODULUID => $modul->getUid(), 'modulname' => $modul->getModulname(), 'modulnummer' => $modul->getModulnr(), 'gueltigkeitszeitraum' => $modul->getGueltigkeitszeitraum()
));
}
这是PHP函数的单元测试代码
public function newActionAssignsTheGivenFachToView() {
$fach = new \ReRe\Rere\Domain\Model\Fach();
$modul = array('');
$MockGetArgument = $this->getMock('ReRe\Rere\Domain\Repository\ModulRepository', array('getArgument'), array(), '', FALSE);
$MockGetArgument->expects($this->any())->method('getArgument')->with('modul');
$mockRequest = $this->getMock('TYPO3\CMS\Extbase\Mvc\Request');
$mockRequest->expects($this->any())->method('hasArgument')->with('modul');
$this->inject($this->subject, 'request', $mockRequest);
$objectManager = $this->getMock('TYPO3\CMS\Extbase\Persistence\ObjectManager', array(), array(), '', FALSE);
$this->inject($this->subject, 'objectManager', $objectManager);
$modulRepository = $this->getMock('ReRe\Rere\Domain\Repository\ModulRepository');
$modulRepository->expects($this->any())->method('findByUid')->will($this->returnValue($modul));
$this->inject($this->subject, 'modulRepository', $modulRepository);
$view = $this->getMock(self::VIEWINTERFACE);
$view->expects($this->any())->method(self::ASSIGN)->with('newFach', $fach);
$this->inject($this->subject, 'view', $view);
$this->subject->newAction($fach);
}
我在 运行 测试
时不断收到此错误测试用例 newActionAssignsTheGivenFachToView 出错 文件:/Applications/MAMP/typo3_src/typo3/sysext/extbase/Classes/Mvc/Controller/AbstractController.php 线路:162 传递给 TYPO3\CMS\Extbase\Mvc\Controller\AbstractController::injectObjectManager() 的参数 1 必须实现接口 TYPO3\CMS\Extbase\Object\ObjectManagerInterface,给定的 Mock_ObjectManager_fa2fde18 实例,在第 260 行的 /Applications/MAMP/typo3_src/typo3/sysext/core/Tests/BaseTestCase.php 中调用并定义
这是来自 AbstractController.php 的那行被称为
public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $ObjectManager) {
$this->ObjectManager = $ObjectManager;
$this->arguments = $this->ObjectManager->get('TYPO3\CMS\Extbase\Mvc\Controller\Arguments');
}
如何实现这个接口TYPO3\CMS\Extbase\Object\ObjectManagerInterface?
非常感谢每一个答案!几周来我一直在尝试寻找答案:(
2015 年 2 月 18 日更新:问题已解决
配置代码:
$fach = new \ReRe\Rere\Domain\Model\Fach();
$modul = new \ReRe\Rere\Domain\Model\Modul();
$request = $this->getMock(self::REQUEST, array(), array(), '', FALSE);
$request->expects($this->once())->method('hasArgument')->will($this->returnValue($this->subject));
$modulRepository = $this->getMock(self::MODULREPOSITORY, array('findByUid'), array(), '', FALSE);
$modulRepository->expects($this->once())->method('findByUid')->will($this->returnValue($modul));
$this->inject($this->subject, 'modulRepository', $modulRepository);
$request->expects($this->once())->method('getArgument')->will($this->returnValue($this->subject));
$this->inject($this->subject, 'request', $request);
$view = $this->getMock(self::VIEWINTERFACE);
$view->expects($this->once())->method('assignMultiple')->with(array(
'newFach' => $fach,
'moduluid' => $modul->getUid(),
'modulname' => $modul->getModulname(),
'modulnummer' => $modul->getModulnr(),
'gueltigkeitszeitraum' => $modul->getGueltigkeitszeitraum()
));
$this->inject($this->subject, 'view', $view);
$this->subject->newAction($fach);
您在模拟错误的(不存在的)ObjectManager。正确的命名空间是 TYPO3\CMS\Extbase\Object\ObjectManager
.
所以正确的行应该是$objectManager = $this->getMock('TYPO3\CMS\Extbase\Object\ObjectManager', ...);
否则我想知道你为什么要模拟 ObjectManager。你没有在你的方法中使用它。
附带说明:如果请求中没有 'modul',您的代码将失败。然后 $modul
未设置,您将在非对象上调用 getUid()
、getModulname()
、...。