forked from FluidTYPO3/vhs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Clean up and cover two base VH classes
- Loading branch information
1 parent
547028a
commit 7d14504
Showing
4 changed files
with
273 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
Tests/Unit/ViewHelpers/Resource/AbstractImageViewHelperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
namespace FluidTYPO3\Vhs\Tests\Unit\ViewHelpers\Resource; | ||
|
||
/* | ||
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
use FluidTYPO3\Vhs\Tests\Unit\AbstractTestCase; | ||
use FluidTYPO3\Vhs\ViewHelpers\Resource\AbstractImageViewHelper; | ||
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder; | ||
use TYPO3\CMS\Core\Http\ServerRequest; | ||
use TYPO3\CMS\Core\Resource\File; | ||
use TYPO3\CMS\Core\Resource\ResourceStorage; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; | ||
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; | ||
|
||
class AbstractImageViewHelperTest extends AbstractTestCase | ||
{ | ||
private ?AbstractImageViewHelper $subject = null; | ||
private ?ContentObjectRenderer $contentObjectRenderer = null; | ||
|
||
protected function setUp(): void | ||
{ | ||
$GLOBALS['TYPO3_REQUEST'] = $this->getMockBuilder(ServerRequest::class) | ||
->setMethods(['getAttribute']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$GLOBALS['TYPO3_REQUEST']->method('getAttribute')->willReturn(SystemEnvironmentBuilder::REQUESTTYPE_FE); | ||
|
||
$this->subject = $this->getMockBuilder(AbstractImageViewHelper::class) | ||
->disableOriginalConstructor() | ||
->getMockForAbstractClass(); | ||
$this->contentObjectRenderer = $this->getMockBuilder(ContentObjectRenderer::class) | ||
->setMethods(['getImgResource']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$configurationManager = $this->getMockBuilder(ConfigurationManagerInterface::class) | ||
->getMockForAbstractClass(); | ||
$configurationManager->method('getContentObject')->willReturn($this->contentObjectRenderer); | ||
|
||
$this->subject->injectConfigurationManager($configurationManager); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function testProcessImage(): void | ||
{ | ||
$path = 'https://foo.bar/path/to/file'; | ||
$file = $this->getMockBuilder(File::class)->disableOriginalConstructor()->getMock(); | ||
self::assertSame( | ||
[ | ||
[ | ||
'info' => [ | ||
123, | ||
456, | ||
789, | ||
$path, | ||
], | ||
'source' => $path, | ||
'file' => $file, | ||
], | ||
], | ||
$this->runTestWithImage($file, $path, false), | ||
); | ||
} | ||
|
||
public function testProcessImageWithOnlyProperties(): void | ||
{ | ||
$path = '/path/to/file'; | ||
|
||
$storage = $this->getMockBuilder(ResourceStorage::class) | ||
->setMethods(['getFileInfo']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$storage->method('getFileInfo')->willReturn(['foo' => 'bar']); | ||
|
||
$file = $this->getMockBuilder(File::class) | ||
->setMethods(['getStorage', 'hasProperty', 'getProperty', 'getProperties', 'toArray']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$file->method('getStorage')->willReturn($storage); | ||
$file->method('hasProperty')->willReturn(true); | ||
$file->method('getProperty')->willReturn('prop'); | ||
$file->method('getProperties')->willReturn(['foo' => 'bar']); | ||
$file->method('toArray')->willReturn(['foo' => 'bar']); | ||
|
||
self::assertSame( | ||
[ | ||
[ | ||
'info' => [ | ||
123, | ||
456, | ||
789, | ||
'/path/to/file', | ||
], | ||
'source' => $path, | ||
'file' => ['foo' => 'bar'], | ||
], | ||
], | ||
$this->runTestWithImage($file, $path, true), | ||
); | ||
} | ||
|
||
public function testProcessImageThrowsExceptionOnInvalidImage(): void | ||
{ | ||
$files = [ | ||
$this->getMockBuilder(File::class)->disableOriginalConstructor()->getMock(), | ||
]; | ||
$this->contentObjectRenderer->method('getImgResource')->willReturn(null); | ||
self::expectExceptionCode(1253191060); | ||
$this->subject->preprocessImages($files); | ||
} | ||
|
||
private function runTestWithImage(File $file, string $path, bool $onlyProperties): array | ||
{ | ||
$GLOBALS['TSFE'] = (object) ['lastImageInfo' => null, 'imagesOnPage' => [], 'absRefPrefix' => '']; | ||
$this->contentObjectRenderer->method('getImgResource')->willReturn( | ||
[ | ||
123, | ||
456, | ||
789, | ||
$path | ||
] | ||
); | ||
return $this->subject->preprocessImages([$file], $onlyProperties); | ||
} | ||
|
||
public function testPreProcessSourceUriWithPrependPath(): void | ||
{ | ||
$GLOBALS['TSFE'] = (object) [ | ||
'tmpl' => (object) ['setup' => ['plugin.' => ['tx_vhs.' => ['settings.' => ['prependPath' => 'prepend']]]]], | ||
]; | ||
|
||
$output = $this->subject->preprocessSourceUri('source'); | ||
self::assertSame('prependsource', $output); | ||
} | ||
|
||
public function testPreProcessSourceUriInBackendContext(): void | ||
{ | ||
$GLOBALS['TYPO3_REQUEST'] = $this->getMockBuilder(ServerRequest::class) | ||
->setMethods(['getAttribute']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$GLOBALS['TYPO3_REQUEST']->method('getAttribute')->willReturn(SystemEnvironmentBuilder::REQUESTTYPE_BE); | ||
|
||
$output = $this->subject->preprocessSourceUri('source'); | ||
self::assertSame(GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'source', $output); | ||
} | ||
} |
Oops, something went wrong.