‪TYPO3CMS  ‪main
BytesViewHelperTest.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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
23 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
24 use TYPO3Fluid\Fluid\View\TemplateView;
25 
26 final class ‪BytesViewHelperTest extends FunctionalTestCase
27 {
28  protected bool ‪$initializeDatabase = false;
29 
30  public static function ‪renderConvertsAValueDataProvider(): array
31  {
32  return [
33  // invalid values
34  [
35  '<f:format.bytes value="invalid" units="B,KB,MB,GB,TB,PB,EB,ZB,YB" />',
36  '0 B',
37  ],
38  [
39  '<f:format.bytes value="" decimals="2" units="B,KB,MB,GB,TB,PB,EB,ZB,YB" />',
40  '0.00 B',
41  ],
42  [
43  '<f:format.bytes value="{}" decimals="2" units="B,KB,MB,GB,TB,PB,EB,ZB,YB" />',
44  '0.00 B',
45  ],
46  // valid values
47  [
48  '<f:format.bytes value="123" units="B,KB,MB,GB,TB,PB,EB,ZB,YB" />',
49  '123 B',
50  ],
51  [
52  '<f:format.bytes value="43008" decimals="1" units="B,KB,MB,GB,TB,PB,EB,ZB,YB" />',
53  '42.0 KB',
54  ],
55  [
56  '<f:format.bytes value="1024" decimals="1" units="B,KB,MB,GB,TB,PB,EB,ZB,YB" />',
57  '1.0 KB',
58  ],
59  [
60  '<f:format.bytes value="1023" decimals="2" units="B,KB,MB,GB,TB,PB,EB,ZB,YB" />',
61  '1,023.00 B',
62  ],
63  [
64  '<f:format.bytes value="1073741823" decimals="1" thousandsSeparator="." units="B,KB,MB,GB,TB,PB,EB,ZB,YB" />',
65  '1.024.0 MB',
66  ],
67  [
68  '<f:format.bytes value="{1024 ^ 5}" decimals="1" units="B,KB,MB,GB,TB,PB,EB,ZB,YB" />',
69  '1.0 PB',
70  ],
71  [
72  '<f:format.bytes value="{1024 ^ 8}" decimals="1" units="B,KB,MB,GB,TB,PB,EB,ZB,YB" />',
73  '1.0 YB',
74  ],
75  [
76  '<f:format.bytes units="B,KB,MB,GB,TB,PB,EB,ZB,YB">12345</f:format.bytes>',
77  '12 KB',
78  ],
79  ];
80  }
81 
82  #[DataProvider('renderConvertsAValueDataProvider')]
83  #[Test]
84  public function ‪renderConvertsAValue(string $src, string $expected): void
85  {
86  $context = $this->get(RenderingContextFactory::class)->create();
87  $context->getTemplatePaths()->setTemplateSource($src);
88  self::assertSame($expected, (new TemplateView($context))->render());
89  }
90 }
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format\BytesViewHelperTest
Definition: BytesViewHelperTest.php:27
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format\BytesViewHelperTest\renderConvertsAValue
‪renderConvertsAValue(string $src, string $expected)
Definition: BytesViewHelperTest.php:84
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format\BytesViewHelperTest\$initializeDatabase
‪bool $initializeDatabase
Definition: BytesViewHelperTest.php:28
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format
Definition: BytesViewHelperTest.php:18
‪TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers\Format\BytesViewHelperTest\renderConvertsAValueDataProvider
‪static renderConvertsAValueDataProvider()
Definition: BytesViewHelperTest.php:30
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51