1
votes

I'm doing a standalone unit testing with phpunit for an extbase extension.

here is my phpunt.xml located in 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>

my folder structure is shown below

enter image description here

And the DummyControllerTest file is here

<?php
namespace Ricky\TestExtension\Tests\Unit\Controller;
/***************************************************************
 *  Copyright notice
 *
 *  (c) 2016 Ricky Mathew <[email protected]>, 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 Ricky\TestExtension\Controller\DummyController.
 *
 * @author Ricky Mathew <[email protected]>
 */
class DummyControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
{

    /**
     * @var \Ricky\TestExtension\Controller\DummyController
     */
    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 \Ricky\TestExtension\Domain\Model\Dummy();

        $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);
    }
}

But on running phpunit via commandline it throws

Fatal error: Class 'TYPO3\CMS\Core\Tests\UnitTestCase' not found in /opt/xampp/htdocs/typo3testpro/typo3conf/ext/test_extension/Tests/Unit/Controller/DummyControllerTest.php on line 37

Why it isn't autoloading?I tried to instantiate TYPO3\CMS\Core\Tests\UnitTestCase inside my controller class just for testing and it is autoloading there perfectly.

1

1 Answers

2
votes

You need to bootstrap autoloading in PHPUnit (not only for TYPO3). To do so, add the attribute bootstrap to you <phpunit> element in the configuration file. It's a path to a file which is executed before the tests, and it should set up autoloading.

In TYPO3 context, you can use the file typo3/sysext/core/Build/UnitTestsBootstrap.php, it is provided by the TYPO3 core.

If you are running a project without TYPO3, you usually need to include the composer generated autoload file, by default it's the file vendor/autoload.php.

Your config file should look like this afterwards:

<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>