Je fais des tests unitaires autonomes avec phpunit pour une extension de base.
voici mon phpunt.xml situé dans typo3conf /
<?xml version="1.0" encoding="utf-8"?>
<phpunit colors="true">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./ext/test_extension/Tests/</directory>
</testsuite>
</testsuites>
</phpunit>
ma structure de dossier est affichée ci-dessous
Et le fichier DummyControllerTest est ici
<?php
namespace RickyTestExtensionTestsUnitController;
/***************************************************************
* Copyright notice
*
* (c) 2016 Ricky Mathew <ricky.mk@pitsolutions.com>, Pits
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Test case for class RickyTestExtensionControllerDummyController.
*
* @author Ricky Mathew <ricky.mk@pitsolutions.com>
*/
class DummyControllerTest extends TYPO3CMSCoreTestsUnitTestCase
{
/**
* @var RickyTestExtensionControllerDummyController
*/
protected $subject = NULL;
public function setUp()
{
$this->subject = $this->getMock("Ricky\TestExtension\Controller\DummyController", array("redirect", "forward", "addFlashMessage"), array(), "", FALSE);
}
public function tearDown()
{
unset($this->subject);
}
/**
* @test
*/
public function listActionFetchesAllDummiesFromRepositoryAndAssignsThemToView()
{
$allDummies = $this->getMock("TYPO3\CMS\Extbase\Persistence\ObjectStorage", array(), array(), "", FALSE);
$dummyRepository = $this->getMock("Ricky\TestExtension\Domain\Repository\DummyRepository", array("findAll"), array(), "", FALSE);
$dummyRepository->expects($this->once())->method("findAll")->will($this->returnValue($allDummies));
$this->inject($this->subject, "dummyRepository", $dummyRepository);
$view = $this->getMock("TYPO3\CMS\Extbase\Mvc\View\ViewInterface");
$view->expects($this->once())->method("assign")->with("dummies", $allDummies);
$this->inject($this->subject, "view", $view);
$this->subject->listAction();
}
/**
* @test
*/
public function showActionAssignsTheGivenDummyToView()
{
$dummy = new RickyTestExtensionDomainModelDummy();
$view = $this->getMock("TYPO3\CMS\Extbase\Mvc\View\ViewInterface");
$this->inject($this->subject, "view", $view);
$view->expects($this->once())->method("assign")->with("dummy", $dummy);
$this->subject->showAction($dummy);
}
}
Mais en exécutant phpunit via la ligne de commande, il jette
Erreur fatale: la classe "TYPO3CMSCoreTestsUnitTestCase" ne figure pas dans /opt/xampp/htdocs/typo3testpro/typo3conf/ext/test_extension/Tests/Unit/Controller/DummyControllerTest.php à la ligne 37
Pourquoi ce n'est pas "autoloading? J'ai essayé d'instancier TYPO3CMSCoreTestsUnitTestCase
dans ma classe de contrôleurs juste pour les tests et le chargement automatique y est parfait.
Réponses:
2 pour la réponse № 1Vous devez amorcer le chargement automatique dans PHPUnit (pas seulement pour TYPO3). Pour ce faire, ajoutez l'attribut bootstrap
à toi <phpunit>
élément dans le fichier de configuration. C'est un chemin d'accès à un fichier qui est exécuté avant les tests, et il devrait configurer le chargement automatique.
Dans le contexte TYPO3, vous pouvez utiliser le fichier typo3/sysext/core/Build/UnitTestsBootstrap.php
, il est fourni par le noyau TYPO3.
Si vous exécutez un projet sans TYPO3, vous devez généralement inclure le fichier de chargement automatique généré par le compositeur. Par défaut, il s’agit du fichier. vendor/autoload.php
.
Votre fichier de configuration devrait ressembler à ceci par la suite:
<phpunit
colors="true"
bootstrap="../typo3/sysext/core/Build/UnitTestsBootstrap.php"
>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./ext/test_extension/Tests/</directory>
</testsuite>
</testsuites>
</phpunit>