Skip to content

Commit

Permalink
[TASK] Unit tests: Do not rely on CacheManager instance
Browse files Browse the repository at this point in the history
The typo3/testing-framework bootstrap up until now
initialized the CacheManager singleton and put it
into GeneralUtility instance stack.
To better isolate the tests and as a next step in
side effect free uni testing, the test bootstrap
now cleans this instance after use:

composer update typo/testing-framework

About 300 tests fail with this and are fixed with
the patch by improving their mocking.

Change-Id: Ia3e9dd1f94af10b20e0463a0062ccde74be23660
Resolves: #84004
Releases: master
Reviewed-on: https://review.typo3.org/55855
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
  • Loading branch information
lolli42 authored and andreaskienast committed Feb 21, 2018
1 parent 017e870 commit 73bfbe1
Show file tree
Hide file tree
Showing 35 changed files with 575 additions and 579 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@
* The TYPO3 project - inspiring people to share!
*/

use Prophecy\Argument;
use TYPO3\CMS\Backend\Controller\File\FileController;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

/**
* Tests for \TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileController
*/
class FileControllerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
class FileControllerTest extends UnitTestCase
{
/**
* @var \TYPO3\CMS\Backend\Controller\File\FileController|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
*/
protected $fileController;

/**
* @var \TYPO3\CMS\Core\Resource\File|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -81,81 +82,85 @@ protected function setUp()
/**
* @test
*/
public function flattenResultDataValueFlattensFileAndFolderResourcesButReturnsAnythingElseAsIs()
public function flattenResultDataValueReturnsAnythingElseAsIs()
{
$this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, ['dummy']);
$subject = $this->getAccessibleMock(FileController::class, ['dummy']);
$this->assertTrue($subject->_call('flattenResultDataValue', true));
$this->assertSame([], $subject->_call('flattenResultDataValue', []));
}

$this->folderResourceMock->expects($this->once())->method('getIdentifier')->will($this->returnValue('bar'));
/**
* @test
*/
public function flattenResultDataValueFlattensFile()
{
$subject = $this->getAccessibleMock(FileController::class, ['dummy']);

$this->mockFileProcessor->expects($this->any())->method('getErrorMessages')->will($this->returnValue([]));
$iconFactoryProphecy = $this->prophesize(IconFactory::class);
GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
$iconProphecy = $this->prophesize(Icon::class);
$iconProphecy->render()->shouldBeCalled()->willReturn('');
$iconFactoryProphecy->getIconForFileExtension(Argument::cetera())->willReturn($iconProphecy->reveal());

$this->assertTrue($this->fileController->_call('flattenResultDataValue', true));
$this->assertSame([], $this->fileController->_call('flattenResultDataValue', []));
$result = $this->fileController->_call('flattenResultDataValue', $this->fileResourceMock);
$this->assertContains('<span class="t3js-icon icon icon-size-small icon-state-default icon-mimetypes-text-html" data-identifier="mimetypes-text-html">', $result['icon']);
unset($result['icon']);
$result = $subject->_call('flattenResultDataValue', $this->fileResourceMock);
$this->assertSame(
[
'id' => 'foo',
'date' => '29-11-73',
'icon' => '',
'thumbUrl' => '',
],
$result
);

$this->assertSame(
'bar',
$this->fileController->_call('flattenResultDataValue', $this->folderResourceMock)
);
}

/**
* @test
*/
public function processAjaxRequestDeleteProcessActuallyDoesNotChangeFileData()
{
$this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, ['init', 'main']);
$subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);

$fileData = ['delete' => [true]];
$this->fileController->_set('fileProcessor', $this->mockFileProcessor);
$this->fileController->_set('fileData', $fileData);
$this->fileController->_set('redirect', false);
$subject->_set('fileProcessor', $this->mockFileProcessor);
$subject->_set('fileData', $fileData);
$subject->_set('redirect', false);

$this->fileController->expects($this->once())->method('main');
$subject->expects($this->once())->method('main');

$this->fileController->processAjaxRequest($this->request, $this->response);
$subject->processAjaxRequest($this->request, $this->response);
}

/**
* @test
*/
public function processAjaxRequestEditFileProcessActuallyDoesNotChangeFileData()
{
$this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, ['init', 'main']);
$subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);

$fileData = ['editfile' => [true]];
$this->fileController->_set('fileProcessor', $this->mockFileProcessor);
$this->fileController->_set('fileData', $fileData);
$this->fileController->_set('redirect', false);
$subject->_set('fileProcessor', $this->mockFileProcessor);
$subject->_set('fileData', $fileData);
$subject->_set('redirect', false);

$this->fileController->expects($this->once())->method('main');
$subject->expects($this->once())->method('main');

$this->fileController->processAjaxRequest($this->request, $this->response);
$subject->processAjaxRequest($this->request, $this->response);
}

/**
* @test
*/
public function processAjaxRequestReturnsStatus200IfNoErrorOccures()
{
$this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, ['init', 'main']);
$subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);

$fileData = ['editfile' => [true]];
$this->fileController->_set('fileProcessor', $this->mockFileProcessor);
$this->fileController->_set('fileData', $fileData);
$this->fileController->_set('redirect', false);
$subject->_set('fileProcessor', $this->mockFileProcessor);
$subject->_set('fileData', $fileData);
$subject->_set('redirect', false);

$result = $this->fileController->processAjaxRequest($this->request, $this->response);
$result = $subject->processAjaxRequest($this->request, $this->response);
$this->assertEquals(200, $result->getStatusCode());
}

Expand All @@ -164,10 +169,10 @@ public function processAjaxRequestReturnsStatus200IfNoErrorOccures()
*/
public function processAjaxRequestReturnsStatus500IfErrorOccurs()
{
$this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, ['init', 'main']);
$subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
$this->mockFileProcessor->expects($this->any())->method('getErrorMessages')->will($this->returnValue(['error occured']));
$this->fileController->_set('fileProcessor', $this->mockFileProcessor);
$result = $this->fileController->processAjaxRequest($this->request, $this->response);
$subject->_set('fileProcessor', $this->mockFileProcessor);
$result = $subject->processAjaxRequest($this->request, $this->response);
$this->assertEquals(500, $result->getStatusCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
use TYPO3\CMS\Backend\Form\Element\InputDateTimeElement;
use TYPO3\CMS\Backend\Form\NodeExpansion\FieldInformation;
use TYPO3\CMS\Backend\Form\NodeFactory;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

/**
Expand Down Expand Up @@ -116,6 +119,11 @@ public function renderAppliesCorrectTimestampConversion($input, $serverTimezone,
'additionalHiddenFields' => [],
'stylesheetFiles' => [],
]);
$iconFactoryProphecy = $this->prophesize(IconFactory::class);
GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
$iconProphecy = $this->prophesize(Icon::class);
$iconProphecy->render()->willReturn('');
$iconFactoryProphecy->getIcon(Argument::cetera())->willReturn($iconProphecy->reveal());
$nodeFactoryProphecy = $this->prophesize(NodeFactory::class);
$nodeFactoryProphecy->create(Argument::cetera())->willReturn($abstractNode->reveal());
$fieldInformationProphecy = $this->prophesize(FieldInformation::class);
Expand Down

0 comments on commit 73bfbe1

Please sign in to comment.