class ByteSizeMarkupTest

Same name and namespace in other branches
  1. 10 core/tests/Drupal/Tests/Core/StringTranslation/ByteSizeMarkupTest.php \Drupal\Tests\Core\StringTranslation\ByteSizeMarkupTest

@coversDefaultClass \Drupal\Core\StringTranslation\ByteSizeMarkup @group StringTranslation

Hierarchy

Expanded class hierarchy of ByteSizeMarkupTest

File

core/tests/Drupal/Tests/Core/StringTranslation/ByteSizeMarkupTest.php, line 17

Namespace

Drupal\Tests\Core\StringTranslation
View source
class ByteSizeMarkupTest extends UnitTestCase {
    
    /**
     * @covers ::create
     * @dataProvider providerTestCommonFormatSize
     */
    public function testCommonFormatSize($expected, $input) : void {
        $size = ByteSizeMarkup::create($input, NULL, $this->getStringTranslationStub());
        $this->assertInstanceOf(TranslatableMarkup::class, $size);
        $this->assertEquals($expected, $size);
    }
    
    /**
     * Provides a list of byte size to test.
     */
    public static function providerTestCommonFormatSize() {
        $kb = Bytes::KILOBYTE;
        return [
            [
                '0 bytes',
                0,
            ],
            // @todo https://www.drupal.org/node/3161118 Prevent display of fractional
            //   bytes for size less then 1KB.
[
                '0.1 bytes',
                0.1,
            ],
            [
                '0.6 bytes',
                0.6,
            ],
            [
                '1 byte',
                1,
            ],
            [
                '-1 bytes',
                -1,
            ],
            [
                '2 bytes',
                2,
            ],
            [
                '-2 bytes',
                -2,
            ],
            [
                '1023 bytes',
                $kb - 1,
            ],
            [
                '1 KB',
                $kb,
            ],
            [
                '1 MB',
                pow($kb, 2),
            ],
            [
                '1 GB',
                pow($kb, 3),
            ],
            [
                '1 TB',
                pow($kb, 4),
            ],
            [
                '1 PB',
                pow($kb, 5),
            ],
            [
                '1 EB',
                pow($kb, 6),
            ],
            [
                '1 ZB',
                pow($kb, 7),
            ],
            [
                '1 YB',
                pow($kb, 8),
            ],
            [
                '1024 YB',
                pow($kb, 9),
            ],
            // Rounded to 1 MB - not 1000 or 1024 kilobytes
[
                '1 MB',
                $kb * $kb - 1,
            ],
            [
                '-1 MB',
                -($kb * $kb - 1),
            ],
            // Decimal Megabytes
[
                '3.46 MB',
                3623651,
            ],
            [
                '3.77 GB',
                4053371676,
            ],
            // Decimal Petabytes
[
                '59.72 PB',
                67234178751368124,
            ],
            // Decimal Yottabytes
[
                '194.67 YB',
                2.3534682382112583E+26,
            ],
        ];
    }
    
    /**
     * @covers ::create
     */
    public function testTranslatableMarkupObject() : void {
        $result = ByteSizeMarkup::create(1, NULL, $this->getStringTranslationStub());
        $this->assertInstanceOf(PluralTranslatableMarkup::class, $result);
        $this->assertEquals("1 byte\x03@count bytes", $result->getUntranslatedString());
        $result = ByteSizeMarkup::create(1048576, 'fr', $this->getStringTranslationStub());
        $this->assertInstanceOf(TranslatableMarkup::class, $result);
        $this->assertEquals("@size MB", $result->getUntranslatedString());
        $this->assertEquals('fr', $result->getOption('langcode'));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ByteSizeMarkupTest::providerTestCommonFormatSize public static function Provides a list of byte size to test.
ByteSizeMarkupTest::testCommonFormatSize public function @covers ::create
@dataProvider providerTestCommonFormatSize
ByteSizeMarkupTest::testTranslatableMarkupObject public function @covers ::create
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 354
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.