‪TYPO3CMS  10.4
ImageScriptServiceTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪ImageScriptServiceTest extends UnitTestCase
32 {
36  protected ‪$resetSingletonInstances = true;
37 
41  protected ‪$subject;
42 
46  protected ‪$environmentService;
47 
51  protected function ‪setUp(): void
52  {
53  parent::setUp();
54  $this->environmentService = $this->createMock(EnvironmentService::class);
55  $resourceFactory = $this->createMock(ResourceFactory::class);
56  $this->subject = new ‪ImageService($this->environmentService, $resourceFactory);
57  $_SERVER['HTTP_HOST'] = 'foo.bar';
58  }
59 
63  public function ‪fileIsUnwrappedFromReferenceForProcessing(): void
64  {
65  $reference = $this->getMockBuilder(FileReference::class)->disableOriginalConstructor()->getMock();
66  $file = $this->createMock(File::class);
67  $processedFile = $this->createMock(ProcessedFile::class);
68  $file->expects(self::once())->method('process')->willReturn($processedFile);
69  $reference->expects(self::once())->method('getOriginalFile')->willReturn($file);
70  $processedFile->expects(self::once())->method('getOriginalFile')->willReturn($file);
71  $processedFile->expects(self::atLeastOnce())->method('getPublicUrl')->willReturn('https://example.com/foo.png');
72 
73  $this->subject->applyProcessingInstructions($reference, []);
74  }
75 
80  {
81  return [
82  'with scheme' => ['http://foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
83  'scheme relative' => ['//foo.bar/img.jpg', '//foo.bar/img.jpg'],
84  'without scheme' => ['foo.bar/img.jpg', '/prefix/foo.bar/img.jpg'],
85  ];
86  }
87 
92  public function ‪prefixIsCorrectlyAppliedToGetImageUri($imageUri, $expected): void
93  {
94  $this->environmentService->expects(self::any())->method('isEnvironmentInFrontendMode')->willReturn(true);
95  ‪$GLOBALS['TSFE'] = new \stdClass();
96  ‪$GLOBALS['TSFE']->absRefPrefix = '/prefix/';
97 
98  $file = $this->createMock(File::class);
99  $file->expects(self::once())->method('getPublicUrl')->willReturn($imageUri);
100 
101  self::assertSame($expected, $this->subject->getImageUri($file));
102  }
103 
108  {
109  return [
110  'with scheme' => ['http://foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
111  'scheme relative' => ['//foo.bar/img.jpg', 'http://foo.bar/img.jpg'],
112  'without scheme' => ['foo.bar/img.jpg', 'http://foo.bar/prefix/foo.bar/img.jpg'],
113  ];
114  }
115 
120  public function ‪prefixIsCorrectlyAppliedToGetImageUriWithForcedAbsoluteUrl($imageUri, $expected): void
121  {
122  $this->environmentService->expects(self::any())->method('isEnvironmentInFrontendMode')->willReturn(true);
123  ‪$GLOBALS['TSFE'] = new \stdClass();
124  ‪$GLOBALS['TSFE']->absRefPrefix = '/prefix/';
125 
126  $file = $this->createMock(File::class);
127  $file->expects(self::once())->method('getPublicUrl')->willReturn($imageUri);
128 
129  self::assertSame($expected, $this->subject->getImageUri($file, true));
130  }
131 }
‪TYPO3\CMS\Extbase\Tests\Unit\Service
Definition: CacheServiceTest.php:16
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\prefixIsCorrectlyAppliedToGetImageUriDataProvider
‪array prefixIsCorrectlyAppliedToGetImageUriDataProvider()
Definition: ImageScriptServiceTest.php:76
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest
Definition: ImageScriptServiceTest.php:32
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:35
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ImageScriptServiceTest.php:35
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\prefixIsCorrectlyAppliedToGetImageUri
‪prefixIsCorrectlyAppliedToGetImageUri($imageUri, $expected)
Definition: ImageScriptServiceTest.php:89
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\setUp
‪setUp()
Definition: ImageScriptServiceTest.php:48
‪TYPO3\CMS\Extbase\Service\EnvironmentService
Definition: EnvironmentService.php:27
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:44
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\fileIsUnwrappedFromReferenceForProcessing
‪fileIsUnwrappedFromReferenceForProcessing()
Definition: ImageScriptServiceTest.php:60
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\$environmentService
‪EnvironmentService PHPUnit Framework MockObject MockObject $environmentService
Definition: ImageScriptServiceTest.php:43
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\prefixIsCorrectlyAppliedToGetImageUriWithAbsolutePathDataProvider
‪array prefixIsCorrectlyAppliedToGetImageUriWithAbsolutePathDataProvider()
Definition: ImageScriptServiceTest.php:104
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\prefixIsCorrectlyAppliedToGetImageUriWithForcedAbsoluteUrl
‪prefixIsCorrectlyAppliedToGetImageUriWithForcedAbsoluteUrl($imageUri, $expected)
Definition: ImageScriptServiceTest.php:117
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ImageScriptServiceTest\$subject
‪ImageService $subject
Definition: ImageScriptServiceTest.php:39