dongzhong7299 2015-02-16 17:09
浏览 50

控制器测试PHPUnit不起作用

I have some problems with my PHPUnit test, because the PHPUnit default test suite doesn't actually cover the code complexity and I kept receiving error with this PHPUnit test.

below is the controller code:

class ModulController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {

    const RETURNMODUL = "returnModul";
    const MODULNAME = "modulname";
    const MODULNUMMER = "modulnummer";
    const GUELTIGKEITSZEITRAUM = "gueltigkeitszeitraum";
    const FACHNAME = "fachname";
    const FACHNUMMER = "fachnummer";
    const PRUEFER = "pruefer";
    const NEWSTRING = "new";
    const NOTENSCHEMA = "notenschema";

    /**
     * Protected Variable modulRepository wird mit NULL initialisiert.
     *
     * @var \ReRe\Rere\Domain\Repository\ModulRepository
     * @inject
     */
    protected $modulRepository = NULL;

    /**
     * Protected Variable fachRepository wird mit NULL initialisiert.
     *
     * @var \ReRe\Rere\Domain\Repository\FachRepository
     * @inject
     */
    protected $fachRepository = NULL;

    /**
     * Protected Variable noteRepository wird mit NULL initialisiert.
     *
     * @var \ReRe\Rere\Domain\Repository\NoteRepository
     * @inject
     */
    protected $noteRepository = NULL;

    /**
     * Protected Variable prueflingRepository wird mit NULL initialisiert.
     *
     * @var \ReRe\Rere\Domain\Repository\PrueflingRepository
     * @inject
     */
    protected $prueflingRepository = NULL;

    /**
     * Protected Variable intervallRepository wird mit NULL initialisiert.
     *
     * @var \ReRe\Rere\Domain\Repository\IntervallRepository
     * @inject
     */
    protected $intervallRepository = NULL;

    /**
     * Protected Variable settingsRepository wird mit NULL initialisiert.
     *
     * @var \ReRe\Rere\Domain\Repository\SettingsRepository
     * @inject
     */
    protected $settingsRepository = NULL;

    /**
     * Protected Variable objectManager wird mit NULL initialisiert.
     *
     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
     * @inject
     */
    protected $objectManager = NULL;

    /**
     * Mit dieser Methode werden alle Module des aktuell ausgewählten Intervalls angezeigt.
     *
     * @return void
     */
    public function listAction() {
        $moduls = $this->modulRepository->findAll();
        $intervall = $this->intervallRepository->findByUid(1);
        $settings = $this->settingsRepository->findByUid(1);
        $filteredmoduls = array();

        // Prüfen ob Settings leer sind
        if ($settings == Null) {
            $mail = $this->objectManager->create('\\ReRe\\Rere\\Domain\\Model\\Settings');
            $mail->setMailAbsender("DEFAULT");
            $this->settingsRepository->add($mail);
        }

        // Prüfen, ob die Tabelle wirklich einen Wert hat (also ob ein Intervall gesetzt wurde).
        if ($intervall == Null) {
            // wenn Intervall noch nicht gesetzt ist, wird ein Intervall-Objekt erzeugt
            $createdIntervall = $this->objectManager->create('\\ReRe\\Rere\\Domain\\Model\\Intervall');
            $createdIntervall->setAktuell('WS14/15');
            $createdIntervall->setType('studienhalbjahr');
            $this->intervallRepository->add($createdIntervall);
            $this->redirect('list');
        }
        if ($intervall != Null) {
            // wenn Intervall gesetzt ist, wird es geholt.
            $akteullesintervall = $intervall->getAktuell();
            $intervallType = $intervall->getType();
        }
        // Alle Module des aktuellen Intervalls holen
        foreach ($moduls as $modul) {
            if ($modul->getGueltigkeitszeitraum() == $akteullesintervall) {
                array_push($filteredmoduls, $modul);
            }
        }
        // Ausgabe an View
        $this->view->assignMultiple(array(
            'aktuellintervall' => $akteullesintervall,
            'intervallType' => $intervallType,
            'moduls' => $filteredmoduls
        ));
        return $this->view->render();
    }
  }

and below this is my configured test code:

class ModulControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {

    const OBJECTMANAGER = 'TYPO3\\CMS\\Extbase\\Object\\ObjectManager';
    const MODULCONTROLLER = 'ReRe\\Rere\\Controller\\ModulController';
    const MODULREPOSITORY = 'ReRe\\Rere\\Domain\\Repository\\ModulRepository';
    const INTERVALLREPOSITORY = 'ReRe\\Rere\\Domain\\Repository\\IntervallRepository';
    const SETTINGSREPOSITORY = 'ReRe\\Rere\\Domain\\Repository\\SettingsRepository';
    const MODULREPO = 'modulRepository';
    const INTERVALLREPO = 'intervallRepository';
    const SETTINGSREPO = 'settingsRepository';
    const VIEWINTERFACE = 'TYPO3\\CMS\\Extbase\\Mvc\\View\\ViewInterface';
    const ASSIGN = "assign";
    const ASSIGNMULTIPLE = "assignmultiple";

    /**
     * @var \ReRe\Rere\Controller\ModulController
     */
    protected $subject = NULL;

    protected function setUp() {
        $this->subject = $this->getMock(self::MODULCONTROLLER, array('redirect', 'forward', 'addFlashMessage'), array(), '', FALSE);
    }

    protected function tearDown() {
        unset($this->subject);
    }

    /**
     * @test
     */
    public function listActionFetchesAllModulsFromRepositoryAndAssignsThemToView() {

        $allModuls = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage', array(), array(), '', FALSE);
        $modulRepository = $this->getMock(self::MODULREPOSITORY, array('findAll'), array(), '', FALSE);
        $modulRepository->expects($this->once())->method('findAll')->will($this->returnValue($allModuls));
        $this->inject($this->subject, self::MODULREPO, $modulRepository);
        
        $allSettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage', array(), array(), '', FALSE);
        $intervall = new \ReRe\Rere\Domain\Model\Intervall();
        
        $mail = $this->getMock('\\ReRe\\Rere\\Domain\\Model\\Settings', array(), array(), '', FALSE);
        $mail->expects($this->once())->method('setMailAbsender')->with("DEFAULT");
        
        $settingsRepository = $this->getMock(self::SETTINGSREPOSITORY, array('add'), array(), '', FALSE);
        $settingsRepository->expects($this->once())->method('add')->will($this->returnValue($mail));
        $this->inject($this->subject, self::SETTINGSREPO, $settingsRepository);

        $createdIntervall = $this->getMock('\\ReRe\\Rere\\Domain\\Model\\Intervall', array(), array(), '', FALSE);
        $createdIntervall->expects($this->at(0))->method('setAktuell')->with('WS14/15');
        $createdIntervall->expects($this->at(1))->method('setType')->with('studienhalbjahr');
        
        $intervallRepository = $this->getMock(self::INTERVALLREPOSITORY, array('add'), array(), '', FALSE);
        $intervallRepository->expects($this->once())->method('add')->will($this->returnValue($createdIntervall));
        $this->inject($this->subject, self::INTERVALLREPO, $intervallRepository);
        $this->subject->expects($this->once())->method('redirect')->with('list');
        
        $aktuellesIntervall= $this->getMock('\\ReRe\\Rere\\Domain\\Model\\Intervall', array(), array(), '', FALSE);
        $aktuellesIntervall->expects($this->once())->method('getAktuell');
        $intervallType= $this->getMock('\\ReRe\\Rere\\Domain\\Model\\Intervall', array(), array(), '', FALSE);
        $intervallType->expects($this->once())->method('getType');
        
        $objectManager = $this->getMock(SELF::OBJECTMANAGER, array(), array(), '', FALSE);
        $objectManager->expects($this->once())->method('create')->will($this->returnValue($intervall));
        $objectManager->expects($this->once())->method('create')->will($this->returnValue($allSettings));
        $this->inject($this->subject, 'objectManager', $objectManager);

        $view = $this->getMock(self::VIEWINTERFACE);
        $view->expects($this->once())->method(self::ASSIGNMULTIPLE)->with(array(
            'intervallType' => $intervallType,
            'aktuellintervall' => $aktuellesIntervall,
            'moduls' => $allModuls
        ));
        $this->inject($this->subject, 'view', $view);

        $this->subject->listAction();
    }
  }

the test was ran on PHPUnit v 4.3.5, the test failed, but I received no error messages at all. The test just failed like that. any answers will be very appreciated. Been looking for answers for about a month..

you can see the error below

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测